Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,18 @@ def setup(self) -> None:
self.addfinalizer(func)

def collect(self) -> Iterable[nodes.Item | nodes.Collector]:
# Apply package-level pytestmark from __init__.py so markers propagate
# to contained tests (regression from Package no longer being a Module).
try:
init_mod = importtestmodule(self.path / "__init__.py", self.config)
except nodes.Collector.CollectError:
init_mod = None
if init_mod is not None and not getattr(self, "_package_marks_applied", False):
marks = get_unpacked_marks(init_mod)
self.own_markers.extend(marks)
self.keywords.update((mark.name, mark) for mark in marks)
self._package_marks_applied = True # type: ignore[attr-defined]

# Always collect __init__.py first.
def sort_key(entry: os.DirEntry[str]) -> object:
return (entry.name != "__init__.py", entry.name)
Expand Down
15 changes: 15 additions & 0 deletions testing/test_mark.py
Original file line number Diff line number Diff line change
Expand Up @@ -1377,3 +1377,18 @@ def test_something():
# The module is buggy (__getattr__ returns None for all attributes),
# so no tests are collected, but pytest should NOT crash with a TypeError.
assert result.ret != ExitCode.INTERNAL_ERROR


def test_package_level_pytestmark_propagates(pytester: Pytester) -> None:
"""Pytestmark in package __init__.py must apply to tests in the package (#14737)."""
pkg = pytester.mkpydir("skippedpkg")
pkg.joinpath("__init__.py").write_text(
'import pytest\npytestmark = pytest.mark.skip(reason="package is disabled")\n',
encoding="utf-8",
)
pkg.joinpath("test_skipped.py").write_text(
"def test_should_be_skipped():\n assert False\n",
encoding="utf-8",
)
result = pytester.runpytest("-q")
result.assert_outcomes(skipped=1)
Loading