Skip to content

Commit 8713c32

Browse files
committed
python: unpacked marks need to be added to keywords on all node types
(except `Instance`) Currently, `Function` does this manually, but other node types don't get their markers added to their `keywords`, but they should, if only for consistency.
1 parent e9bb1aa commit 8713c32

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/_pytest/python.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ def obj(self):
303303
# used to avoid Function marker duplication
304304
if self._ALLOW_MARKERS:
305305
self.own_markers.extend(get_unpacked_marks(self.obj))
306+
# This assumes that `obj` is called before there is a chance
307+
# to add custom keys to `self.keywords`, so no fear of overriding.
308+
self.keywords.update({mark.name: mark for mark in self.own_markers})
306309
return obj
307310

308311
@obj.setter

testing/test_collection.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,36 @@ def test_method(self): pass
881881
assert item.keywords["kw"] == "method"
882882
assert len(item.keywords) == len(set(item.keywords))
883883

884+
def test_unpacked_marks_added_to_keywords(self, pytester: Pytester) -> None:
885+
item = pytester.getitem(
886+
"""
887+
import pytest
888+
pytestmark = pytest.mark.foo
889+
class TestClass:
890+
pytestmark = pytest.mark.bar
891+
def test_method(self): pass
892+
test_method.pytestmark = pytest.mark.baz
893+
""",
894+
"test_method",
895+
)
896+
assert isinstance(item, pytest.Function)
897+
cls = item.getparent(pytest.Class)
898+
assert cls is not None
899+
mod = item.getparent(pytest.Module)
900+
assert mod is not None
901+
902+
assert item.keywords["foo"] == pytest.mark.foo.mark
903+
assert item.keywords["bar"] == pytest.mark.bar.mark
904+
assert item.keywords["baz"] == pytest.mark.baz.mark
905+
906+
assert cls.keywords["foo"] == pytest.mark.foo.mark
907+
assert cls.keywords["bar"] == pytest.mark.bar.mark
908+
assert "baz" not in cls.keywords
909+
910+
assert mod.keywords["foo"] == pytest.mark.foo.mark
911+
assert "bar" not in mod.keywords
912+
assert "baz" not in mod.keywords
913+
884914

885915
COLLECTION_ERROR_PY_FILES = dict(
886916
test_01_failure="""

0 commit comments

Comments
 (0)