pytest "fixture 'X' not found" - Fix Missing Fixtures in CI
A test requested a fixture pytest can’t find. Either it’s defined somewhere not visible to that test, the conftest.py isn’t where pytest looks, or a plugin providing it isn’t installed.
What this error means
A test errors at setup with fixture X not found, often listing the available fixtures. The test function or its signature references a fixture name pytest never registered.
E fixture 'db_session' not found
> available fixtures: cache, capfd, capsys, monkeypatch, tmp_path, ...
> use 'pytest --fixtures [testpath]' for help on them.Common causes
Fixture defined out of scope
A fixture in one test file isn’t visible to another. Shared fixtures must live in a conftest.py at or above the tests that use them.
conftest.py not discovered
If conftest.py sits below the test or outside the rootdir, pytest never loads it, so its fixtures don’t exist for that test.
A fixture-providing plugin isn’t installed
Fixtures like freezer, mocker, or event_loop come from plugins (pytest-freezegun, pytest-mock, pytest-asyncio). Without the plugin installed in CI, the fixture is missing.
How to fix it
Put shared fixtures in conftest.py
Move fixtures used by multiple files into a conftest.py at the appropriate directory level.
# tests/conftest.py
import pytest
@pytest.fixture
def db_session():
...Install the plugin that provides it
pip install pytest-mock pytest-asyncio
pytest --fixtures | grep db_session # confirm it's registeredList what pytest can see
- Run
pytest --fixturesto list all available fixtures and where they come from. - Check the fixture name is spelled identically in the definition and the test signature.
- Verify the
conftest.pyis at or above the test in the directory tree.
How to prevent it
- Keep shared fixtures in a top-level
tests/conftest.py. - Declare test plugins in your dev/test dependencies and install them in CI.
- Use
pytest --fixtureswhen adding fixtures to confirm visibility.