Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverse order on marks from MRO #11154

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/10447.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When resolving marks from a class hierarchy MRO, reverse the order so that more basic classes' marks will be processed ahead of ancestral classes.
6 changes: 4 additions & 2 deletions src/_pytest/mark/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,11 @@
"""
if isinstance(obj, type):
if not consider_mro:
mark_lists = [obj.__dict__.get("pytestmark", [])]
mark_lists: Iterable[Any] = [obj.__dict__.get("pytestmark", [])]

Check warning on line 375 in src/_pytest/mark/structures.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/mark/structures.py#L375

Added line #L375 was not covered by tests
else:
mark_lists = [x.__dict__.get("pytestmark", []) for x in obj.__mro__]
mark_lists = reversed(
[x.__dict__.get("pytestmark", []) for x in obj.__mro__]
)
mark_list = []
for item in mark_lists:
if isinstance(item, list):
Expand Down
37 changes: 36 additions & 1 deletion testing/test_mark.py
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,41 @@ class C(A, B):

all_marks = get_unpacked_marks(C)

assert all_marks == [xfail("c").mark, xfail("a").mark, xfail("b").mark]
assert all_marks == [xfail("b").mark, xfail("a").mark, xfail("c").mark]

assert get_unpacked_marks(C, consider_mro=False) == [xfail("c").mark]


# @pytest.mark.issue("https://github.com/pytest-dev/pytest/issues/10447")
def test_mark_fixture_order_mro(pytester: Pytester):
"""This ensures we walk marks of the mro starting with the base classes
the action at a distance fixtures are taken as minimal example from a real project

"""
foo = pytester.makepyfile(
"""
import pytest

@pytest.fixture
def add_attr1(request):
request.instance.attr1 = object()


@pytest.fixture
def add_attr2(request):
request.instance.attr2 = request.instance.attr1


@pytest.mark.usefixtures('add_attr1')
class Parent:
pass


@pytest.mark.usefixtures('add_attr2')
class TestThings(Parent):
def test_attrs(self):
assert self.attr1 == self.attr2
"""
)
result = pytester.runpytest(foo)
result.assert_outcomes(passed=1)