Skip to content

Commit 42f3275

Browse files
committed
Document ordering of test modules
- see #51
1 parent 26f453e commit 42f3275

File tree

6 files changed

+86
-0
lines changed

6 files changed

+86
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
* use trusted publisher for release (see https://docs.pypi.org/trusted-publishers/)
1010
* pin Python 3.7 builds to ubuntu 22.04 (not available in 24.04)
1111

12+
### Documentation
13+
* added use case for ordering test modules (see [#51](https://github.com/pytest-dev/pytest-order/issues/51))
14+
1215
## [Version 1.3.0](https://pypi.org/project/pytest-order/1.3.0/) (2024-08-22)
1316
Allows to fail tests that cannot be ordered.
1417

docs/source/usage.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,3 +483,36 @@ Although multiple test order markers create their own parametrization, it can be
483483
test_multiple_markers.py::test_two_and_four[index=4-bbb] PASSED [ 87%]
484484
test_multiple_markers.py::test_two_and_four[index=4-ccc] PASSED [100%]
485485
============================== 8 passed in 0.02s ==============================
486+
487+
488+
Ordering test modules
489+
---------------------
490+
491+
Sometimes you want to order whole test modules instead of single tests.
492+
This may be the case if you are testing several steps of a workflow, where each step
493+
has a number of independent tests in the same test module, but the test modules have
494+
to be executed in a certain order, because the workflow steps depend on each other,
495+
as is for example the case with testing a set of API endpoints.
496+
497+
In this case, instead of using the module :ref:`order-scope` and marking single tests,
498+
you can just mark the whole test module with the same marker using ``pytestmark`` in each
499+
of the concerned test modules:
500+
501+
.. code:: python
502+
503+
import pytest
504+
505+
pytestmark = pytest.mark.order(1)
506+
507+
508+
def test_1():
509+
...
510+
511+
In this case, all tests in the module will have the same order marker. As the test order
512+
is not changed for tests with the same order number, the tests *inside* each module will be run
513+
in the same order as without any ordering. The order of the test module execution, however,
514+
now depends on the defined ``pytestmark``. No extra command line argument is needed in this case.
515+
516+
Modules will be ordered the same way as single tests with order markers: first the modules with
517+
an order marker >= 0 will be executed in ascending marker order, afterwards all modules without order
518+
markers in the same order as without ordering, and finally any modules with a negative order marker.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""Each module uses pytestmark to set the order for that module.
2+
The tests inside each module will be executed in the same order as without ordering,
3+
while the test modules will be ordered as defined in pytestmark, in this case in the order:
4+
test_module3 -> test_module2 -> test_module1
5+
"""
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
3+
pytestmark = pytest.mark.order(3)
4+
5+
6+
def test1():
7+
pass
8+
9+
10+
def test2():
11+
pass
12+
13+
14+
def test3():
15+
pass
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
3+
pytestmark = pytest.mark.order(2)
4+
5+
6+
def test1():
7+
pass
8+
9+
10+
def test2():
11+
pass
12+
13+
14+
def test3():
15+
pass
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
3+
pytestmark = pytest.mark.order(1)
4+
5+
6+
def test1():
7+
pass
8+
9+
10+
def test2():
11+
pass
12+
13+
14+
def test3():
15+
pass

0 commit comments

Comments
 (0)