pytest "Fixture X called directly" in CI
A function decorated with @pytest.fixture was called like a regular function. pytest manages fixture setup/teardown by injecting them as arguments, so calling one directly is an error rather than a value.
What this error means
A test errors with "Failed: Fixture \"X\" called directly. Fixtures are not meant to be called directly, but are created automatically when test functions request them as parameters."
Failed: Fixture "client" called directly. Fixtures are not meant to be called
directly, but are created automatically when test functions request them as parameters.Common causes
Calling the fixture function in the test body
Writing client() invokes the fixture directly instead of receiving the value pytest would inject.
Reusing a fixture from another fixture by calling it
A fixture that needs another should request it as a parameter, not call it.
How to fix it
Request the fixture as a parameter
Add the fixture name to the test (or fixture) signature so pytest injects the value.
def test_get(client): # requested, not called
assert client.get("/health").status_code == 200Extract shared logic into a plain helper
If you need to call it like a function, move the logic to a non-fixture helper and have the fixture wrap it.
def make_client():
return TestClient(app)
@pytest.fixture
def client():
return make_client()How to prevent it
- Always request fixtures via parameters, never call them.
- Put reusable logic in plain helpers that fixtures wrap.
- Have dependent fixtures request other fixtures as parameters.