Skip to content

get_closest_marker is working unexpectedly #14329

Done

Description

@Wintreist
  • a detailed description of the bug or problem you are having
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible

Description:
get_closest_marker is working unexpectedly for me
I redefine the mark on the class in which the test is defined, but I get the mark of the parent of the class, i.e. a deeper mark than expected.

Versions:
pytest v9.0.2, Windows 10

Minimal Example:

from typing import cast

import pytest


@pytest.mark.some_mark(data=0)
class TestParent:
    def test_case(self, request: pytest.FixtureRequest):
        some_mark = self.get_mark(request)
        assert some_mark.kwargs.get("data") == 0

    def get_mark(self, request: pytest.FixtureRequest):
        some_mark = cast(pytest.Function, request.node).get_closest_marker("some_mark")
        assert some_mark
        return some_mark


@pytest.mark.some_mark(data=1)
class TestChild(TestParent):
    def test_case(self, request: pytest.FixtureRequest):
        some_mark = self.get_mark(request)
        assert some_mark.kwargs.get("data") == 1

Test: test_closest_mark.py::TestChild::test_case fails:

>       assert some_mark.kwargs.get("data") == 1
E       AssertionError: assert 0 == 1
E        +  where 0 = <built-in method get of dict object at 0x000001AC94A73740>('data')
E        +    where <built-in method get of dict object at 0x000001AC94A73740> = {'data': 0}.get
E        +      where {'data': 0} = Mark(name='some_mark', args=(), kwargs={'data': 0}).kwargs

In my understanding, the mark defined on TestChild is a closer mark than the mark defined on TestParent

Activity

  1. RonnyPfannschmidt commented on Mar 27, 2026

    @RonnyPfannschmidt
    Member

    #11155 added this to resolve a regression - https://xkcd.com/1172/ applies
    i wonder if there’s a nicer way to do this

  2. Wintreist commented on Mar 27, 2026

    @Wintreist
    ContributorAuthor

    @RonnyPfannschmidt, Hi!
    I looked at the original issue #10447 for which this change was added.
    I would like to note that in that task, it was about mark usefixtures.
    In terms of inheritance, I agree that it is expected that a fixture from the parent will be called first, followed by a fixture from the child. But I would still expect that .get_closest_marker("usefixtures") would be exactly the usefixtures on the child.
    I want to say that mark usefixture is a unique case. Perhaps it's worth processing usefixtures separately?

  3. RonnyPfannschmidt commented on Mar 27, 2026

    @RonnyPfannschmidt
    Member

    Yes a higher order plan is needed

    The bugfix corrected a regression at the expense of clarity

    The propper fix needs extra consideration

  4. Wintreist commented on Mar 28, 2026

    @Wintreist
    ContributorAuthor

    @RonnyPfannschmidt, Hi!
    I created a PR that solves this problem.
    I realize that I probably don't understand everything about pytest, and maybe I could have missed something, but all the tests passed. So if possible, please take a look at my PR

  5. added
    type: bug
    topic: marks
    on Apr 7, 2026
  6. added theissue type on Apr 7, 2026
  7. Wintreist commented on Jun 19, 2026

    @Wintreist
    ContributorAuthor

    Hi!
    I remind you about this problem, it was already ready to merge, but something got up.
    Please make a decision on it.

  8. RonnyPfannschmidt commented on Jun 20, 2026

    @RonnyPfannschmidt
    Member

    I created a draft that corrects the issue without reordering

  9. 11 remaining items

  10. RonnyPfannschmidt commented on Nov 25, 2026

    @RonnyPfannschmidt
    Member

    closed by #14630

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: markstype: bug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions