Skip to content

Commit d30fc3e

Browse files
committed
Document ordering of test modules
1 parent e62fc31 commit d30fc3e

File tree

6 files changed

+80
-0
lines changed

6 files changed

+80
-0
lines changed

CHANGELOG.md

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

9+
### Documentation
10+
* added use case for ordering test modules (see [#51](https://github.com/pytest-dev/pytest-order/issues/51))
11+
912
## [Version 1.3.0](https://pypi.org/project/pytest-order/1.3.0/) (2024-08-22)
1013
Allows to fail tests that cannot be ordered.
1114

docs/source/usage.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,3 +483,30 @@ 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 maybe 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+
496+
In this case, instead of using the module :ref:`order-scope` and marking single tests,
497+
you can just mark the whole test module with the same marker using ``pytestmark`` in each
498+
of the concerned test modules:
499+
500+
.. code:: python
501+
502+
import pytest
503+
504+
pytestmark = pytest.mark.order(1)
505+
506+
def test_1():
507+
...
508+
509+
In this case, all tests in the module will have the same order marker. As the test order
510+
is not changed for tests with the same order number, the tests *inside* each module will be run
511+
in the same order as without any ordering. The order of the test module execution, however,
512+
now depends on the defined ``pytestmark``. No extra command line argument is needed in this case.
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)