From f13f4360d3ff379144f32b4bbb4b4fa4a7a8cf23 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Wed, 12 Oct 2022 10:20:16 +0200 Subject: [PATCH] Apply suggestions from code review Co-authored-by: Ran Benita --- changelog/7792.bugfix.rst | 6 +++++- src/_pytest/mark/structures.py | 8 +++++++- testing/test_mark.py | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/changelog/7792.bugfix.rst b/changelog/7792.bugfix.rst index 00e09ebe8..8f6563789 100644 --- a/changelog/7792.bugfix.rst +++ b/changelog/7792.bugfix.rst @@ -1 +1,5 @@ -Consider the full mro when getting marks from classes. +Marks are now inherited according to the full MRO in test classes. Previously, if a test class inherited from two or more classes, only marks from the first super-class would apply. + +When inheriting marks from super-classes, marks from the sub-classes are now ordered before marks from the super-classes, in MRO order. Previously it was the reverse. + +When inheriting marks from super-classes, the `pytestmark` attribute of the sub-class now only contains the marks directly applied to it. Previously, it also contained marks from its super-classes. Please note that this attribute should not normally be accessed directly; use :func:`pytest.Node.iter_markers` instead. diff --git a/src/_pytest/mark/structures.py b/src/_pytest/mark/structures.py index 0b1daefd6..b93bf6ed9 100644 --- a/src/_pytest/mark/structures.py +++ b/src/_pytest/mark/structures.py @@ -357,9 +357,15 @@ class MarkDecorator: def get_unpacked_marks( obj: Union[object, type], + *, consider_mro: bool = True, ) -> List[Mark]: - """Obtain the unpacked marks that are stored on an object.""" + """Obtain the unpacked marks that are stored on an object. + + If obj is a class and consider_mro is true, return marks applied to + this class and all of its super-classes in MRO order. If consider_mro + is false, only return marks applied directly to this class. + """ if isinstance(obj, type): if not consider_mro: mark_lists = [obj.__dict__.get("pytestmark", [])] diff --git a/testing/test_mark.py b/testing/test_mark.py index ebb70e247..e2d1a40c3 100644 --- a/testing/test_mark.py +++ b/testing/test_mark.py @@ -1111,7 +1111,7 @@ def test_marker_expr_eval_failure_handling(pytester: Pytester, expr) -> None: assert result.ret == ExitCode.USAGE_ERROR -def test_mark_mro(): +def test_mark_mro() -> None: xfail = pytest.mark.xfail @xfail("a")