Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
- `--order-marker-prefix` uses the complete marker suffix as the order index,
so multi-digit and negative indices work; negative indices can be written
as `neg` (for example `m_neg1` for -1)
- a test with multiple order markers is run once per marker even if it is
collected more than once, e.g. as a method inherited by several test classes;
previously only the first collected instance kept its order markers

## [Version 1.5.0](https://pypi.org/project/pytest-order/1.5.0/) (2026-06-13)
Adds option to abort tests after a failed ordering.
Expand Down
35 changes: 20 additions & 15 deletions src/pytest_order/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,25 +157,30 @@ def pytest_generate_tests(metafunc):

Make parametrized tests with corresponding order marks.
"""
if getattr(metafunc, "function", False) and getattr(
metafunc.function, "pytestmark", False
):
function = getattr(metafunc, "function", None)
if not function:
return
# The order marks are removed from the function below, so if the same
# function is collected again (e.g. inherited by another test class or
# imported into another module), the removed marks are taken from here
order_marks = getattr(function, "_pytest_order_marks", None)
if order_marks is None:
# Get list of order marks
marks = metafunc.function.pytestmark
marks = getattr(function, "pytestmark", [])
order_marks = [mark for mark in marks if mark.name == "order"]
if len(order_marks) > 1:
# Remove all order marks
metafunc.function.pytestmark = [
mark for mark in marks if mark.name != "order"
]
# Prepare arguments for parametrization with order marks
args = [
pytest.param(_get_mark_description(mark), marks=[mark])
for mark in order_marks
]
if "order" not in metafunc.fixturenames:
metafunc.fixturenames.append("order")
metafunc.parametrize("order", args)
function.pytestmark = [mark for mark in marks if mark.name != "order"]
function._pytest_order_marks = order_marks
if len(order_marks) > 1:
# Prepare arguments for parametrization with order marks
args = [
pytest.param(_get_mark_description(mark), marks=[mark])
for mark in order_marks
]
if "order" not in metafunc.fixturenames:
metafunc.fixturenames.append("order")
metafunc.parametrize("order", args)


@pytest.fixture
Expand Down
58 changes: 58 additions & 0 deletions tests/test_multiple_ordering.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,61 @@ def test_2():
"TestA::test_1_and_3[index=3]",
"TestA::test_4",
]


def test_multiple_markers_in_inherited_classes(item_names_for):
test_content = """
import pytest

class Base:
@pytest.mark.order(1)
@pytest.mark.order(-1)
def test_1_and_last(self):
pass

class TestA(Base):
pass

class TestB(Base):
pass

def test_2():
pass
"""
assert item_names_for(test_content) == [
"TestA::test_1_and_last[index=1]",
"TestB::test_1_and_last[index=1]",
"test_2",
"TestA::test_1_and_last[index=-1]",
"TestB::test_1_and_last[index=-1]",
]


def test_multiple_markers_on_imported_test(test_path):
test_path.makepyfile(
test_a="""
import pytest

@pytest.mark.order(1)
@pytest.mark.order(-1)
def test_1_and_last():
pass
""",
test_b="""
from test_a import test_1_and_last

def test_2():
pass
""",
)
result = test_path.runpytest("-v")
result.assert_outcomes(passed=5)
result.stdout.fnmatch_lines(
[
"test_a.py::test_1_and_last[[]index=1] PASSED",
"test_b.py::test_1_and_last[[]index=1] PASSED",
"test_b.py::test_2 PASSED",
"test_a.py::test_1_and_last[[]index=-1] PASSED",
"test_b.py::test_1_and_last[[]index=-1] PASSED",
]
)
Loading