Fix non-sensical error message (#9077)

* Fix non-sensical error message

Introduced in 12de92cd2b / #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>
This commit is contained in:
Florian Bruhin 2021-09-24 06:15:53 +02:00 committed by GitHub
parent 037d2903be
commit 112204cf8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 1 deletions

View File

@ -0,0 +1 @@
Fixed confusing error message when ``request.fspath`` / ``request.path`` was accessed from a session-scoped fixture.

View File

@ -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

View File

@ -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(