Skip to content

Commit 2ad69f5

Browse files
authored
Keep multiple order markers for tests that are collected more than once (#277)
1 parent d06e084 commit 2ad69f5

3 files changed

Lines changed: 81 additions & 15 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
- `--order-marker-prefix` uses the complete marker suffix as the order index,
1313
so multi-digit and negative indices work; negative indices can be written
1414
as `neg` (for example `m_neg1` for -1)
15+
- a test with multiple order markers is run once per marker even if it is
16+
collected more than once, e.g. as a method inherited by several test classes;
17+
previously only the first collected instance kept its order markers
1518

1619
## [Version 1.5.0](https://pypi.org/project/pytest-order/1.5.0/) (2026-06-13)
1720
Adds option to abort tests after a failed ordering.

‎src/pytest_order/plugin.py‎

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -157,25 +157,30 @@ def pytest_generate_tests(metafunc):
157157
158158
Make parametrized tests with corresponding order marks.
159159
"""
160-
if getattr(metafunc, "function", False) and getattr(
161-
metafunc.function, "pytestmark", False
162-
):
160+
function = getattr(metafunc, "function", None)
161+
if not function:
162+
return
163+
# The order marks are removed from the function below, so if the same
164+
# function is collected again (e.g. inherited by another test class or
165+
# imported into another module), the removed marks are taken from here
166+
order_marks = getattr(function, "_pytest_order_marks", None)
167+
if order_marks is None:
163168
# Get list of order marks
164-
marks = metafunc.function.pytestmark
169+
marks = getattr(function, "pytestmark", [])
165170
order_marks = [mark for mark in marks if mark.name == "order"]
166171
if len(order_marks) > 1:
167172
# Remove all order marks
168-
metafunc.function.pytestmark = [
169-
mark for mark in marks if mark.name != "order"
170-
]
171-
# Prepare arguments for parametrization with order marks
172-
args = [
173-
pytest.param(_get_mark_description(mark), marks=[mark])
174-
for mark in order_marks
175-
]
176-
if "order" not in metafunc.fixturenames:
177-
metafunc.fixturenames.append("order")
178-
metafunc.parametrize("order", args)
173+
function.pytestmark = [mark for mark in marks if mark.name != "order"]
174+
function._pytest_order_marks = order_marks
175+
if len(order_marks) > 1:
176+
# Prepare arguments for parametrization with order marks
177+
args = [
178+
pytest.param(_get_mark_description(mark), marks=[mark])
179+
for mark in order_marks
180+
]
181+
if "order" not in metafunc.fixturenames:
182+
metafunc.fixturenames.append("order")
183+
metafunc.parametrize("order", args)
179184

180185

181186
@pytest.fixture

‎tests/test_multiple_ordering.py‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,61 @@ def test_2():
113113
"TestA::test_1_and_3[index=3]",
114114
"TestA::test_4",
115115
]
116+
117+
118+
def test_multiple_markers_in_inherited_classes(item_names_for):
119+
test_content = """
120+
import pytest
121+
122+
class Base:
123+
@pytest.mark.order(1)
124+
@pytest.mark.order(-1)
125+
def test_1_and_last(self):
126+
pass
127+
128+
class TestA(Base):
129+
pass
130+
131+
class TestB(Base):
132+
pass
133+
134+
def test_2():
135+
pass
136+
"""
137+
assert item_names_for(test_content) == [
138+
"TestA::test_1_and_last[index=1]",
139+
"TestB::test_1_and_last[index=1]",
140+
"test_2",
141+
"TestA::test_1_and_last[index=-1]",
142+
"TestB::test_1_and_last[index=-1]",
143+
]
144+
145+
146+
def test_multiple_markers_on_imported_test(test_path):
147+
test_path.makepyfile(
148+
test_a="""
149+
import pytest
150+
151+
@pytest.mark.order(1)
152+
@pytest.mark.order(-1)
153+
def test_1_and_last():
154+
pass
155+
""",
156+
test_b="""
157+
from test_a import test_1_and_last
158+
159+
def test_2():
160+
pass
161+
""",
162+
)
163+
result = test_path.runpytest("-v")
164+
result.assert_outcomes(passed=5)
165+
result.stdout.fnmatch_lines(
166+
[
167+
"test_a.py::test_1_and_last[[]index=1] PASSED",
168+
"test_b.py::test_1_and_last[[]index=1] PASSED",
169+
"test_b.py::test_2 PASSED",
170+
"test_a.py::test_1_and_last[[]index=-1] PASSED",
171+
"test_b.py::test_1_and_last[[]index=-1] PASSED",
172+
]
173+
)

0 commit comments

Comments
 (0)