pytest fixture not found error: how to fix
Fixes pytest 'fixture not found' errors: naming, scope, and conftest placement. Use when pytest cannot find a fixture. Not for fixture logic errors.
TL;DR
The fixture is misnamed, defined in the wrong conftest, or shadowed. Check the name matches exactly, the conftest is at the right level, and no plugin fixture overrides it.
Error
E fixture 'db_session' not found
> available fixtures: cache, capfd, ...Steps
- Compare the requested name with the definition character by character. Expected: typos found fast.
- Check conftest.py placement: fixtures apply to their directory and below. Expected: the conftest covers the test file.
- Check for shadowing: a local fixture or plugin with the same name wins. Expected: the winner identified.
- Verify the fixture is decorated with
@pytest.fixture. Expected: decoration present. - Re-run with
--fixturesto list what pytest sees. Expected: your fixture appears.
When to use
fixture 'x' not foundat collection.- After moving test files between directories.
When not to use
- The fixture exists but errors inside (different bug).
- Plugin fixtures missing (install the plugin).
Tool compatibility
- pytest 7/8; conftest.py discovery rules.
Variant phrasings
pytest fixture not available
Same error; check scope and naming.
fixture called directly (newer pytest)
Fixtures cannot be called directly; request them as arguments.
Why it happens
Fixture discovery is directory-scoped and name-exact. Moves, renames, and typos break it silently until collection.
Edge cases
conftest.pyin the rootdir applies everywhere; nested ones override.- Fixture name clashes with test function arguments confuse the error.
--import-modechanges conftest discovery; keep it consistent.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_xJ9LUm1XPs0Vt6XISyeu1g
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.