pytest "fixture 'db' not found" (pytest-django) in CI
The db fixture is provided by the pytest-django plugin. If the plugin is not installed (or was disabled), pytest does not know the fixture and fails any test that requests it.
What this error means
A test errors during setup with "fixture 'db' not found" and lists the available fixtures, none of which include db - a sign pytest-django is missing or inactive.
E fixture 'db' not found
> available fixtures: cache, capfd, capsys, doctest_namespace, ...
> use 'pytest --fixtures' for help on them.Common causes
pytest-django is not installed
Without the plugin in the environment, its fixtures (db, client, ...) do not exist.
The plugin is disabled
A -p no:django flag or a missing DJANGO_SETTINGS_MODULE can prevent pytest-django from activating.
How to fix it
Install and configure pytest-django
- Add
pytest-djangoto the test dependencies. - Set
DJANGO_SETTINGS_MODULEin pytest config so the plugin activates. - Re-run; the
dbfixture is now available.
pip install pytest-djangoDeclare the settings so the plugin loads
pytest-django needs the settings module to bootstrap its fixtures.
[pytest]
DJANGO_SETTINGS_MODULE = myproject.settings.testHow to prevent it
- Pin
pytest-djangoin your test requirements. - Set
DJANGO_SETTINGS_MODULEin pytest config. - Run
pytest --fixturesto confirm the plugin is active.