From 06ac7eba1e8e3ebcd4fe7b9008862abc1d56bc98 Mon Sep 17 00:00:00 2001 From: "SU, Yanyu" <238259854+TohruSu@users.noreply.github.com> Date: Fri, 13 Nov 2026 22:47:30 +0900 Subject: [PATCH] Keep multiple order markers for tests that are collected more than once --- CHANGELOG.md | 3 ++ src/pytest_order/plugin.py | 35 +++++++++++--------- tests/test_multiple_ordering.py | 58 +++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b12a9cb..ffbae41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/src/pytest_order/plugin.py b/src/pytest_order/plugin.py index 8348df1..f420ee9 100644 --- a/src/pytest_order/plugin.py +++ b/src/pytest_order/plugin.py @@ -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 diff --git a/tests/test_multiple_ordering.py b/tests/test_multiple_ordering.py index df0fa65..6df91bf 100644 --- a/tests/test_multiple_ordering.py +++ b/tests/test_multiple_ordering.py @@ -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", + ] + )