diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 6b882fa3515..33a4a20ab25 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -464,6 +464,17 @@ def module(self): assert mod is not None return mod.obj + @property + def package(self): + """Python package object where the test function was collected.""" + if self.scope not in ("function", "class", "module", "package"): + raise AttributeError( + f"package not available in {self.scope}-scoped context" + ) + pkg = self._pyfuncitem.getparent(_pytest.python.Package) + assert pkg is not None + return pkg.obj + @property def path(self) -> Path: """Path where the test function was collected.""" diff --git a/testing/test_doctest.py b/testing/test_doctest.py index 4aa4876c711..f0dd544d81a 100644 --- a/testing/test_doctest.py +++ b/testing/test_doctest.py @@ -1366,7 +1366,7 @@ def auto(request): result.stdout.no_fnmatch_line("*FAILURES*") result.stdout.fnmatch_lines(["*=== 1 passed in *"]) - @pytest.mark.parametrize("scope", SCOPES) + @pytest.mark.parametrize("scope", [*SCOPES, "package"]) def test_auto_use_request_attributes(self, pytester, scope): """Check that all attributes of a request in an autouse fixture behave as expected when requested for a doctest item. @@ -1377,6 +1377,8 @@ def test_auto_use_request_attributes(self, pytester, scope): @pytest.fixture(autouse=True, scope="{scope}") def auto(request): + if "{scope}" == 'package': + assert request.package is None if "{scope}" == 'module': assert request.module is None if "{scope}" == 'class':