Skip to content

Commit 641fe30

Browse files
authored
Allow bucketing of test items by their direct parent or grand-parent (#27)
* Closes #26 - allow limiting randomisation with custom test collections where items don't have module * Missing dep for Python 2.6
1 parent 66cb677 commit 641fe30

File tree

6 files changed

+18
-4
lines changed

6 files changed

+18
-4
lines changed

docs/index.rst

+10-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Design
5050

5151
pytest-random-order plugin groups tests in buckets, shuffles them within buckets and then shuffles the buckets.
5252

53-
You can choose from four types of buckets:
53+
You can choose from a few types of buckets:
5454

5555
class
5656
Tests will be shuffled within a class and classes will be shuffled,
@@ -64,12 +64,21 @@ package
6464
belong to package ``x.y.z`` do not belong to package ``x.y``, so they will fall in different buckets
6565
when randomising with ``package`` bucket type.
6666

67+
parent
68+
If you are using custom test items which don't belong to any module, you can use this to
69+
limit reordering of test items to within the ``parent`` to which they belong. For normal test
70+
functions the parent is the module in which they are declared.
71+
72+
grandparent
73+
Similar to *parent* above, but use the parent of the parent of the test item as the bucket key instead.
74+
6775
global
6876
All tests fall in the same bucket, full randomness, tests probably take longer to run.
6977

7078
none
7179
Disable shuffling.
7280

81+
7382
If you have three buckets of tests ``A``, ``B``, and ``C`` with three tests ``1`` and ``2``, and ``3`` in each of them,
7483
then one of many potential orderings that non-global randomisation can produce could be:
7584

pytest_random_order/plugin.py

+3-1
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', 'none'),
15+
choices=('global', 'package', 'module', 'class', 'parent', 'grandparent', 'none'),
1616
help='Limit reordering of test items across units of code',
1717
)
1818
group.addoption(
@@ -72,4 +72,6 @@ def pytest_collection_modifyitems(session, config, items):
7272
'package': lambda x: x.module.__package__,
7373
'module': lambda x: x.module.__name__,
7474
'class': lambda x: (x.module.__name__, x.cls.__name__) if x.cls else x.module.__name__,
75+
'parent': lambda x: x.parent,
76+
'grandparent': lambda x: x.parent.parent,
7577
}

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def read(fname):
1313

1414
setup(
1515
name='pytest-random-order',
16-
version='0.6.0',
16+
version='0.7.0',
1717
author='Jazeps Basko',
1818
author_email='[email protected]',
1919
maintainer='Jazeps Basko',

tests/test_actual_test_runs.py

+2
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ def inspect_attr(this_call, prev_call, attr_name):
142142
('package', 2, 5),
143143
('global', 2, 5),
144144
('none', 1, 1),
145+
('parent', 1, 5),
146+
('grandparent', 1, 5),
145147
])
146148
def test_it_works_with_actual_tests(tmp_tree_of_tests, get_test_calls, bucket, min_sequences, max_sequences):
147149
sequences = set()

tests/test_cli.py

+1-1
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,none}*',
7+
'*--random-order-bucket={global,package,module,class,parent,grandparent,none}*',
88
'*--random-order-seed=*',
99
])
1010

tox.ini

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ deps =
1313
pytest
1414
pytest-xdist
1515
ordereddict
16+
pytest-xdist
1617

1718
[testenv:flake8]
1819
skip_install = true

0 commit comments

Comments
 (0)