From ea1892f662ec85799ebae89695d361bbd685c9ef Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 6 Sep 2021 19:49:51 +0200 Subject: [PATCH 1/4] Fix non-sensical error message Introduced in 12de92cd2b818906d342dbdfaf96999887bc9658 / #7698 --- changelog/9077.bugfix.rst | 1 + src/_pytest/fixtures.py | 2 +- 2 files changed, 2 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 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 From e2017a7f16fe566c93613814a820b7327312870f Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 6 Sep 2021 21:54:14 +0200 Subject: [PATCH 2/4] Add a test --- testing/python/fixtures.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index e634dab45c1..25144a4781b 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -1092,6 +1092,26 @@ def test_1(arg): reprec = pytester.inline_run() reprec.assertoutcome(passed=2) + @pytest.mark.parametrize("name", ["path", "fspath", "module"]) + def test_session_scoped_unavailable_attributes(self, pytester, name): + pytester.makepyfile( + f""" + import pytest + + @pytest.fixture(scope="session") + def fixt(request): + request.{name} + + def test_request(fixt): + pass + """ + ) + result = pytester.runpytest() + expected = "path" if name == "fspath" else name + result.stdout.fnmatch_lines( + [f"E*AttributeError: {expected} not available in session-scoped context"] + ) + class TestRequestMarking: def test_applymarker(self, pytester: Pytester) -> None: From 33923141cb8dbecf50c1bf9931576a5577809465 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 9 Sep 2021 20:05:13 +0200 Subject: [PATCH 3/4] Put the unit back into unittest --- testing/python/fixtures.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index 25144a4781b..e6b940767d4 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -1092,25 +1092,21 @@ def test_1(arg): reprec = pytester.inline_run() reprec.assertoutcome(passed=2) - @pytest.mark.parametrize("name", ["path", "fspath", "module"]) - def test_session_scoped_unavailable_attributes(self, pytester, name): - pytester.makepyfile( - f""" - import pytest - @pytest.fixture(scope="session") - def fixt(request): - request.{name} +class TestRequestSessionScoped: - def test_request(fixt): - pass - """ - ) - result = pytester.runpytest() + @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 - result.stdout.fnmatch_lines( - [f"E*AttributeError: {expected} not available in session-scoped context"] - ) + with pytest.raises( + AttributeError, + match=f"{expected} not available in session-scoped context", + ): + getattr(session_request, name) class TestRequestMarking: From 120615f96f1d8b16eb7767210aa8074018a07020 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 9 Sep 2021 18:06:24 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- testing/python/fixtures.py | 1 - 1 file changed, 1 deletion(-) diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index e6b940767d4..ea66f50f0d9 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -1094,7 +1094,6 @@ def test_1(arg): class TestRequestSessionScoped: - @pytest.fixture(scope="session") def session_request(self, request): return request