1
+ ===================================
1
2
pytest-random-order
2
3
===================================
3
4
4
- .. image :: https://img.shields.io/badge/python-2.6%2C%202.7%2C%203.5-blue.svg
5
- :target: https://github.com/jbasko/pytest-random-order
6
-
7
- .. image :: https://img.shields.io/badge/coverage-98%25-green.svg
5
+ .. image :: https://img.shields.io/badge/python-3.5%2C%203.6%2C%203.7-blue.svg
8
6
:target: https://github.com/jbasko/pytest-random-order
9
7
10
8
.. image :: https://travis-ci.org/jbasko/pytest-random-order.svg?branch=master
11
9
:target: https://travis-ci.org/jbasko/pytest-random-order
12
10
13
- pytest-random-order is a plugin for `pytest <http://pytest.org >`_ that randomises the order in which
14
- tests are run to reveal unwanted coupling between tests. The plugin allows user to control the level
15
- of randomness they want to introduce and to disable reordering on subsets of tests.
16
- Tests can be rerun in a specific order by passing a seed value reported in a previous test run.
11
+ **pytest-random-order ** is a `pytest <http://pytest.org >`_ plugin that randomises the order of tests.
12
+ This can be useful to detect a test that passes just because it happens to run after an unrelated test that
13
+ leaves the system in a *favourable * state.
17
14
15
+ The plugin allows user to control the level of randomness they want to introduce and to disable
16
+ reordering on subsets of tests. Tests can be rerun in a specific order by passing a seed value reported
17
+ in a previous test run.
18
18
19
+ -----------
19
20
Quick Start
20
21
-----------
21
22
23
+ Installation:
24
+
22
25
::
23
26
24
27
$ pip install pytest-random-order
25
28
26
- The plugin **is enabled by default **. To randomise the order of tests within modules and shuffle the order of
27
- test modules (which is the default behaviour of the plugin), just run pytest as always:
29
+ From v1.0.0 onwards, this plugin no longer randomises tests by default. To enable randomisation, you have to run
30
+ pytest in one of the following ways:
31
+
32
+ ::
33
+
34
+ pytest --random-order
35
+ pytest --random-order-bucket=<bucket_type>
36
+ pytest --random-order-seed=<seed>
37
+
38
+ If you want to always randomise the order of tests, configure pytest. There are many ways to do it,
39
+ my favourite one is to add ``addopts = --random-order `` in your project-specific configuration file
40
+ under the pytest options (usually ``[pytest] `` or ``[tool:pytest] `` section).
41
+
42
+ Alternatively, you can set environment variable ``PYTEST_ADDOPTS ``:
43
+
44
+ ::
45
+
46
+ export PYTEST_ADDOPTS="--random-order"
47
+
48
+
49
+ To randomise the order of tests within modules and shuffle the order of
50
+ test modules (which is the default behaviour of the plugin), run pytest as follows:
28
51
29
52
::
30
53
31
- $ pytest -v
54
+ $ pytest --random-order
32
55
33
- To change the level of randomness allowed , run pytest with ``--random-order-bucket=<bucket-type> `` option
56
+ To change the scope of re-ordering , run pytest with ``--random-order-bucket=<bucket-type> `` option
34
57
where ``<bucket-type> `` can be ``class ``, ``module ``, ``package ``, ``global ``, or ``none ``:
35
58
36
59
::
@@ -47,28 +70,46 @@ To rerun tests in a particular order:
47
70
48
71
::
49
72
50
- $ pytest -v --random-order-seed=<value-reported-in-previous-run >
73
+ $ pytest -v --random-order-seed=<seed >
51
74
75
+ All runs in which the randomisation is enabled report seed so if you encounter a specific ordering of tests
76
+ that causes problems you can look up the value in the test report and repeat the run with the above command.
52
77
78
+ ::
79
+
80
+ platform darwin -- Python 3.5.6, pytest-3.9.1, py-1.7.0, pluggy-0.8.0
81
+ Using --random-order-bucket=module
82
+ Using --random-order-seed=383013
83
+
84
+ ------
53
85
Design
54
86
------
55
87
56
- pytest-random-order plugin groups tests in buckets, shuffles them within buckets and then shuffles the buckets.
88
+ The plugin groups tests in buckets, shuffles them within buckets and then shuffles the buckets.
57
89
58
- You can choose from four types of buckets:
90
+ You can choose from a few types of buckets:
59
91
60
92
class
61
93
Tests will be shuffled within a class and classes will be shuffled,
62
94
but tests from one class will never have tests from other classes or modules run in-between them.
63
95
64
96
module
65
- Same as above at module level. **This is the default setting **.
97
+ Same as above at module level. This is the setting applied if you run pytest with just ``--random-order `` flag
98
+ or ``--random-order-seed=<seed> ``.
66
99
67
100
package
68
101
Same as above at package level. Note that modules (and hence tests inside those modules) that
69
102
belong to package ``x.y.z `` do not belong to package ``x.y ``, so they will fall in different buckets
70
103
when randomising with ``package `` bucket type.
71
104
105
+ parent
106
+ If you are using custom test items which don't belong to any module, you can use this to
107
+ limit reordering of test items to within the ``parent `` to which they belong. For normal test
108
+ functions the parent is the module in which they are declared.
109
+
110
+ grandparent
111
+ Similar to *parent * above, but use the parent of the parent of the test item as the bucket key instead.
112
+
72
113
global
73
114
All tests fall in the same bucket, full randomness, tests probably take longer to run.
74
115
@@ -86,12 +127,13 @@ As you can see, all C tests are executed "next" to each other and so are tests i
86
127
Tests from any bucket X are guaranteed to not be interspersed with tests from another bucket Y.
87
128
For example, if you choose bucket type ``module `` then bucket X contains all tests that are in this module.
88
129
89
- By default, your tests will be randomised at ``module `` level which means that
130
+ By default, when randomisation is enabled, your tests will be randomised at ``module `` level which means that
90
131
tests within a single module X will be executed in no particular order, but tests from
91
132
other modules will not be mixed in between tests of module X.
92
133
93
134
The randomised reordering can be disabled per module or per class irrespective of the chosen bucket type.
94
135
136
+ --------------
95
137
Usage and Tips
96
138
--------------
97
139
@@ -171,11 +213,34 @@ pass undeservedly, you can disable it:
171
213
172
214
::
173
215
174
- $ pytest -p no:random-order -v
216
+ $ pytest -p no:random_order
175
217
176
- To disable just the shuffling, but let the plugin exist:
218
+ Note that randomisation is disabled by default. By passing ``-p no:random_order `` you are stopping the plugin
219
+ from being registered so its hooks won't be registered and its command line options won't appear in ``--help ``.
220
+
221
+ --------------
222
+ Changelog
223
+ --------------
224
+
225
+ v1.0.0 (2018-10-20)
226
+ +++++++++++++++++++
227
+
228
+ * Plugin no longer alters the test order by default. You will have to either 1) pass ``--random-order ``,
229
+ or ``--random-order-bucket=<bucket> ``, or ``--random-order-seed=<seed> ``, or
230
+ 2) edit your pytest configuration file and add one of these options
231
+ there under ``addopts ``, or 3) specify these flags in environment variable ``PYTEST_ADDOPTS ``.
232
+ * Python 3.5+ is required. If you want to use this plugin with Python 2.7, use v0.8.0 which is stable and fine
233
+ if you are happy with it randomising the test order by default.
234
+ * The name under which the plugin registers itself is changed from ``random-order `` (hyphen) to ``random_order ``
235
+ (underscore). This addresses the issue of consistency when disabling or enabling this plugin via the standard
236
+ ``-p `` flag. Previously, the plugin could be disabled by passing ``-p no:random-order `` yet re-enabled
237
+ only by passing ``-p pytest_random_order.plugin ``. Now they are ``-p no:random_order ``
238
+ to disable and ``-p random_order.plugin `` to enable (The ``.plugin `` bit, I think, is required because
239
+ pytest probably thinks it's an unrelated thing to ``random_order `` and import it, yet without it it's the
240
+ same thing so doesn't import it).
177
241
178
- ::
179
242
180
- $ pytest --random-order-bucket=none
243
+ v0.8.0
244
+ ++++++
181
245
246
+ * pytest cache plugin's ``--failed-first `` works now.
0 commit comments