locustio/locust · error · ValueError

Could not find fixture for parameter {name!r} in {function.n

Error message

Could not find fixture for parameter {name!r} in {function.name}

What it means

When loading a locustfile via pytest (PytestUser), each test function's parameters must be resolvable to exactly one pytest fixture. If fixturemanager.getfixturedefs returns none for a parameter, ValueError is raised.

Source

Thrown at locust/util/load_locustfile.py:126

            "-s",  # dont capture stdin (locust uses it for opening a browser and other input events)
            path,
        ],
    )
    config._do_configure()
    session = pytest.Session.from_config(config)
    config.hook.pytest_sessionstart(session=session)
    session.perform_collect()
    config.hook.pytest_collection_modifyitems(session=session, config=config, items=session.items)
    fm = session._fixturemanager

    for function in session.items:
        if isinstance(function, pytest.Function):
            sig = inspect.signature(function.obj)
            function.kwargs = {}  # type: ignore[attr-defined]
            for name in sig.parameters:
                defs = fm.getfixturedefs(name, function)
                if not defs:
                    raise ValueError(f"Could not find fixture for parameter {name!r} in {function.name}")
                if len(defs) > 1:
                    raise ValueError(f"Multiple fixtures found for parameter {name!r} in {function.name}: {defs}")
                function.fixturedef = defs[0]  # type: ignore[attr-defined]
            if not function.name in user_classes:
                user_classes[function.name] = type(function.name, (PytestUser,), {})
                user_classes[function.name].functions = []
            user_classes[function.name].functions.append(function)
        else:
            pass  # Skipping non-function item
    return user_classes  # type: ignore

View on GitHub (pinned to f391a716e1)

Solutions

  1. Define the missing fixture (in the locustfile or an imported conftest.py)
  2. Fix the parameter name typo to match an existing fixture
  3. Install/import the plugin providing the fixture
  4. Ensure exactly one fixture matches the parameter name

Example fix

// before
def test_login(user_client):
    ...  # no fixture named user_client
// after
@pytest.fixture
def user_client():
    return ...

def test_login(user_client):
    ...
Defensive patterns

Strategy: validation

Validate before calling

import pytest
for p in inspect.signature(test_fn).parameters:
    assert fm.getfixturedefs(p, test_fn), f"Missing fixture {p}"

Try / catch

try:
    load_locustfile_pytest(path)
except ValueError as e:
    logging.error("Fixture problem in locustfile: %s", e)

Prevention

When it happens

Trigger: A pytest-style user test function declares a parameter that matches no fixture (not defined, autouse-only mismatch, or defined in a non-imported plugin/conftest).

Common situations: Typos in fixture names; fixtures defined in a conftest.py outside the collection scope; missing plugin; parametrized fixtures yielding multiple defs (separate error).

Related errors


AI-assisted analysis of locustio/locust@f391a716e1 (2026-08-29). Data as JSON: /api/errors/71c92364f424e5db. Report an issue: GitHub.