Skip to content

Commit

Permalink
Merge pull request #9990 from petebman/improve_getfixturevalue_error_…
Browse files Browse the repository at this point in the history
…message
  • Loading branch information
Zac-HD committed May 28, 2022
2 parents c988e49 + 0e62861 commit 420dc78
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
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

0 comments on commit 420dc78

Please sign in to comment.