Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix non-sensical error message #9077

Merged
merged 4 commits into from Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/9077.bugfix.rst
@@ -0,0 +1 @@
Fixed confusing error message when ``request.fspath`` / ``request.path`` was accessed from a session-scoped fixture.
2 changes: 1 addition & 1 deletion src/_pytest/fixtures.py
Expand Up @@ -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

Expand Down
15 changes: 15 additions & 0 deletions testing/python/fixtures.py
Expand Up @@ -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(
Expand Down