Skip to content

Commit 112204c

Browse files
Fix non-sensical error message (#9077)
* Fix non-sensical error message Introduced in 12de92c / #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>
1 parent 037d290 commit 112204c

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

changelog/9077.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed confusing error message when ``request.fspath`` / ``request.path`` was accessed from a session-scoped fixture.

src/_pytest/fixtures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ def fspath(self) -> LEGACY_PATH:
536536
@property
537537
def path(self) -> Path:
538538
if self.scope not in ("function", "class", "module", "package"):
539-
raise AttributeError(f"module not available in {self.scope}-scoped context")
539+
raise AttributeError(f"path not available in {self.scope}-scoped context")
540540
# TODO: Remove ignore once _pyfuncitem is properly typed.
541541
return self._pyfuncitem.path # type: ignore
542542

testing/python/fixtures.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,21 @@ def test_1(arg):
10931093
reprec.assertoutcome(passed=2)
10941094

10951095

1096+
class TestRequestSessionScoped:
1097+
@pytest.fixture(scope="session")
1098+
def session_request(self, request):
1099+
return request
1100+
1101+
@pytest.mark.parametrize("name", ["path", "fspath", "module"])
1102+
def test_session_scoped_unavailable_attributes(self, session_request, name):
1103+
expected = "path" if name == "fspath" else name
1104+
with pytest.raises(
1105+
AttributeError,
1106+
match=f"{expected} not available in session-scoped context",
1107+
):
1108+
getattr(session_request, name)
1109+
1110+
10961111
class TestRequestMarking:
10971112
def test_applymarker(self, pytester: Pytester) -> None:
10981113
item1, item2 = pytester.getitems(

0 commit comments

Comments
 (0)