pytest-dev/pytest · error · FixtureLookupError
The fixture value for "{argname}" is not available during te
Error message
The fixture value for "{argname}" is not available during teardown because it was not previously requested.
Only fixtures that were already active can be retrieved during teardown.
Request the fixture before teardown begins by declaring it in the fixture signature or by calling request.getfixturevalue() before the fixture yields. What it means
Raised by `request.getfixturevalue(argname)` (via `_raise_teardown_lookup_error`) during the teardown phase for a fixture that was not already set up. Since pytest 9.1 dynamically requesting a brand-new fixture during teardown is deprecated/rejected because its own setup/teardown lifecycle cannot be safely inserted at that point. Only fixtures already active when teardown began may be retrieved.
Source
Thrown at src/_pytest/fixtures.py:663
self.node.add_marker(marker)
def raiseerror(self, msg: str | None) -> NoReturn:
"""Raise a FixtureLookupError exception.
:param msg:
An optional custom error message.
"""
raise FixtureLookupError(None, self, msg)
def _raise_teardown_lookup_error(self, argname: str) -> NoReturn:
msg = (
f'The fixture value for "{argname}" is not available during teardown '
"because it was not previously requested.\n"
"Only fixtures that were already active can be retrieved during teardown.\n"
"Request the fixture before teardown begins by declaring it in the fixture "
"signature or by calling request.getfixturevalue() before the fixture yields."
)
raise FixtureLookupError(argname, self, msg)
def getfixturevalue(self, argname: str) -> Any:
"""Dynamically run a named fixture function.
Declaring fixtures via function argument is recommended where possible.
But if you can only decide whether to use another fixture at test
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. Avoid using it during the teardown phase.
.. versionchanged:: 9.1
Calling ``request.getfixturevalue()`` during teardown to request a
fixture that was not already requested
:ref:`is deprecated <dynamic-fixture-request-during-teardown>`.
:param argname:View on GitHub (pinned to 98b357f69e)
Solutions
- Declare the needed fixture in the fixture signature so it is set up before yield: `def myfix(request, other):`.
- Call `request.getfixturevalue("other")` in the setup phase (before yield) and reuse the captured value in teardown.
- Move teardown work into a dedicated finalizer registered via `request.addfinalizer` that closes over the already-obtained value.
Example fix
// before
@pytest.fixture
def myfix(request):
yield 1
helper = request.getfixturevalue("cleanup") # error
// after
@pytest.fixture
def myfix(request, cleanup):
yield 1
cleanup() # already active Defensive patterns
Strategy: validation
Validate before calling
if argname in request.fixturenames:
val = request.getfixturevalue(argname)
else:
val = None # not active; do not request during teardown Type guard
def fixture_is_active(request, argname: str) -> bool:
return argname in set(request._fixture_defs) or argname in request.fixturenames Try / catch
try:
val = request.getfixturevalue(argname)
except FixtureLookupError:
val = None Prevention
- Declare dependent fixtures in the signature so they are set up before yield.
- Call request.getfixturevalue only in setup; reuse the captured value in teardown.
- Never introduce a brand-new fixture during teardown.
When it happens
Trigger: After `yield` in a generator fixture, calling `request.getfixturevalue("other")` where "other" was not declared as a dependency and not requested before the yield. The teardown path tries to materialize a fixture that was never instantiated.
Common situations: Cleanup code after yield that conditionally requests helper fixtures. Refactoring teardown logic to call getfixturevalue. Migrating from older pytest where this was silently allowed.
Related errors
- errors while tearing down fixture "{self.argname}" of {node}
- function not available in {self.scope}-scoped context
- cls not available in {self.scope}-scoped context
- module not available in {self.scope}-scoped context
- path not available in {self.scope}-scoped context
AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04).
Data as JSON: /data/errors/621addbfce52cf75.json.
Report an issue: GitHub.