Skip to content

get_closest_marker is working unexpectedly #14329

@Wintreist

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: marksrelated to marks, either the general marks or builtintype: bugproblem that needs to be addressed

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions