Skip to content

Commit 7e13a71

Browse files
authored
Merge pull request #8 from jbasko/dev
Use the suggested pytest way of marking tests
2 parents d3b55b1 + 6a0158c commit 7e13a71

File tree

4 files changed

+54
-23
lines changed

4 files changed

+54
-23
lines changed

README.rst

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,27 @@ Advanced Options
108108
Disable Shuffling In a Module
109109
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110110

111-
You can disable shuffling of tests within a single module by placing a pytest marker in the module:
111+
You can disable shuffling of tests within a single module or class by marking the module or class
112+
with ``random_order`` marker and passing ``disabled=True`` to it:
112113

113114
::
114115

115-
pytest.mark.random_order_disabled = True
116+
pytestmark = pytest.mark.random_order(disabled=True)
116117

117118
def test_number_one():
118-
pass
119+
assert True
119120

120121
def test_number_two():
121-
pass
122+
assert True
123+
124+
::
125+
126+
class MyTest(TestCase):
127+
pytestmark = pytest.mark.random_order(disabled=True)
128+
129+
def test_number_one(self):
130+
self.assertTrue(True)
131+
122132

123133
No matter what will be the bucket type for the test run, ``test_number_one`` will always run
124134
before ``test_number_two``.

pytest_random_order/shuffler.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22

33
import collections
4-
import operator
54
import random
65

76

@@ -97,19 +96,12 @@ def _get_set_of_item_ids(items):
9796
return s
9897

9998

100-
_is_random_order_disabled = operator.attrgetter('pytest.mark.random_order_disabled')
101-
102-
10399
def _disable(item):
104-
try:
105-
# In actual test runs, this is returned as a truthy instance of MarkDecorator even when you don't have
106-
# set the marker. This is a hack.
107-
is_disabled = _is_random_order_disabled(item.module)
108-
if is_disabled and is_disabled is True:
109-
# It is not enough to return just True because in case the shuffling
110-
# is disabled on module, we must preserve the module unchanged
111-
# even when the bucket type for this test run is say package or global.
112-
return item.module.__name__
113-
except AttributeError:
114-
pass
100+
marker = item.get_marker('random_order')
101+
if marker:
102+
is_disabled = marker.kwargs.get('disabled', False)
103+
if is_disabled:
104+
# A test item can only be disabled in its parent context -- where it is part of some order.
105+
# We use parent name as the key so that all children of the same parent get the same disabled key.
106+
return item.parent.name
115107
return False

setup.py

Lines changed: 1 addition & 1 deletion
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.4.3',
16+
version='0.5.0',
1717
author='Jazeps Basko',
1818
author_email='[email protected]',
1919
maintainer='Jazeps Basko',

tests/test_markers.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,19 @@ def twenty_tests():
99
return ''.join(code)
1010

1111

12-
@pytest.mark.parametrize('disabled', [True])
13-
def test_pytest_mark_random_order_disabled(testdir, twenty_tests, get_test_calls, disabled):
12+
@pytest.fixture
13+
def twenty_cls_tests():
14+
code = []
15+
for i in range(20):
16+
code.append('\tdef test_b{}(self): self.assertTrue\n'.format(str(i).zfill(2)))
17+
return ''.join(code)
18+
19+
20+
@pytest.mark.parametrize('disabled', [True, False])
21+
def test_marker_disables_random_order_in_module(testdir, twenty_tests, get_test_calls, disabled):
1422
testdir.makepyfile(
1523
'import pytest\n' +
16-
'pytest.mark.random_order_disabled = {}\n'.format(disabled) +
24+
('pytestmark = pytest.mark.random_order(disabled={})\n'.format(disabled)) +
1725
twenty_tests
1826
)
1927

@@ -26,3 +34,24 @@ def test_pytest_mark_random_order_disabled(testdir, twenty_tests, get_test_calls
2634
assert names == sorted_names
2735
else:
2836
assert names != sorted_names
37+
38+
39+
@pytest.mark.parametrize('disabled', [True, False])
40+
def test_marker_disables_random_order_in_class(testdir, twenty_cls_tests, get_test_calls, disabled):
41+
testdir.makepyfile(
42+
'import pytest\n\n' +
43+
'from unittest import TestCase\n\n' +
44+
'class MyTest(TestCase):\n' +
45+
'\tpytestmark = pytest.mark.random_order(disabled={})\n'.format(disabled) +
46+
twenty_cls_tests + '\n'
47+
)
48+
49+
result = testdir.runpytest('--random-order-bucket=module', '-v')
50+
result.assert_outcomes(passed=20)
51+
names = [c.name for c in get_test_calls(testdir.runpytest())]
52+
sorted_names = sorted(list(names))
53+
54+
if disabled:
55+
assert names == sorted_names
56+
else:
57+
assert names != sorted_names

0 commit comments

Comments
 (0)