11pytest-random-order
22===================================
33
4- **pytest ** plugin to randomise the order of tests within module, package, or globally.
4+ This is a **pytest ** plugin to randomise the order in which tests are run with a little bit of control
5+ over how much randomness one allows.
56
6- This plugin allows you to control the level at which the order of tests is randomised
7- through ``--random-order-mode `` command line option.
7+ It is a good idea to randomise the order in which your tests run
8+ because a test running early as part of a larger suite of tests may have left
9+ the system under test in a particularly fortunate state for a subsequent test to pass.
10+
11+ How Random
12+ __________
13+
14+ **pytest-random-order ** groups tests in buckets, shuffles them within buckets and then shuffles the buckets.
15+
16+ User can choose among four types of buckets to use:
17+
18+ * ``class ``
19+ * ``module `` -- **this is the default setting **
20+ * ``package ``
21+ * ``global `` -- all tests fall in the same bucket, full randomness, tests probably take longer to run
22+
23+ If you have three buckets of tests ``A ``, ``B ``, and ``C `` with three tests ``1 `` and ``2 ``, and ``3 `` in each of them,
24+ then here are just two of many potential orderings that non-global randomisation can produce:
25+
26+ ::
27+
28+ C2 C1 C3 A3 A1 A2 B3 B2 B1
29+
30+ A2 A1 A3 C1 C2 C3 B2 B1 B3
31+
32+ As you can see, all C tests are executed "next" to each other and so are tests in buckets A and B.
33+ Tests from any bucket X are guaranteed to not be interspersed with tests from another bucket Y.
34+ For example, if you choose bucket type ``module `` then bucket X contains all tests that are in this module.
35+
36+ Note that modules (and hence tests inside those modules) that belong to package ``x.y `` do not belong
37+ to package ``x.y.z ``, so they will fall in different buckets when randomising with ``package `` bucket type.
838
939By default, your tests will be randomised at ``module `` level which means that
1040tests within a single module X will be executed in no particular order, but tests from
1141other modules will not be mixed in between tests of module X.
1242
13- Similarly, you can randomise the order of tests at ``package `` and ``global `` levels.
14-
1543----
1644
1745Installation
@@ -25,21 +53,38 @@ Installation
2553Usage
2654-----
2755
56+ The plugin is enabled by default. To randomise the order of tests within modules, just run pytest as always:
57+
2858::
2959
3060 $ pytest -v
3161
32- $ pytest -v --random-order-mode=global
62+ It is best to start with smallest bucket type (``class `` or ``module `` depending on whether you have class-based tests),
63+ and switch to a larger bucket type when you are sure your tests handle that.
64+
65+ If your tests rely on fixtures that are module or session-scoped, more randomised order of tests will mean slower tests.
66+ You probably don't want to randomise at ``global `` or ``package `` level while you are developing and need a quick confirmation
67+ that nothing big is broken.
68+
69+ ::
70+
71+ $ pytest -v --random-order-bucket=class
3372
34- $ pytest -v --random-order-mode=package
73+ $ pytest -v --random-order-bucket=module
3574
36- $ pytest -v --random-order-mode=module
75+ $ pytest -v --random-order-bucket=package
3776
38- $ pytest -v --random-order-mode=class
77+ $ pytest -v --random-order-bucket=global
78+
79+ If the plugin misbehaves or you just want to assure yourself that it is not the plugin making your tests fail or
80+ pass undeservedly, you can disable it:
81+
82+ ::
83+
84+ $ pytest -p no:random-order -v
3985
4086
4187License
4288-------
4389
4490Distributed under the terms of the MIT license, "pytest-random-order" is free and open source software
45-
0 commit comments