pytest-dev / pytest-dev/pytest

iter_markers order is top-to-bottom at module node/scope

Open
#7,597 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: marks
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

Not entirely sure if it would be considered a bug, but it surprised me. I expect iter_markers to return the markers in bottom-to-top order (outwards from the test), but for the pytestmark variable this is not the case, it returns them in the list order.

import pytest

pytestmark = [
    pytest.mark.order(4),
    pytest.mark.order(3),
]

@pytest.mark.order(2)
@pytest.mark.order(1)
def test_marker(request):
    lowest = 0
    for mark in request.node.iter_markers("order"):
        order = mark.args[0]
        assert order > lowest
        lowest = order
FAILED test_marker.py::test_marker - assert 3 > 4

I expect this will also apply for pytestmark as a class variable.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in the issue and trace request.node.iter_markers("order") for module-level pytestmark, then check whether class-level pytestmark follows the same path. Done means the marker sequence is bottom-to-top as expected while preserving the existing test-marker behavior; add or update coverage for the shown module and class cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.