pytest-dev/pytest · error · FixtureLookupError

The fixture value for

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

During fixture teardown, pytest can only yield fixture values that were already activated during setup. If a teardown path tries to look up a fixture that was never requested (e.g. via request.getfixturevalue after yield), pytest raises FixtureLookupError explaining the fixture was not active and how to make it active before teardown.

Solutions

  1. Declare the fixture in the fixture signature so it is set up before teardown begins: 'def myfix(request, other_fix):'.
  2. Call request.getfixturevalue('x') BEFORE the yield so it is active by teardown time.
  3. Track with your own flag whether setup happened, and only clean up if it did.

Example fix

// before
@pytest.fixture
def myfix(request):
    yield
    val = request.getfixturevalue('maybe')   # may not have been set up
// after
@pytest.fixture
def myfix(request, maybe):
    yield
    # 'maybe' is guaranteed active; safe to use
Defensive patterns

Strategy: validation

Validate before calling

def teardown_safe_getfixturevalue(request, argname):
    active = getattr(request, '_fixture_defs', {})
    if argname not in active:
        raise RuntimeError(
            f'cannot retrieve {argname!r} at teardown; declare it before yield'
        )
    return request.getfixturevalue(argname)

Type guard

null

Try / catch

null

Prevention

When it happens

Trigger: Calling request.getfixturevalue('x') inside a 'finally' or after 'yield' in a fixture, where 'x' was not declared in the fixture signature or requested earlier; conditional teardown that reaches for a fixture only sometimes set up.

Common situations: Refactoring a fixture to do cleanup of an optional dependency; try/finally blocks that call getfixturevalue; a fixture that conditionally depends on another but only resolves it during teardown.

Related errors


AI-assisted analysis of pytest-dev/pytest@0d6fbdeffa (2026-08-11). Data as JSON: /api/errors/621addbfce52cf75. Report an issue: GitHub.

Appendix: 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 0d6fbdeffa)