diff --git a/changelog/9077.bugfix.rst b/changelog/9077.bugfix.rst new file mode 100644 index 00000000000..fcee5d385df --- /dev/null +++ b/changelog/9077.bugfix.rst @@ -0,0 +1 @@ +Fixed confusing error message when ``request.fspath`` / ``request.path`` was accessed from a session-scoped fixture. diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 79b7c877f46..de27abdb734 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -536,7 +536,7 @@ def fspath(self) -> LEGACY_PATH: @property def path(self) -> Path: if self.scope not in ("function", "class", "module", "package"): - raise AttributeError(f"module not available in {self.scope}-scoped context") + raise AttributeError(f"path not available in {self.scope}-scoped context") # TODO: Remove ignore once _pyfuncitem is properly typed. return self._pyfuncitem.path # type: ignore diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index e634dab45c1..ea66f50f0d9 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -1093,6 +1093,21 @@ def test_1(arg): reprec.assertoutcome(passed=2) +class TestRequestSessionScoped: + @pytest.fixture(scope="session") + def session_request(self, request): + return request + + @pytest.mark.parametrize("name", ["path", "fspath", "module"]) + def test_session_scoped_unavailable_attributes(self, session_request, name): + expected = "path" if name == "fspath" else name + with pytest.raises( + AttributeError, + match=f"{expected} not available in session-scoped context", + ): + getattr(session_request, name) + + class TestRequestMarking: def test_applymarker(self, pytester: Pytester) -> None: item1, item2 = pytester.getitems(