From 112204cf8d292ee3221d46fa16d4f22334682e6d Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 24 Sep 2021 06:15:53 +0200 Subject: [PATCH] Fix non-sensical error message (#9077) * Fix non-sensical error message Introduced in 12de92cd2b818906d342dbdfaf96999887bc9658 / #7698 * Add a test * Put the unit back into unittest * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- changelog/9077.bugfix.rst | 1 + src/_pytest/fixtures.py | 2 +- testing/python/fixtures.py | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 changelog/9077.bugfix.rst diff --git a/changelog/9077.bugfix.rst b/changelog/9077.bugfix.rst new file mode 100644 index 000000000..fcee5d385 --- /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 79b7c877f..de27abdb7 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -536,7 +536,7 @@ class FixtureRequest: @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 e634dab45..ea66f50f0 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -1093,6 +1093,21 @@ class TestRequestBasic: 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(