Visitar URL original
Keep multiple order markers for tests that are collected more than once by TohruSu · Pull Request #277 · pytest-dev/pytest-order · GitHub
Skip to content

Keep multiple order markers for tests that are collected more than once - #277

Open
TohruSu wants to merge 1 commit into
pytest-dev:mainfrom
TohruSu:fix-multiple-order-marks-inherited
Open

TohruSu wants to merge 1 commit into
pytest-dev:mainfrom
TohruSu:fix-multiple-order-marks-inherited

Conversation

@TohruSu

@TohruSu TohruSu commented Nov 13, 2026

Copy link
Copy Markdown

When a test has more than one order marker, pytest_generate_tests parametrizes it with one case per marker and removes the order marks from the test function, so that each case keeps only its own marker. The function object is shared, though, so when the same function is collected again, for example as a method inherited by a second test class or as a test function imported into another module, the later collection no longer finds any order marks. That test then runs only once, without an order marker:

import pytest


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


class TestA(Base):
    pass


class TestB(Base):
    pass

Here TestA::test_x runs first and last as expected, but TestB::test_x runs once in the middle.

This change keeps the removed markers on the function and uses them again when the same function is collected another time. Tests with a single order marker or none are not affected.

I added tests for the inherited and the imported case and a changelog entry. The new tests fail without the change, and the test suite passes on Windows with Python 3.11 and pytest 9.2.0 and 6.2.5.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant