@@ -9,8 +9,9 @@ These attributes can be absolute (i.e. first, or second-to-last) or relative
9
9
.. note ::
10
10
It is generally considered bad practice to write tests that depend on each
11
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.
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.
14
15
15
16
Relationship with pytest-ordering
16
17
---------------------------------
@@ -31,6 +32,9 @@ Here are examples for which markers correspond to markers in
31
32
- ``pytest.mark.order1 ``, ``pytest.mark.run(order=1) `` => ``pytest.mark.order(1) ``
32
33
- ``pytest.mark.first `` => ``pytest.mark.order("first") ``
33
34
35
+ Additionally, ``pytest-order `` provides a number of features (relative
36
+ ordering, all configuration options) that are not available in
37
+ ``pytest-ordering ``.
34
38
35
39
Supported Python and pytest versions
36
40
------------------------------------
@@ -73,7 +77,7 @@ the output is something like:
73
77
74
78
::
75
79
76
- $ py.test test_foo.py -vv
80
+ $ pytest test_foo.py -vv
77
81
============================= test session starts ==============================
78
82
platform darwin -- Python 3.7.1, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- env/bin/python
79
83
collected 2 items
@@ -101,7 +105,7 @@ This will generate the output:
101
105
102
106
::
103
107
104
- $ py.test test_foo.py -vv
108
+ $ pytest test_foo.py -vv
105
109
============================= test session starts ==============================
106
110
platform darwin -- Python 3.7.1, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- env/bin/python
107
111
plugins: order
@@ -162,7 +166,7 @@ are used in Python lists, e.g. to count from the end:
162
166
163
167
::
164
168
165
- $ py.test test_foo.py -vv
169
+ $ pytest test_foo.py -vv
166
170
============================= test session starts ==============================
167
171
platform darwin -- Python 3.7.1, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- env/bin/python
168
172
plugins: order
@@ -206,7 +210,7 @@ same effect as the numbers 0, 1, -1 and -2 that have been shown above:
206
210
207
211
::
208
212
209
- $ py.test test_foo.py -vv
213
+ $ pytest test_foo.py -vv
210
214
============================= test session starts ==============================
211
215
platform darwin -- Python 3.7.1, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- env/bin/python
212
216
plugins: order
@@ -271,7 +275,7 @@ by their name:
271
275
272
276
::
273
277
274
- $ py.test test_foo.py -vv
278
+ $ pytest test_foo.py -vv
275
279
============================= test session starts ==============================
276
280
platform darwin -- Python 3.7.1, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- env/bin/python
277
281
plugins: order
@@ -307,8 +311,39 @@ with a ``::`` suffix has to be prepended to the test name:
307
311
assert True
308
312
309
313
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
312
347
313
348
If an unknown test is referenced, a warning is issued and the test in
314
349
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
343
378
``pytest-order ``. If you want to skip or xfail tests dependent on other
344
379
tests you can use ``pytest-dependency ``. If you want to have both behaviors
345
380
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 .
347
382
348
383
Configuration
349
384
=============
350
385
There are a few command line options that change the behavior of the
351
386
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
353
394
354
395
.. _order-scope :
355
396
@@ -371,6 +412,66 @@ separate test functions, these test functions are handled separately from the
371
412
test classes. If a module has no test classes, the effect is the same as
372
413
if using ``--order-scope=module ``.
373
414
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
+
374
475
``--indulgent-ordering ``
375
476
------------------------
376
477
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:
433
534
def test_one ():
434
535
assert True
435
536
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
439
551
``pytest-ordering ``.
440
552
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.
451
573
452
574
.. _order-dependencies :
453
575
@@ -476,27 +598,47 @@ following tests:
476
598
477
599
.. code :: python
478
600
479
- import pytest
601
+ import pytest
480
602
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
484
606
485
- def test_b ():
486
- assert True
607
+ @pytest.mark.dependency
608
+ def test_b ():
609
+ assert True
487
610
488
611
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 `` -
492
634
if this is done, this option will not be needed, but for the time being you
493
635
can use both plugins together to get this behavior.
494
636
Note that ``pytest-order `` does not replace ``pytest-dependency ``--it just
495
637
adds ordering to the existing functionality if needed.
496
638
497
639
.. note ::
498
640
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 .
500
642
501
643
Miscellaneous
502
644
=============
0 commit comments