Skip to content

Commit e511194

Browse files
committed
Refactor plugin module into a package
1 parent b8819c5 commit e511194

File tree

7 files changed

+68
-67
lines changed

7 files changed

+68
-67
lines changed

pytest_random_order/__init__.py

Whitespace-only changes.

pytest_random_order/plugin.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import sys
2+
import traceback
3+
4+
from pytest_random_order.shuffler import _get_set_of_item_ids, _shuffle_items, _disable
5+
6+
7+
def pytest_addoption(parser):
8+
group = parser.getgroup('random-order')
9+
group.addoption(
10+
'--random-order-bucket',
11+
action='store',
12+
dest='random_order_bucket',
13+
default='module',
14+
choices=('global', 'package', 'module', 'class'),
15+
help='Limit reordering of test items across units of code',
16+
)
17+
18+
19+
def pytest_report_header(config):
20+
out = None
21+
22+
if config.getoption('random_order_bucket'):
23+
bucket = config.getoption('random_order_bucket')
24+
out = "Using --random-order-bucket={0}".format(bucket)
25+
26+
return out
27+
28+
29+
def pytest_collection_modifyitems(session, config, items):
30+
failure = None
31+
32+
item_ids = _get_set_of_item_ids(items)
33+
34+
try:
35+
bucket_type = config.getoption('random_order_bucket')
36+
_shuffle_items(items, key=_random_order_item_keys[bucket_type], disable=_disable)
37+
38+
except Exception as e:
39+
# See the finally block -- we only fail if we have lost user's tests.
40+
_, _, exc_tb = sys.exc_info()
41+
failure = 'pytest-random-order plugin has failed with {!r}:\n{}'.format(
42+
e, ''.join(traceback.format_tb(exc_tb, 10))
43+
)
44+
config.warn(0, failure, None)
45+
46+
finally:
47+
# Fail only if we have lost user's tests
48+
if item_ids != _get_set_of_item_ids(items):
49+
if not failure:
50+
failure = 'pytest-random-order plugin has failed miserably'
51+
raise RuntimeError(failure)
52+
53+
54+
_random_order_item_keys = {
55+
'global': lambda x: None,
56+
'package': lambda x: x.module.__package__,
57+
'module': lambda x: x.module.__name__,
58+
'class': lambda x: (x.module.__name__, x.cls.__name__) if x.cls else x.module.__name__,
59+
}

pytest_random_order.py renamed to pytest_random_order/shuffler.py

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3-
import random
4-
import sys
5-
import traceback
6-
73
import operator
8-
9-
10-
def pytest_addoption(parser):
11-
group = parser.getgroup('random-order')
12-
group.addoption(
13-
'--random-order-bucket',
14-
action='store',
15-
dest='random_order_bucket',
16-
default='module',
17-
choices=('global', 'package', 'module', 'class'),
18-
help='Limit reordering of test items across units of code',
19-
)
20-
21-
22-
def pytest_report_header(config):
23-
out = None
24-
25-
if config.getoption('random_order_bucket'):
26-
bucket = config.getoption('random_order_bucket')
27-
out = "Using --random-order-bucket={0}".format(bucket)
28-
29-
return out
30-
31-
32-
_random_order_item_keys = {
33-
'global': lambda x: None,
34-
'package': lambda x: x.module.__package__,
35-
'module': lambda x: x.module.__name__,
36-
'class': lambda x: (x.module.__name__, x.cls.__name__) if x.cls else x.module.__name__,
37-
}
4+
import random
385

396

407
def _shuffle_items(items, key=None, disable=None, preserve_bucket_order=False):
@@ -120,28 +87,3 @@ def _disable(item):
12087
except AttributeError:
12188
pass
12289
return False
123-
124-
125-
def pytest_collection_modifyitems(session, config, items):
126-
failure = None
127-
128-
item_ids = _get_set_of_item_ids(items)
129-
130-
try:
131-
bucket_type = config.getoption('random_order_bucket')
132-
_shuffle_items(items, key=_random_order_item_keys[bucket_type], disable=_disable)
133-
134-
except Exception as e:
135-
# See the finally block -- we only fail if we have lost user's tests.
136-
_, _, exc_tb = sys.exc_info()
137-
failure = 'pytest-random-order plugin has failed with {!r}:\n{}'.format(
138-
e, ''.join(traceback.format_tb(exc_tb, 10))
139-
)
140-
config.warn(0, failure, None)
141-
142-
finally:
143-
# Fail only if we have lost user's tests
144-
if item_ids != _get_set_of_item_ids(items):
145-
if not failure:
146-
failure = 'pytest-random-order plugin has failed miserably'
147-
raise RuntimeError(failure)

setup.py

Lines changed: 3 additions & 3 deletions
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.3.1',
16+
version='0.4.0',
1717
author='Jazeps Basko',
1818
author_email='[email protected]',
1919
maintainer='Jazeps Basko',
@@ -22,7 +22,7 @@ def read(fname):
2222
url='https://github.com/jbasko/pytest-random-order',
2323
description='Randomise the order in which pytest tests are run with some control over the randomness',
2424
long_description=read('README.rst'),
25-
py_modules=['pytest_random_order'],
25+
py_modules=['pytest_random_order.plugin', 'pytest_random_order.shuffler'],
2626
install_requires=['pytest>=2.9.2'],
2727
classifiers=[
2828
'Development Status :: 4 - Beta',
@@ -38,7 +38,7 @@ def read(fname):
3838
],
3939
entry_points={
4040
'pytest11': [
41-
'random-order = pytest_random_order',
41+
'random-order = pytest_random_order.plugin',
4242
],
4343
},
4444
)

tests/test_plugin_failure.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_a2():
3030

3131

3232
def test_faulty_shuffle_that_preserves_items_does_not_fail_test_run(monkeypatch, simple_testdir):
33-
monkeypatch.setattr('pytest_random_order._shuffle_items', acceptably_failing_shuffle_items)
33+
monkeypatch.setattr('pytest_random_order.plugin._shuffle_items', acceptably_failing_shuffle_items)
3434

3535
result = simple_testdir.runpytest()
3636
result.assert_outcomes(passed=2)
@@ -40,7 +40,7 @@ def test_faulty_shuffle_that_preserves_items_does_not_fail_test_run(monkeypatch,
4040

4141

4242
def test_faulty_shuffle_that_loses_items_fails_test_run(monkeypatch, simple_testdir):
43-
monkeypatch.setattr('pytest_random_order._shuffle_items', critically_failing_shuffle_items)
43+
monkeypatch.setattr('pytest_random_order.plugin._shuffle_items', critically_failing_shuffle_items)
4444
result = simple_testdir.runpytest()
4545
result.assert_outcomes(passed=0, failed=0, skipped=0)
4646
result.stdout.fnmatch_lines("""
@@ -49,7 +49,7 @@ def test_faulty_shuffle_that_loses_items_fails_test_run(monkeypatch, simple_test
4949

5050

5151
def test_seemingly_ok_shuffle_that_loses_items_fails_test_run(monkeypatch, simple_testdir):
52-
monkeypatch.setattr('pytest_random_order._shuffle_items', critically_not_failing_shuffle_items)
52+
monkeypatch.setattr('pytest_random_order.plugin._shuffle_items', critically_not_failing_shuffle_items)
5353
result = simple_testdir.runpytest()
5454
result.assert_outcomes(passed=0, failed=0, skipped=0)
5555
result.stdout.fnmatch_lines("""

tests/test_shuffle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

33

4-
from pytest_random_order import _shuffle_items
4+
from pytest_random_order.shuffler import _shuffle_items
55

66

77
def identity_key(x):

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ commands = py.test {posargs:tests}
99
[testenv:flake8]
1010
skip_install = true
1111
deps = flake8
12-
commands = flake8 pytest_random_order.py setup.py tests
12+
commands = flake8 pytest_random_order setup.py tests

0 commit comments

Comments
 (0)