Skip to content

Commit 4b2a8fe

Browse files
authored
#23 - Allow disabling shuffling with --random-order-bucket=none (#24)
1 parent 095c05c commit 4b2a8fe

File tree

5 files changed

+26
-10
lines changed

5 files changed

+26
-10
lines changed

docs/index.rst

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ test modules (which is the default behaviour of the plugin), just run pytest as
2626
$ pytest -v
2727

2828
To change the level of randomness allowed, run pytest with ``--random-order-bucket=<bucket-type>`` option
29-
where ``<bucket-type>`` can be ``class``, ``module``, ``package``, or ``global``:
29+
where ``<bucket-type>`` can be ``class``, ``module``, ``package``, ``global``, or ``none``:
3030

3131
::
3232

@@ -67,6 +67,9 @@ package
6767
global
6868
All tests fall in the same bucket, full randomness, tests probably take longer to run.
6969

70+
none
71+
Disable shuffling.
72+
7073
If you have three buckets of tests ``A``, ``B``, and ``C`` with three tests ``1`` and ``2``, and ``3`` in each of them,
7174
then one of many potential orderings that non-global randomisation can produce could be:
7275

@@ -148,8 +151,8 @@ You can now use the ``--random-order-seed=...`` bit as an argument to the next r
148151
$ pytest -v --random-order-seed=24775
149152

150153

151-
Disable the Plugin
152-
++++++++++++++++++
154+
Disable Randomisation or the Plugin
155+
+++++++++++++++++++++++++++++++++++
153156

154157
If the plugin misbehaves or you just want to assure yourself that it is not the plugin making your tests fail or
155158
pass undeservedly, you can disable it:
@@ -158,3 +161,9 @@ pass undeservedly, you can disable it:
158161

159162
$ pytest -p no:random-order -v
160163

164+
To disable just the shuffling, but let the plugin exist:
165+
166+
::
167+
168+
$ pytest --random-order-bucket=none
169+

pytest_random_order/plugin.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def pytest_addoption(parser):
1212
action='store',
1313
dest='random_order_bucket',
1414
default='module',
15-
choices=('global', 'package', 'module', 'class'),
15+
choices=('global', 'package', 'module', 'class', 'none'),
1616
help='Limit reordering of test items across units of code',
1717
)
1818
group.addoption(
@@ -55,7 +55,8 @@ def pytest_collection_modifyitems(session, config, items):
5555
try:
5656
seed = getattr(config, 'random_order_seed', None)
5757
bucket_type = config.getoption('random_order_bucket')
58-
_shuffle_items(items, bucket_key=_random_order_item_keys[bucket_type], disable=_disable, seed=seed)
58+
if bucket_type != 'none':
59+
_shuffle_items(items, bucket_key=_random_order_item_keys[bucket_type], disable=_disable, seed=seed)
5960

6061
except Exception as e:
6162
# See the finally block -- we only fail if we have lost user's tests.

tests/test_actual_test_runs.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,14 @@ def inspect_attr(this_call, prev_call, attr_name):
136136
assert num_modules <= num_module_switches <= num_modules + 1
137137

138138

139-
@pytest.mark.parametrize('bucket', ['class', 'module', 'package', 'global'])
140-
def test_it_works_with_actual_tests(tmp_tree_of_tests, get_test_calls, bucket):
139+
@pytest.mark.parametrize('bucket,min_sequences,max_sequences', [
140+
('class', 2, 5),
141+
('module', 2, 5),
142+
('package', 2, 5),
143+
('global', 2, 5),
144+
('none', 1, 1),
145+
])
146+
def test_it_works_with_actual_tests(tmp_tree_of_tests, get_test_calls, bucket, min_sequences, max_sequences):
141147
sequences = set()
142148

143149
for x in range(5):
@@ -148,7 +154,7 @@ def test_it_works_with_actual_tests(tmp_tree_of_tests, get_test_calls, bucket):
148154
assert len(seq) == 17
149155
sequences.add(seq)
150156

151-
assert 1 < len(sequences) <= 5
157+
assert min_sequences <= len(sequences) <= max_sequences
152158

153159

154160
def test_random_order_seed_is_respected(testdir, twenty_tests, get_test_calls):

tests/test_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ def test_help_message(testdir):
44
)
55
result.stdout.fnmatch_lines([
66
'random-order:',
7-
'*--random-order-bucket={global,package,module,class}*',
7+
'*--random-order-bucket={global,package,module,class,none}*',
88
'*--random-order-seed=*',
99
])
1010

tests/test_plugin_failure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_faulty_shuffle_that_preserves_items_does_not_fail_test_run(monkeypatch,
3535
result = simple_testdir.runpytest()
3636
result.assert_outcomes(passed=2)
3737
result.stdout.fnmatch_lines("""
38-
*W0 None pytest-random-order plugin has failed with ValueError*
38+
*pytest-random-order plugin has failed with ValueError*
3939
""")
4040

4141

0 commit comments

Comments
 (0)