Skip to content

Commit 0e62861

Browse files
committed
Improve error message in getfixturevalue
1 parent 611b579 commit 0e62861

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

changelog/9984.trivial.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Improve the error message when we attempt to access a fixture that has been
2+
torn down.
3+
Add an additional sentence to the docstring explaining when it's not a good
4+
idea to call getfixturevalue.

src/_pytest/fixtures.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,11 +548,18 @@ def getfixturevalue(self, argname: str) -> Any:
548548
setup time, you may use this function to retrieve it inside a fixture
549549
or test function body.
550550
551+
This method can be used during the test setup phase or the test run
552+
phase, but during the test teardown phase a fixture's value may not
553+
be available.
554+
551555
:raises pytest.FixtureLookupError:
552556
If the given fixture could not be found.
553557
"""
554558
fixturedef = self._get_active_fixturedef(argname)
555-
assert fixturedef.cached_result is not None
559+
assert fixturedef.cached_result is not None, (
560+
f'The fixture value for "{argname}" is not available. '
561+
"This can happen when the fixture has already been torn down."
562+
)
556563
return fixturedef.cached_result[0]
557564

558565
def _get_active_fixturedef(

0 commit comments

Comments
 (0)