|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 |
|
3 | | -import random |
4 | | -import sys |
5 | | -import traceback |
6 | | - |
7 | 3 | 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 |
38 | 5 |
|
39 | 6 |
|
40 | 7 | def _shuffle_items(items, key=None, disable=None, preserve_bucket_order=False): |
@@ -120,28 +87,3 @@ def _disable(item): |
120 | 87 | except AttributeError: |
121 | 88 | pass |
122 | 89 | 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) |
0 commit comments