Skip to content

Commit aa432f0

Browse files
committed
Merge pull request #13 from mattias-lundell/develop
Added ability to mark whole classes with order
2 parents 639dd02 + 23a9bd9 commit aa432f0

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

pytest_ordering/__init__.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import re
2-
31
from ._version import __version__
42

3+
import re
4+
from collections import defaultdict
55

66
replacements = {
77
'first': 0,
@@ -24,8 +24,8 @@
2424

2525

2626
def pytest_configure(config):
27-
"""Register the "run" marker.
28-
"""
27+
"""Register the "run" marker."""
28+
2929
config_line = (
3030
'run: specify ordering information for when tests should run '
3131
'in relation to one another. Provided by pytest-ordering. '
@@ -41,19 +41,22 @@ def pytest_collection_modifyitems(session, config, items):
4141
def orderable(marker_name, marker_info):
4242
if not hasattr(marker_info, 'kwargs'):
4343
return False
44-
if 'order' in marker_info.kwargs:
44+
elif 'order' in marker_info.kwargs:
4545
return True
46-
match = re.match('^order(\d+)$', marker_name)
47-
return bool(match) or marker_name in replacements
46+
else:
47+
match = re.match('^order(\d+)$', marker_name)
48+
return bool(match) or marker_name in replacements
4849

4950

5051
def get_index(marker_name, marker_info):
5152
match = re.match('^order(\d+)$', marker_name)
53+
5254
if match:
5355
return int(match.group(1)) - 1
54-
if marker_name in replacements:
56+
elif marker_name in replacements:
5557
return replacements[marker_name]
56-
return marker_info.kwargs['order']
58+
else:
59+
return marker_info.kwargs['order']
5760

5861

5962
def split(dictionary):
@@ -67,30 +70,37 @@ def split(dictionary):
6770

6871

6972
def _order_tests(tests):
70-
ordered_tests = {}
73+
ordered_tests = defaultdict(list)
7174
remaining_tests = []
75+
7276
for test in tests:
7377
# There has got to be an API for this. :-/
7478
markers = test.keywords.__dict__['_markers']
7579
orderable_markers = [(k, v) for (k, v) in markers.items()
7680
if orderable(k, v)]
7781
if len(orderable_markers) == 1:
7882
marker_name, marker_info = orderable_markers[0]
79-
ordered_tests[get_index(marker_name, marker_info)] = test
83+
ordered_tests[get_index(marker_name, marker_info)].append(test)
8084
else:
8185
remaining_tests.append(test)
86+
8287
from_beginning, from_end = split(ordered_tests)
8388
remaining_iter = iter(remaining_tests)
89+
8490
for i in range(max(from_beginning or [-1]) + 1):
8591
if i in from_beginning:
86-
yield from_beginning[i]
92+
for test in from_beginning[i]:
93+
yield test
8794
else:
8895
yield next(remaining_iter)
96+
8997
# TODO TODO TODO
9098
for i in range(min(from_end or [0]), 0):
9199
if i in from_end:
92-
yield from_end[i]
100+
for test in from_end[i]:
101+
yield test
93102
else:
94103
yield next(remaining_iter)
104+
95105
for test in remaining_iter:
96106
yield test

tests/marked_classes.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pytest
2+
3+
ordering = 'fdeabc'
4+
5+
6+
@pytest.mark.order3
7+
class TestA:
8+
def test_a():
9+
pass
10+
11+
def test_b():
12+
pass
13+
14+
def test_c():
15+
pass
16+
17+
18+
@pytest.mark.order2
19+
class TestB:
20+
def test_d():
21+
pass
22+
23+
def test_e():
24+
pass
25+
26+
27+
@pytest.mark.order1
28+
def test_f():
29+
pass

tests/test_ordering.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import pytest
44

55
import pytest_ordering
6-
from . import numbers, words, words_backwards, grouping
6+
from . import numbers, words, words_backwards, grouping, marked_classes
77

88

99
@pytest.mark.parametrize('module', [
10-
numbers, words, words_backwards, grouping,
10+
numbers, words, words_backwards, grouping, marked_classes
1111
])
1212
def test_ordered_tests(module, testdir):
1313
items = testdir.getitems(module)

0 commit comments

Comments
 (0)