|
26 | 26 |
|
27 | 27 |
|
28 | 28 | def pytest_configure(config):
|
29 |
| - """Register the "run" marker.""" |
| 29 | + """Register the "run" marker and configur the plugin depending on the CLI |
| 30 | + options""" |
30 | 31 |
|
31 | 32 | config_line = (
|
32 | 33 | 'run: specify ordering information for when tests should run '
|
33 | 34 | 'in relation to one another. Provided by pytest-ordering. '
|
34 | 35 | 'See also: http://pytest-ordering.readthedocs.org/'
|
35 | 36 | )
|
36 | 37 | config.addinivalue_line('markers', config_line)
|
| 38 | + if config.getoption('indulgent-ordering'): |
| 39 | + # We need to dynamically add this `tryfirst` decorator to the plugin: |
| 40 | + # only when the CLI option is present should the decorator be added. |
| 41 | + # Thus, we manually run the decorator on the class function and |
| 42 | + # manually replace it. |
| 43 | + OrderingPlugin.pytest_collection_modifyitems = pytest.hookimpl( |
| 44 | + function=OrderingPlugin.pytest_collection_modifyitems, tryfirst=True) |
| 45 | + config.pluginmanager.register(OrderingPlugin(), 'orderingplugin') |
37 | 46 |
|
38 | 47 |
|
39 |
| -def pytest_collection_modifyitems(session, config, items): |
40 |
| - grouped_items = {} |
| 48 | +def pytest_addoption(parser): |
| 49 | + """Set up CLI option for pytest""" |
| 50 | + group = parser.getgroup('ordering') |
| 51 | + group.addoption('--indulgent-ordering', action='store_true', |
| 52 | + dest='indulgent-ordering', help='''Request that the sort \ |
| 53 | +order provided by pytest-ordering be applied before other sorting, allowing the \ |
| 54 | +other sorting to have priority''') |
41 | 55 |
|
42 |
| - for item in items: |
| 56 | +class OrderingPlugin: |
| 57 | + """ |
| 58 | + Plugin implementation |
43 | 59 |
|
44 |
| - for mark_name, order in orders_map.items(): |
45 |
| - mark = item.get_closest_marker(mark_name) |
| 60 | + By putting this in a class, we are able to dynamically register it after |
| 61 | + the CLI is parsed. |
| 62 | + """ |
| 63 | + def pytest_collection_modifyitems(session, config, items): |
| 64 | + grouped_items = {} |
46 | 65 |
|
47 |
| - if mark: |
48 |
| - item.add_marker(pytest.mark.run(order=order)) |
49 |
| - break |
| 66 | + for item in items: |
| 67 | + |
| 68 | + for mark_name, order in orders_map.items(): |
| 69 | + mark = item.get_closest_marker(mark_name) |
50 | 70 |
|
51 |
| - mark = item.get_closest_marker('run') |
| 71 | + if mark: |
| 72 | + item.add_marker(pytest.mark.run(order=order)) |
| 73 | + break |
52 | 74 |
|
53 |
| - if mark: |
54 |
| - order = mark.kwargs.get('order') |
55 |
| - else: |
56 |
| - order = None |
| 75 | + mark = item.get_closest_marker('run') |
| 76 | + |
| 77 | + if mark: |
| 78 | + order = mark.kwargs.get('order') |
| 79 | + else: |
| 80 | + order = None |
57 | 81 |
|
58 |
| - grouped_items.setdefault(order, []).append(item) |
| 82 | + grouped_items.setdefault(order, []).append(item) |
59 | 83 |
|
60 |
| - sorted_items = [] |
61 |
| - unordered_items = [grouped_items.pop(None, [])] |
| 84 | + sorted_items = [] |
| 85 | + unordered_items = [grouped_items.pop(None, [])] |
62 | 86 |
|
63 |
| - start_list = sorted((i for i in grouped_items.items() if i[0] >= 0), |
64 |
| - key=operator.itemgetter(0)) |
65 |
| - end_list = sorted((i for i in grouped_items.items() if i[0] < 0), |
66 |
| - key=operator.itemgetter(0)) |
| 87 | + start_list = sorted((i for i in grouped_items.items() if i[0] >= 0), |
| 88 | + key=operator.itemgetter(0)) |
| 89 | + end_list = sorted((i for i in grouped_items.items() if i[0] < 0), |
| 90 | + key=operator.itemgetter(0)) |
67 | 91 |
|
68 |
| - sorted_items.extend([i[1] for i in start_list]) |
69 |
| - sorted_items.extend(unordered_items) |
70 |
| - sorted_items.extend([i[1] for i in end_list]) |
| 92 | + sorted_items.extend([i[1] for i in start_list]) |
| 93 | + sorted_items.extend(unordered_items) |
| 94 | + sorted_items.extend([i[1] for i in end_list]) |
71 | 95 |
|
72 |
| - items[:] = [item for sublist in sorted_items for item in sublist] |
| 96 | + items[:] = [item for sublist in sorted_items for item in sublist] |
0 commit comments