Skip to content
Latchkey

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.

pytest
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

  1. Add pytest-django to the test dependencies.
  2. Set DJANGO_SETTINGS_MODULE in pytest config so the plugin activates.
  3. Re-run; the db fixture is now available.
Terminal
pip install pytest-django

Declare the settings so the plugin loads

pytest-django needs the settings module to bootstrap its fixtures.

pytest.ini
[pytest]
DJANGO_SETTINGS_MODULE = myproject.settings.test

How to prevent it

  • Pin pytest-django in your test requirements.
  • Set DJANGO_SETTINGS_MODULE in pytest config.
  • Run pytest --fixtures to confirm the plugin is active.

Frequently asked questions

What causes ""fixture 'db' not found""?
Without the plugin in the environment, its fixtures (db, client, ...) do not exist.
How do I fix "fixture 'db' not found"?
Install and configure pytest-django

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card