{"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/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/fixtures.py#L645-L681","documentation":"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.","triggerScenarios":"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.","commonSituations":"Cleanup code after yield that conditionally requests helper fixtures. Refactoring teardown logic to call getfixturevalue. Migrating from older pytest where this was silently allowed.","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."],"exampleFix":"// before\n@pytest.fixture\ndef myfix(request):\n    yield 1\n    helper = request.getfixturevalue(\"cleanup\")  # error\n// after\n@pytest.fixture\ndef myfix(request, cleanup):\n    yield 1\n    cleanup()  # already active","handlingStrategy":"validation","validationCode":"if argname in request.fixturenames:\n    val = request.getfixturevalue(argname)\nelse:\n    val = None  # not active; do not request during teardown","typeGuard":"def fixture_is_active(request, argname: str) -> bool:\n    return argname in set(request._fixture_defs) or argname in request.fixturenames","tryCatchPattern":"try:\n    val = request.getfixturevalue(argname)\nexcept FixtureLookupError:\n    val = None","preventionTips":["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."],"tags":["pytest","fixtures","teardown","getfixturevalue","deprecation"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}