@@ -122,11 +122,17 @@ The above is a trivial example, but ordering is respected across test files.
122
122
regardless of the module and class they reside in. This can be changed
123
123
by using the :ref: `order-scope ` option.
124
124
125
- There are currently three possibilities to define the order:
125
+ Ordering is done either absolutely, by using ordinal numbers that define the
126
+ order, or relative to other tests, using the ``before `` and ``after ``
127
+ attributes of the marker.
128
+
129
+ Ordering by numbers
130
+ -------------------
131
+ The order can be defined by ordinal numbers, or by ordinal strings.
126
132
127
133
Order by index
128
- --------------
129
- As already shown above, the order can be defined using the ordinal numbers.
134
+ ~~~~~~~~~~~~~~
135
+ As already shown above, the order can be defined using ordinal numbers.
130
136
There is a long form that uses the keyword ``index ``, and a short form, that
131
137
uses just the ordinal number--both are shown in the example below. The long
132
138
form may be better readable if you want to combine it with a dependency marker
@@ -172,7 +178,7 @@ are used in Python lists, e.g. to count from the end:
172
178
There is no limit for the numbers that can be used in this way.
173
179
174
180
Order using ordinals
175
- --------------------
181
+ ~~~~~~~~~~~~~~~~~~~~
176
182
177
183
Instead of the numbers, you can use ordinal names such as "first", "second",
178
184
"last", and "second_to_last". These are convenience notations, and have the
@@ -233,8 +239,19 @@ Here is the complete list with the corresponding numbers:
233
239
- 'seventh_to_last': -7
234
240
- 'eighth_to_last': -8
235
241
242
+ Handling of unordered tests
243
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
244
+ By default, tests with no ``order `` mark are executed after all tests with
245
+ positive ordinal numbers (or the respective names), and before tests with
246
+ negative ordinal numbers. The order of these tests in relationship to each
247
+ other is not changed. This behavior may slightly change if the option
248
+ :ref: `sparse-ordering ` is used and the ordinals are not contiguous (see
249
+ below).
250
+
251
+
236
252
Order relative to other tests
237
253
-----------------------------
254
+
238
255
The test order can be defined relative to other tests, which are referenced
239
256
by their name:
240
257
@@ -249,7 +266,7 @@ by their name:
249
266
def test_second ():
250
267
assert True
251
268
252
- @pytest.mark.run (before = ' test_second' )
269
+ @pytest.mark.order (before = ' test_second' )
253
270
def test_first ():
254
271
assert True
255
272
@@ -267,20 +284,67 @@ by their name:
267
284
268
285
=========================== 4 passed in 0.02 seconds ===========================
269
286
287
+ Referencing of tests in other classes or modules
288
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
270
289
If a test is referenced using the unqualified test name as shown in the
271
- example, the test is assumed to be in the current module. For tests in other
272
- modules the qualified test name (e.g. ``my_module.test_something ``) has to
273
- be used.
290
+ example above, the test is assumed to be in the current module and the current
291
+ class, if any. For tests in other classes in the same module the class name
292
+ with a ``:: `` suffix has to be prepended to the test name:
293
+
294
+ .. code :: python
295
+
296
+ import pytest
297
+
298
+ class TestA :
299
+ @pytest.mark.order (after = ' TestB::test_c' )
300
+ def test_a ():
301
+ assert True
302
+
303
+ def test_b ():
304
+ assert True
305
+
306
+ class TestB :
307
+ def test_c ():
308
+ assert True
309
+
310
+ If the referenced test lives in another module, the test name has to be
311
+ prepended by the module path to be uniquely identifiable
312
+ (e.g. ``mod_test.test_something `` or ``mod_test.TestA.test_a ``).
274
313
275
314
If an unknown test is referenced, a warning is issued and the test in
276
315
question is ordered behind all other tests.
277
316
278
- .. note ::
279
- The `pytest-dependency <https://pypi.org/project/pytest-dependency/ >`__
280
- plugin also manages dependencies between tests (skips tests that depend
281
- on skipped or failed tests), but doesn't do any ordering. You can combine
282
- both plugins if you need both options--see :ref: `order-dependencies `
283
- below for more information.
317
+ Combination of absolute and relative ordering
318
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
319
+ If you combine absolute and relative order markers, the ordering is first done
320
+ for the absolute markers (e.g. the ordinals), and afterwards for the relative
321
+ ones. This means that relative ordering always takes preference:
322
+
323
+ .. code :: python
324
+
325
+ import pytest
326
+
327
+ @pytest.mark.order (index = 0 , after = ' test_second' )
328
+ def test_first ():
329
+ assert True
330
+
331
+ @pytest.mark.order (1 )
332
+ def test_second ():
333
+ assert True
334
+
335
+ In this case, ``test_second `` will be executed before ``test_first ``,
336
+ regardless of the ordinal markers.
337
+
338
+ Relationship with pytest-dependency
339
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
340
+ The `pytest-dependency <https://pypi.org/project/pytest-dependency/ >`__
341
+ plugin also manages dependencies between tests (skips tests that depend
342
+ on skipped or failed tests), but currently doesn't do any ordering. If you
343
+ want to execute the tests in a specific order to each other, you can use
344
+ ``pytest-ordering ``. If you want to skip or xfail tests dependent on other
345
+ tests you can use ``pytest-dependency ``. If you want to have both behaviors
346
+ combined, you can use both plugins together with the
347
+ option :ref: `order-dependencies `--see below for more information.
284
348
285
349
Configuration
286
350
=============
@@ -325,6 +389,8 @@ separate test functions, these test functions are handled separately from the
325
389
test classes. If a module has no test classes, the effect is the same as
326
390
if using ``--order-scope=module ``.
327
391
392
+ .. _sparse-ordering :
393
+
328
394
``--sparse-ordering ``
329
395
---------------------
330
396
Ordering tests by ordinals where some numbers are missing by default behaves
@@ -430,9 +496,8 @@ Note that ``pytest-order`` does not replace ``pytest-dependency``--it just
430
496
adds ordering to the existing functionality if needed.
431
497
432
498
.. note ::
433
- This feature is considered experimental. It will not handle all cases of
434
- defined dependencies. If there is sufficient demand (reflected in issues),
435
- this may be expanded.
499
+ This feature is considered experimental. It may not handle all cases of
500
+ defined dependencies. Please write an issue if you need further support.
436
501
437
502
Miscellaneous
438
503
=============
0 commit comments