1
- import re
2
-
3
1
from ._version import __version__
4
2
3
+ import re
4
+ from collections import defaultdict
5
5
6
6
replacements = {
7
7
'first' : 0 ,
24
24
25
25
26
26
def pytest_configure (config ):
27
- """Register the "run" marker.
28
- """
27
+ """Register the "run" marker."""
28
+
29
29
config_line = (
30
30
'run: specify ordering information for when tests should run '
31
31
'in relation to one another. Provided by pytest-ordering. '
@@ -41,19 +41,22 @@ def pytest_collection_modifyitems(session, config, items):
41
41
def orderable (marker_name , marker_info ):
42
42
if not hasattr (marker_info , 'kwargs' ):
43
43
return False
44
- if 'order' in marker_info .kwargs :
44
+ elif 'order' in marker_info .kwargs :
45
45
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
48
49
49
50
50
51
def get_index (marker_name , marker_info ):
51
52
match = re .match ('^order(\d+)$' , marker_name )
53
+
52
54
if match :
53
55
return int (match .group (1 )) - 1
54
- if marker_name in replacements :
56
+ elif marker_name in replacements :
55
57
return replacements [marker_name ]
56
- return marker_info .kwargs ['order' ]
58
+ else :
59
+ return marker_info .kwargs ['order' ]
57
60
58
61
59
62
def split (dictionary ):
@@ -67,30 +70,37 @@ def split(dictionary):
67
70
68
71
69
72
def _order_tests (tests ):
70
- ordered_tests = {}
73
+ ordered_tests = defaultdict ( list )
71
74
remaining_tests = []
75
+
72
76
for test in tests :
73
77
# There has got to be an API for this. :-/
74
78
markers = test .keywords .__dict__ ['_markers' ]
75
79
orderable_markers = [(k , v ) for (k , v ) in markers .items ()
76
80
if orderable (k , v )]
77
81
if len (orderable_markers ) == 1 :
78
82
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 )
80
84
else :
81
85
remaining_tests .append (test )
86
+
82
87
from_beginning , from_end = split (ordered_tests )
83
88
remaining_iter = iter (remaining_tests )
89
+
84
90
for i in range (max (from_beginning or [- 1 ]) + 1 ):
85
91
if i in from_beginning :
86
- yield from_beginning [i ]
92
+ for test in from_beginning [i ]:
93
+ yield test
87
94
else :
88
95
yield next (remaining_iter )
96
+
89
97
# TODO TODO TODO
90
98
for i in range (min (from_end or [0 ]), 0 ):
91
99
if i in from_end :
92
- yield from_end [i ]
100
+ for test in from_end [i ]:
101
+ yield test
93
102
else :
94
103
yield next (remaining_iter )
104
+
95
105
for test in remaining_iter :
96
106
yield test
0 commit comments