{"record":{"id":"621addbfce52cf75","repo":"pytest-dev/pytest","slug":"the-fixture-value-for-argname-is-not-available","errorCode":null,"errorMessage":"The fixture value for \"{argname}\" is not available during teardown because it was not previously requested.\nOnly fixtures that were already active can be retrieved during teardown.\nRequest the fixture before teardown begins by declaring it in the fixture signature or by calling request.getfixturevalue() before the fixture yields.","messagePattern":"The fixture value for \"(.+?)\" is not available during teardown because it was not previously requested\\.\nOnly fixtures that were already active can be retrieved during teardown\\.\nRequest the fixture before teardown begins by declaring it in the fixture signature or by calling request\\.getfixturevalue\\(\\) before the fixture yields\\.","errorType":"exception","errorClass":"FixtureLookupError","httpStatus":null,"severity":"error","filePath":"src/_pytest/fixtures.py","lineNumber":663,"sourceCode":"        self.node.add_marker(marker)\n\n    def raiseerror(self, msg: str | None) -> NoReturn:\n        \"\"\"Raise a FixtureLookupError exception.\n\n        :param msg:\n            An optional custom error message.\n        \"\"\"\n        raise FixtureLookupError(None, self, msg)\n\n    def _raise_teardown_lookup_error(self, argname: str) -> NoReturn:\n        msg = (\n            f'The fixture value for \"{argname}\" is not available during teardown '\n            \"because it was not previously requested.\\n\"\n            \"Only fixtures that were already active can be retrieved during teardown.\\n\"\n            \"Request the fixture before teardown begins by declaring it in the fixture \"\n            \"signature or by calling request.getfixturevalue() before the fixture yields.\"\n        )\n        raise FixtureLookupError(argname, self, msg)\n\n    def getfixturevalue(self, argname: str) -> Any:\n        \"\"\"Dynamically run a named fixture function.\n\n        Declaring fixtures via function argument is recommended where possible.\n        But if you can only decide whether to use another fixture at test\n        setup time, you may use this function to retrieve it inside a fixture\n        or test function body.\n\n        This method can be used during the test setup phase or the test run\n        phase. Avoid using it during the teardown phase.\n\n        .. versionchanged:: 9.1\n            Calling ``request.getfixturevalue()`` during teardown to request a\n            fixture that was not already requested\n            :ref:`is deprecated <dynamic-fixture-request-during-teardown>`.\n\n        :param argname:","sourceCodeStart":645,"sourceCodeEnd":681,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/fixtures.py#L645-L681","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Declare the fixture in the fixture signature so it is set up before teardown begins: 'def myfix(request, other_fix):'.","Call request.getfixturevalue('x') BEFORE the yield so it is active by teardown time.","Track with your own flag whether setup happened, and only clean up if it did."],"exampleFix":"// before\n@pytest.fixture\ndef myfix(request):\n    yield\n    val = request.getfixturevalue('maybe')   # may not have been set up\n// after\n@pytest.fixture\ndef myfix(request, maybe):\n    yield\n    # 'maybe' is guaranteed active; safe to use","handlingStrategy":"validation","validationCode":"def teardown_safe_getfixturevalue(request, argname):\n    active = getattr(request, '_fixture_defs', {})\n    if argname not in active:\n        raise RuntimeError(\n            f'cannot retrieve {argname!r} at teardown; declare it before yield'\n        )\n    return request.getfixturevalue(argname)","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Declare optional dependencies in the fixture signature so they are always active.","Call request.getfixturevalue before yield, not after.","Track setup state with a flag and only clean up what was actually created."],"tags":["fixtures","teardown","request","lifecycle"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}