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
pip listfrom the virtual environment you are usingDescription:
get_closest_markeris working unexpectedly for meI 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 10Minimal Example:
Test:
test_closest_mark.py::TestChild::test_casefails:In my understanding, the mark defined on
TestChildis a closer mark than the mark defined onTestParent