Skip to content

Commit 4699ba8

Browse files
authored
Merge pull request #26 from Bengreen/develop
Update to enable non-contiguous ordering
2 parents 94e63ff + 1b7e7df commit 4699ba8

File tree

3 files changed

+95
-17
lines changed

3 files changed

+95
-17
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
language: python
22
python: 2.7
33
env:
4-
#- TOX_ENV=py34
54
- TOX_ENV=py33
6-
- TOX_ENV=py32
75
- TOX_ENV=py27
86
- TOX_ENV=py26
97
install:

pytest_ordering/__init__.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,15 @@ def pytest_collection_modifyitems(session, config, items):
5656
grouped_items.setdefault(order, []).append(item)
5757

5858
if grouped_items:
59-
unordered_items = grouped_items.pop(None, None)
60-
6159
sorted_items = []
62-
prev_key = 0
63-
64-
for key, ordered_items in grouped_items.items():
65-
66-
if unordered_items and prev_key >= 0 and key < 0:
67-
68-
sorted_items.extend(unordered_items)
69-
unordered_items = None
7060

71-
prev_key = key
61+
unordered_items = [grouped_items.pop(None, [])]
7262

73-
sorted_items.extend(ordered_items)
63+
start_list = sorted((i for i in grouped_items.items() if i[0] >= 0), key=lambda x: x[0])
64+
end_list = sorted((i for i in grouped_items.items() if i[0] < 0), key=lambda x: x[0])
7465

75-
if unordered_items:
76-
sorted_items.extend(unordered_items)
66+
sorted_items.extend([i[1] for i in start_list])
67+
sorted_items.extend(unordered_items)
68+
sorted_items.extend([i[1] for i in end_list])
7769

78-
items[:] = sorted_items
70+
items[:] = [item for sublist in sorted_items for item in sublist]

tests/test_ordering.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,94 @@ def test_3(): pass
8989
assert item_names_for(tests_content) == ['test_3', 'test_2', 'test_1']
9090

9191

92+
def test_non_contiguous_positive(item_names_for):
93+
tests_content = """
94+
import pytest
95+
96+
@pytest.mark.run(order=10)
97+
def test_1(): pass
98+
99+
@pytest.mark.run(order=20)
100+
def test_2(): pass
101+
102+
@pytest.mark.run(order=5)
103+
def test_3(): pass
104+
"""
105+
106+
assert item_names_for(tests_content) == ['test_3', 'test_1', 'test_2']
107+
108+
109+
def test_non_contiguous_negative(item_names_for):
110+
tests_content = """
111+
import pytest
112+
113+
@pytest.mark.run(order=-10)
114+
def test_1(): pass
115+
116+
@pytest.mark.run(order=-20)
117+
def test_2(): pass
118+
119+
@pytest.mark.run(order=-5)
120+
def test_3(): pass
121+
"""
122+
123+
assert item_names_for(tests_content) == ['test_2', 'test_1', 'test_3']
124+
125+
126+
def test_non_contiguous_inc_zero(item_names_for):
127+
tests_content = """
128+
import pytest
129+
130+
@pytest.mark.run(order=10)
131+
def test_1(): pass
132+
133+
@pytest.mark.run(order=20)
134+
def test_2(): pass
135+
136+
@pytest.mark.run(order=5)
137+
def test_3(): pass
138+
139+
@pytest.mark.run(order=-10)
140+
def test_4(): pass
141+
142+
@pytest.mark.run(order=-20)
143+
def test_5(): pass
144+
145+
@pytest.mark.run(order=-5)
146+
def test_6(): pass
147+
148+
@pytest.mark.run(order=0)
149+
def test_7(): pass
150+
"""
151+
152+
assert item_names_for(tests_content) == ['test_7', 'test_3', 'test_1', 'test_2', 'test_5', 'test_4', 'test_6']
153+
154+
155+
def test_non_contiguous_inc_none(item_names_for):
156+
tests_content = """
157+
import pytest
158+
159+
@pytest.mark.run(order=5)
160+
def test_1(): pass
161+
162+
@pytest.mark.run(order=0)
163+
def test_2(): pass
164+
165+
@pytest.mark.run(order=1)
166+
def test_3(): pass
167+
168+
@pytest.mark.run(order=-1)
169+
def test_4(): pass
170+
171+
@pytest.mark.run(order=-5)
172+
def test_5(): pass
173+
174+
def test_6(): pass
175+
"""
176+
177+
assert item_names_for(tests_content) == ['test_2', 'test_3', 'test_1', 'test_6', 'test_5', 'test_4']
178+
179+
92180
def test_first_mark_class(item_names_for):
93181
tests_content = """
94182
import pytest

0 commit comments

Comments
 (0)