Skip to content

Commit 3c8af94

Browse files
committed
Add more examples to documentation
- add some clarifications
1 parent f86de60 commit 3c8af94

File tree

2 files changed

+177
-35
lines changed

2 files changed

+177
-35
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ For example, this code:
5656

5757
yields the output:
5858

59-
$ py.test test_foo.py -vv
59+
$ pytest test_foo.py -vv
6060
============================= test session starts ==============================
6161
platform darwin -- Python 3.7.1, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- env/bin/python
6262
plugins: order

docs/source/index.rst

Lines changed: 176 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ These attributes can be absolute (i.e. first, or second-to-last) or relative
99
.. note::
1010
It is generally considered bad practice to write tests that depend on each
1111
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.
12+
issues, legacy code or other constraints. Still, before using this plugin,
13+
you should check if you can refactor your tests to remove the dependencies
14+
between tests.
1415

1516
Relationship with pytest-ordering
1617
---------------------------------
@@ -31,6 +32,9 @@ Here are examples for which markers correspond to markers in
3132
- ``pytest.mark.order1``, ``pytest.mark.run(order=1)`` => ``pytest.mark.order(1)``
3233
- ``pytest.mark.first`` => ``pytest.mark.order("first")``
3334

35+
Additionally, ``pytest-order`` provides a number of features (relative
36+
ordering, all configuration options) that are not available in
37+
``pytest-ordering``.
3438

3539
Supported Python and pytest versions
3640
------------------------------------
@@ -73,7 +77,7 @@ the output is something like:
7377

7478
::
7579

76-
$ py.test test_foo.py -vv
80+
$ pytest test_foo.py -vv
7781
============================= test session starts ==============================
7882
platform darwin -- Python 3.7.1, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- env/bin/python
7983
collected 2 items
@@ -101,7 +105,7 @@ This will generate the output:
101105

102106
::
103107

104-
$ py.test test_foo.py -vv
108+
$ pytest test_foo.py -vv
105109
============================= test session starts ==============================
106110
platform darwin -- Python 3.7.1, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- env/bin/python
107111
plugins: order
@@ -162,7 +166,7 @@ are used in Python lists, e.g. to count from the end:
162166
163167
::
164168

165-
$ py.test test_foo.py -vv
169+
$ pytest test_foo.py -vv
166170
============================= test session starts ==============================
167171
platform darwin -- Python 3.7.1, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- env/bin/python
168172
plugins: order
@@ -206,7 +210,7 @@ same effect as the numbers 0, 1, -1 and -2 that have been shown above:
206210
207211
::
208212

209-
$ py.test test_foo.py -vv
213+
$ pytest test_foo.py -vv
210214
============================= test session starts ==============================
211215
platform darwin -- Python 3.7.1, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- env/bin/python
212216
plugins: order
@@ -271,7 +275,7 @@ by their name:
271275
272276
::
273277

274-
$ py.test test_foo.py -vv
278+
$ pytest test_foo.py -vv
275279
============================= test session starts ==============================
276280
platform darwin -- Python 3.7.1, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- env/bin/python
277281
plugins: order
@@ -307,8 +311,39 @@ with a ``::`` suffix has to be prepended to the test name:
307311
assert True
308312
309313
If the referenced test lives in another module, the test name has to be
310-
prepended by the module path to be uniquely identifiable
311-
(e.g. ``mod_test.test_something`` or ``mod_test.TestA.test_a``).
314+
prepended by the module path to be uniquely identifiable. Let's say we have
315+
the following module and test layout:
316+
317+
::
318+
319+
test_module_a.py
320+
TestA
321+
test_a
322+
test_b
323+
test_module_b.py
324+
test_a
325+
test_b
326+
test_module_c.py
327+
test_submodule.py
328+
test_1
329+
test_2
330+
331+
Suppose the tests in ``test_module_b`` shall depend on tests in the other
332+
modules, this could be expressed like:
333+
334+
**test_module_b.py**
335+
336+
.. code:: python
337+
338+
import pytest
339+
340+
@pytest.mark.order(after='test_module_a.TestA::test_a')
341+
def test_a():
342+
assert True
343+
344+
@pytest.mark.order(before='test_module_c.test_submodule.test_2')
345+
def test_b():
346+
assert True
312347
313348
If an unknown test is referenced, a warning is issued and the test in
314349
question is ordered behind all other tests.
@@ -343,13 +378,19 @@ want to execute the tests in a specific order to each other, you can use
343378
``pytest-order``. If you want to skip or xfail tests dependent on other
344379
tests you can use ``pytest-dependency``. If you want to have both behaviors
345380
combined, you can use both plugins together with the
346-
option :ref:`order-dependencies`--see below for more information.
381+
option :ref:`order-dependencies`, described below.
347382

348383
Configuration
349384
=============
350385
There are a few command line options that change the behavior of the
351386
plugin. As with any pytest option, you can add the options to your
352-
``pytest.ini`` if you want to have them applied to all tests automatically.
387+
``pytest.ini`` if you want to have them applied to all tests automatically:
388+
389+
.. code::
390+
391+
[pytest]
392+
; always order tests with dependency markers
393+
addopts = --order-dependencies
353394
354395
.. _order-scope:
355396

@@ -371,6 +412,66 @@ separate test functions, these test functions are handled separately from the
371412
test classes. If a module has no test classes, the effect is the same as
372413
if using ``--order-scope=module``.
373414

415+
.. note::
416+
This option only affects ordinal ordering. Ordering relative to other tests
417+
is always global, as the related tests are referenced by name.
418+
419+
For example consider two test modules:
420+
421+
**tests/test_module1.py**:
422+
423+
.. code::
424+
425+
import pytest
426+
427+
@pytest.mark.order(2)
428+
def test2():
429+
pass
430+
431+
@pytest.mark.order(1)
432+
def test1():
433+
pass
434+
435+
**tests/test_module2.py**:
436+
437+
.. code::
438+
439+
import pytest
440+
441+
@pytest.mark.order(2)
442+
def test2():
443+
pass
444+
445+
@pytest.mark.order(1)
446+
def test1():
447+
pass
448+
449+
Here is what you get using session and module-scoped sorting:
450+
451+
::
452+
453+
$ pytest tests -vv
454+
============================= test session starts ==============================
455+
...
456+
457+
tests/test_module1.py:9: test1 PASSED
458+
tests/test_module2.py:9: test1 PASSED
459+
tests/test_module1.py:5: test2 PASSED
460+
tests/test_module2.py:5: test2 PASSED
461+
462+
463+
::
464+
465+
$ pytest tests -vv --order-scope=module
466+
============================= test session starts ==============================
467+
...
468+
469+
tests/test_module1.py:9: test1 PASSED
470+
tests/test_module1.py:5: test2 PASSED
471+
tests/test_module2.py:9: test1 PASSED
472+
tests/test_module2.py:5: test2 PASSED
473+
474+
374475
``--indulgent-ordering``
375476
------------------------
376477
You may sometimes find that you want to suggest an ordering of tests, while
@@ -433,21 +534,42 @@ are executed in the same order as:
433534
def test_one():
434535
assert True
435536
436-
namely, they are run in the order ``test_one``, ``test_two``, ``test_three``
437-
and ``test_four``. The gaps between numbers, and the fact that the starting
438-
number is not 0, are ignored. This is consistent with the current behavior of
537+
e.g. you get:
538+
539+
::
540+
541+
$ pytest tests -vv
542+
============================= test session starts ==============================
543+
...
544+
test_module.py:13: test_one PASSED
545+
test_module.py:3: test_two PASSED
546+
test_module.py:6: test_three PASSED
547+
test_module.py:9: test_four PASSED
548+
549+
The gaps between numbers, and the fact that the starting number is not 0,
550+
are ignored. This is consistent with the current behavior of
439551
``pytest-ordering``.
440552

441-
If you use the ``--sparse-ordering`` option, the behavior will change: now
442-
all missing numbers (starting with 0) are filled with unordered tests, as
443-
long as unordered tests are left. So the shown example will now order as
444-
``test_three`` (filled in for the missing number 0), ``test_one``,
445-
``test_four`` (filled in for the missing number 2), and ``test_two``. This
446-
will also work for tests with negative order numbers (or the respective names).
447-
The missing ordinals are filled with unordered tests first from the start,
448-
then from the end if there are negative numbers, and the rest will be in
449-
between (e.g. between positive and negative numbers), as it is without this
450-
option.
553+
If you use the ``--sparse-ordering`` option, the behavior will change:
554+
555+
::
556+
557+
$ pytest tests -vv --sparse-ordering
558+
============================= test session starts ==============================
559+
...
560+
test_module.py:6: test_three PASSED
561+
test_module.py:13: test_one PASSED
562+
test_module.py:9: test_four PASSED
563+
test_module.py:3: test_two PASSED
564+
565+
Now all missing numbers (starting with 0) are filled with unordered tests, as
566+
long as unordered tests are left. In the shown example, ``test_three``
567+
is filled in for the missing number 0, and ``test_four`` is filled in for the
568+
missing number 2. This will also work for tests with negative order numbers
569+
(or the respective names). The missing ordinals are filled with unordered
570+
tests first from the start, then from the end if there are negative numbers,
571+
and the rest will be in between (e.g. between positive and negative numbers),
572+
as it is without this option.
451573

452574
.. _order-dependencies:
453575

@@ -476,27 +598,47 @@ following tests:
476598

477599
.. code:: python
478600
479-
import pytest
601+
import pytest
480602
481-
@pytest.mark.dependency(depends=['test_b'])
482-
def test_a():
483-
assert True
603+
@pytest.mark.dependency(depends=['test_b'])
604+
def test_a():
605+
assert True
484606
485-
def test_b():
486-
assert True
607+
@pytest.mark.dependency
608+
def test_b():
609+
assert True
487610
488611
By default, ``test_a`` is not run, because it depends on ``test_b``, which
489-
is only run after ``test_b``. If you use ``--order-dependencies``, this will
490-
change--the tests will now be reordered according to the dependency and both
491-
run. Note that a similar feature may be added to ``pytest-dependency`` -
612+
is only run after ``test_b``:
613+
614+
::
615+
616+
$ pytest tests -vv
617+
============================= test session starts ==============================
618+
...
619+
test_dep.py::test_a SKIPPED
620+
test_dep.py::test_b PASSED
621+
622+
If you use ``--order-dependencies``, this will change--the tests will now be
623+
reordered according to the dependency and both run:
624+
625+
::
626+
627+
$ pytest tests -vv --order-dependencies
628+
============================= test session starts ==============================
629+
...
630+
test_dep.py::test_b PASSED
631+
test_dep.py::test_a PASSED
632+
633+
Note that a similar feature may be added to ``pytest-dependency`` -
492634
if this is done, this option will not be needed, but for the time being you
493635
can use both plugins together to get this behavior.
494636
Note that ``pytest-order`` does not replace ``pytest-dependency``--it just
495637
adds ordering to the existing functionality if needed.
496638

497639
.. note::
498640
This feature is considered experimental. It may not handle all cases of
499-
defined dependencies. Please write an issue if you need further support.
641+
defined dependencies. Please write an issue if you find any problems.
500642

501643
Miscellaneous
502644
=============

0 commit comments

Comments
 (0)