pytest-dev/pytest · error · FixtureLookupError

fixture '{argname}' not found

Error message

fixture '{argname}' not found

What it means

FixtureLookupError is pytest's signal that a fixture name could not be resolved for the current test. The short message 'fixture {argname} not found' is the headline; the full FixtureLookupError formats a detailed report (available fixtures, misspelled suggestions, line numbers) when rendered. It is raised both at static collection time (param/signature resolution) and at dynamic getfixturevalue() time.

Source

Thrown at src/_pytest/fixtures.py:728

            return RequestFixtureDef(self)

        # If we already finished computing a fixture by this name in this item,
        # return it.
        fixturedef = self._fixture_defs.get(argname)
        if fixturedef is not None:
            self._check_scope(fixturedef, fixturedef._scope)
            return fixturedef

        # Find the appropriate fixturedef.
        fixturedefs = self._arg2fixturedefs.get(argname, None)
        if fixturedefs is None:
            # We arrive here because of a dynamic call to
            # getfixturevalue(argname) which was naturally
            # not known at parsing/collection time.
            fixturedefs = self._fixturemanager.getfixturedefs(argname, self._pyfuncitem)
        # No fixtures defined with this name.
        if fixturedefs is None:
            raise FixtureLookupError(argname, self)
        # The are no fixtures with this name applicable for the function.
        if not fixturedefs:
            raise FixtureLookupError(argname, self)

        # A fixture may override another fixture with the same name, e.g. a
        # fixture in a module can override a fixture in a conftest, a fixture in
        # a class can override a fixture in the module, and so on.
        # An overriding fixture can request its own name (possibly indirectly);
        # in this case it gets the value of the fixture it overrides, one level
        # up.
        # Check how many `argname`s deep we are, and take the next one.
        # `fixturedefs` is sorted from furthest to closest, so use negative
        # indexing to go in reverse.
        index = -1
        for request in self._iter_chain():
            if request.fixturename == argname:
                index -= 1
        # If already consumed all of the available levels, fail.

View on GitHub (pinned to 0d6fbdeffa)

Solutions

  1. Check the spelling of the fixture name against its definition (and against the 'available fixtures' list in the rendered error).
  2. Ensure the fixture is defined in the test module, a conftest.py in the right directory, or an installed/importable plugin.
  3. If the fixture is in a plugin, confirm the plugin is installed and enabled (pytest --trace-config or pip show).
  4. If the fixture is in a conftest, make sure that conftest imports cleanly (fix any ImportError shown earlier in the run).
  5. For dynamic lookups, verify the fixture name is registered for this test's path via pytest --fixtures.

Example fix

// before
@pytest.fixture
def usr():
    return {}

def test_user(user):   # typo: 'user' vs 'usr'
    assert user
// after
@pytest.fixture
def user():
    return {}

def test_user(user):
    assert user
Defensive patterns

Strategy: validation

Validate before calling

def fixture_exists(request, argname: str) -> bool:
    defs = request._fixturemanager.getfixturedefs(argname, request._pyfuncitem)
    return bool(defs)

if not fixture_exists(request, 'myfix'):
    pytest.skip('myfix not available for this test')

Type guard

null

Try / catch

import pytest

try:
    val = request.getfixturevalue('maybe_missing')
except pytest.FixtureLookupError:
    val = None

Prevention

When it happens

Trigger: A test or fixture referencing a fixture name that is not defined in the test module, any conftest.py, or an installed plugin; a typo in the fixture name; the fixture lives in a conftest that is not on the path for this test; requesting a fixture that is parametrized out of scope; a conftest with a syntax/import error preventing fixture registration.

Common situations: Misspelled fixture name; conftest.py import failure (a different error masks the fixture); fixture defined in a plugin that is not installed or enabled; moving a test out of a package whose conftest defined the fixture; scope/parametrize mismatch leaving no applicable fixturedef.

Related errors


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