Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/9984.trivial.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Improve the error message when we attempt to access a fixture that has been
torn down.
Add an additional sentence to the docstring explaining when it's not a good
idea to call getfixturevalue.
9 changes: 8 additions & 1 deletion src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,11 +548,18 @@ def getfixturevalue(self, argname: str) -> Any:
setup time, you may use this function to retrieve it inside a fixture
or test function body.

This method can be used during the test setup phase or the test run
phase, but during the test teardown phase a fixture's value may not
be available.

:raises pytest.FixtureLookupError:
If the given fixture could not be found.
"""
fixturedef = self._get_active_fixturedef(argname)
assert fixturedef.cached_result is not None
assert fixturedef.cached_result is not None, (
f'The fixture value for "{argname}" is not available. '
"This can happen when the fixture has already been torn down."
)
return fixturedef.cached_result[0]

def _get_active_fixturedef(
Expand Down