Skip to content

Commit 6c474ee

Browse files
committed
More documentation updates
- add notes about sparse ordering and handling of unknown dependencies - add note about test dependencies as bad practice - add more tests
1 parent 4a16d8d commit 6c474ee

File tree

4 files changed

+196
-17
lines changed

4 files changed

+196
-17
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ History
2020
This is a fork of [pytest-ordering](https://github.com/ftobia/pytest-ordering).
2121
That project is not maintained anymore, and there are several helpful PRs
2222
that are now integrated into `pytest-order`. The idea and most of the code
23-
has been created by Frank Tobia, the author of that plugin. In case
24-
`pytest-ordering` is revived, this project will be obsolete.
23+
has been created by Frank Tobia, the author of that plugin, and contributors.
2524

2625
Compatibility to `pytest_ordering`
2726
---------------------------------

docs/source/index.rst

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ Introduction
22
============
33
``pytest-order`` is a pytest plugin which allows you to customize the order
44
in which your tests are run. It provides the marker ``order``, that has
5-
attributes that defines when your tests should run in relation to each other.
5+
attributes that define when your tests should run in relation to each other.
66
These attributes can be absolute (i.e. first, or second-to-last) or relative
77
(i.e. run this test before this other test).
88

9+
.. note::
10+
It is generally considered bad practice to write tests that depend on each
11+
other. However, in reality this may still be needed due to performance
12+
problems or legacy code. Still, before using this plugin, you should check
13+
if you can refactor your tests to remove the dependencies between tests.
14+
915
Relationship with pytest-ordering
1016
---------------------------------
1117
``pytest-order`` is a fork of
@@ -110,11 +116,11 @@ Usage
110116
=====
111117
The above is a trivial example, but ordering is respected across test files.
112118

113-
.. note ::
114-
The scope of the ordering is always global, e.g. tests with lower ordinal
115-
numbers are always executed before tests with higher numbers, regardless of
116-
the module and class they reside in. This may be changed to be
117-
configurable in a later version.
119+
.. note::
120+
The scope of the ordering is global per default, e.g. tests with lower
121+
ordinal numbers are always executed before tests with higher numbers,
122+
regardless of the module and class they reside in. This can be changed
123+
by using the :ref:`order-scope` option.
118124

119125
There are currently three possibilities to define the order:
120126

@@ -257,11 +263,19 @@ by their name:
257263

258264
=========================== 4 passed in 0.02 seconds ===========================
259265

266+
If a test is referenced using the unqualified test name as shown in the
267+
example, the test is assumed to be in the current module. For tests in other
268+
modules the qualified test name (e.g. ``my_module.test_something``) has to
269+
be used.
270+
271+
If an unknown test is referenced, a warning is issued and the test in
272+
question is ordered behind all other tests.
273+
260274
.. note::
261-
The `pytest-dependency <https://pypi.org/project/pytest-dependency/>`__
262-
plugin also manages dependencies between tests (skips tests that depend
263-
on skipped or failed tests), but doesn't do any ordering. You can combine
264-
both plugins if you need both options.
275+
The `pytest-dependency <https://pypi.org/project/pytest-dependency/>`__
276+
plugin also manages dependencies between tests (skips tests that depend
277+
on skipped or failed tests), but doesn't do any ordering. You can combine
278+
both plugins if you need both options.
265279

266280
Configuration
267281
=============
@@ -286,6 +300,8 @@ pytest-order *before* the sort from ``--failed-first``, allowing the failed
286300
tests to be sorted to the front (note that in pytest versions from 6.0 on,
287301
this seems not to be needed anymore, at least in this specific case).
288302

303+
.. _order-scope:
304+
289305
``--order-scope``
290306
-----------------
291307
By default, tests are ordered per session, e.g. across all modules in the
@@ -317,6 +333,50 @@ together, we have to put each group of dependent tests in one file, and call
317333
pytest with ``--dist=loadfile`` (this is taken from
318334
`this issue <https://github.com/ftobia/pytest-ordering/issues/36>`__).
319335

336+
Sparse ordinal behavior
337+
-----------------------
338+
Sparse ordering (e.g. missing some ordinals in your markers) behaves the
339+
same as if the the ordinals are consecutive. For example, these tests:
340+
341+
.. code:: python
342+
343+
import pytest
344+
345+
@pytest.mark.order(4)
346+
def test_two():
347+
assert True
348+
349+
def test_three():
350+
assert True
351+
352+
@pytest.mark.order(2)
353+
def test_two():
354+
assert True
355+
356+
have the same output as:
357+
358+
.. code:: python
359+
360+
import pytest
361+
362+
@pytest.mark.order(1)
363+
def test_two():
364+
assert True
365+
366+
def test_three():
367+
assert True
368+
369+
@pytest.mark.order(0)
370+
def test_two():
371+
assert True
372+
373+
namely, the tests are run in the order ``test_one``, ``test_two``,
374+
``test_three``, regardless of the gaps between numbers, and the starting
375+
number being not 0. It would be possible to change the ordering behavior to
376+
fill the gaps between the numbers with tests without a marker, as long as
377+
any are available. However, this leads to some not very intuitive behavior,
378+
so it is currently not implemented. If there will be demand for this kind
379+
of behavior, it can be added in a later version.
320380

321381
.. toctree::
322382
:maxdepth: 2

tests/test_misc.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# -*- coding: utf-8 -*-
22
import re
33

4+
import pytest
5+
46
import pytest_order
57

68

@@ -11,3 +13,11 @@ def test_version_exists():
1113
def test_version_valid():
1214
assert re.match(r"[0-9]+\.[0-9]+(\.[0-9]+)?(dev)?$",
1315
pytest_order.__version__)
16+
17+
18+
def test_markers_registered(capsys):
19+
pytest.main(["--markers"])
20+
out, err = capsys.readouterr()
21+
assert "@pytest.mark.order" in out
22+
# only order is supported as marker
23+
assert out.count("Provided by pytest-order.") == 1

tests/test_ordering.py

Lines changed: 115 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,25 @@ def test_one():
296296
"test_three", "test_four"]
297297

298298

299+
def test_sparse_numbers(item_names_for):
300+
test_content = """
301+
import pytest
302+
303+
@pytest.mark.order(4)
304+
def test_two():
305+
assert True
306+
307+
def test_three():
308+
assert True
309+
310+
@pytest.mark.order(2)
311+
def test_one():
312+
assert True
313+
"""
314+
assert item_names_for(test_content) == ["test_one", "test_two",
315+
"test_three"]
316+
317+
299318
def test_quickstart(item_names_for):
300319
test_content = """
301320
import pytest
@@ -418,12 +437,103 @@ def test_first():
418437
"test_third"]
419438

420439

421-
def test_markers_registered(capsys):
422-
pytest.main(["--markers"])
440+
def test_mixed_markers1(item_names_for):
441+
test_content = """
442+
import pytest
443+
444+
@pytest.mark.order(2)
445+
def test_1():
446+
pass
447+
448+
@pytest.mark.order(after="test_1")
449+
def test_2():
450+
pass
451+
452+
@pytest.mark.order(1)
453+
def test_3():
454+
pass
455+
"""
456+
assert item_names_for(test_content) == ["test_3", "test_1", "test_2"]
457+
458+
459+
def test_mixed_markers2(item_names_for):
460+
test_content = """
461+
import pytest
462+
463+
@pytest.mark.order(2)
464+
def test_1():
465+
pass
466+
467+
@pytest.mark.order(1)
468+
def test_2():
469+
pass
470+
471+
@pytest.mark.order(before="test_2")
472+
def test_3():
473+
pass
474+
"""
475+
assert item_names_for(test_content) == ["test_3", "test_2", "test_1"]
476+
477+
478+
def test_dependency_after_unknown_test(item_names_for, capsys):
479+
test_content = """
480+
import pytest
481+
482+
@pytest.mark.order(after="some_module.test_2")
483+
def test_1():
484+
pass
485+
486+
def test_2():
487+
pass
488+
"""
489+
assert item_names_for(test_content) == ["test_2", "test_1"]
490+
out, err = capsys.readouterr()
491+
warning = ("can not execute test relative to others: "
492+
"some_module.test_2 enqueue them behind the others")
493+
assert warning in out
494+
495+
496+
def test_dependency_before_unknown_test(item_names_for, capsys):
497+
test_content = """
498+
import pytest
499+
500+
@pytest.mark.order(before="test_4")
501+
def test_1():
502+
pass
503+
504+
def test_2():
505+
pass
506+
"""
507+
assert item_names_for(test_content) == ["test_2", "test_1"]
508+
out, err = capsys.readouterr()
509+
warning = ("can not execute test relative to others: "
510+
"test_dependency_before_unknown_test.test_4 enqueue them "
511+
"behind the others")
512+
assert warning in out
513+
514+
515+
def test_dependency_loop(item_names_for, capsys):
516+
test_content = """
517+
import pytest
518+
519+
@pytest.mark.order(after="test_3")
520+
def test_1():
521+
pass
522+
523+
@pytest.mark.order(1)
524+
def test_2():
525+
pass
526+
527+
@pytest.mark.order(before="test_1")
528+
def test_3():
529+
pass
530+
"""
531+
assert item_names_for(test_content) == ["test_2", "test_3", "test_1"]
423532
out, err = capsys.readouterr()
424-
assert "@pytest.mark.order" in out
425-
# only order is supported as marker
426-
assert out.count("Provided by pytest-order.") == 1
533+
warning = ("can not execute test relative to others: "
534+
"test_dependency_loop.test_1 test_dependency_loop.test_3 "
535+
"enqueue them behind the others")
536+
assert warning in out
427537

428538

429539
def test_unsupported_order(item_names_for):

0 commit comments

Comments
 (0)