1+ ===================================
12pytest-random-order
23===================================
34
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
86 :target: https://github.com/jbasko/pytest-random-order
97
108.. image :: https://travis-ci.org/jbasko/pytest-random-order.svg?branch=master
119 :target: https://travis-ci.org/jbasko/pytest-random-order
1210
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.
1714
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.
1818
19+ -----------
1920Quick Start
2021-----------
2122
23+ Installation:
24+
2225::
2326
2427 $ pip install pytest-random-order
2528
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:
2851
2952::
3053
31- $ pytest -v
54+ $ pytest --random-order
3255
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
3457where ``<bucket-type> `` can be ``class ``, ``module ``, ``package ``, ``global ``, or ``none ``:
3558
3659::
@@ -47,28 +70,46 @@ To rerun tests in a particular order:
4770
4871::
4972
50- $ pytest -v --random-order-seed=<value-reported-in-previous-run >
73+ $ pytest -v --random-order-seed=<seed >
5174
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.
5277
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+ ------
5385Design
5486------
5587
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.
5789
58- You can choose from four types of buckets:
90+ You can choose from a few types of buckets:
5991
6092class
6193 Tests will be shuffled within a class and classes will be shuffled,
6294 but tests from one class will never have tests from other classes or modules run in-between them.
6395
6496module
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> ``.
6699
67100package
68101 Same as above at package level. Note that modules (and hence tests inside those modules) that
69102 belong to package ``x.y.z `` do not belong to package ``x.y ``, so they will fall in different buckets
70103 when randomising with ``package `` bucket type.
71104
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+
72113global
73114 All tests fall in the same bucket, full randomness, tests probably take longer to run.
74115
@@ -86,12 +127,13 @@ As you can see, all C tests are executed "next" to each other and so are tests i
86127Tests from any bucket X are guaranteed to not be interspersed with tests from another bucket Y.
87128For example, if you choose bucket type ``module `` then bucket X contains all tests that are in this module.
88129
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
90131tests within a single module X will be executed in no particular order, but tests from
91132other modules will not be mixed in between tests of module X.
92133
93134The randomised reordering can be disabled per module or per class irrespective of the chosen bucket type.
94135
136+ --------------
95137Usage and Tips
96138--------------
97139
@@ -171,11 +213,34 @@ pass undeservedly, you can disable it:
171213
172214::
173215
174- $ pytest -p no:random-order -v
216+ $ pytest -p no:random_order
175217
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).
177241
178- ::
179242
180- $ pytest --random-order-bucket=none
243+ v0.8.0
244+ ++++++
181245
246+ * pytest cache plugin's ``--failed-first `` works now.
0 commit comments