Skip to content
Latchkey

Python "factory_boy DjangoModelFactory not registered" in CI

With pytest-factoryboy, a factory must be register()-ed to expose a fixture. If it is not registered (or its model import fails), pytest reports the factory fixture as not found and the test errors during setup.

What this error means

A test fails with "fixture 'user_factory' not found" or factory_boy errors that the model/factory is not registered when the test requests a factory fixture.

pytest
E       fixture 'user_factory' not found

Common causes

The factory was never registered with pytest-factoryboy

No register(UserFactory) call exists, so no user_factory fixture is generated.

The factory module is not imported in conftest

Registration code lives in a module that conftest does not import, so it never runs.

How to fix it

Register the factory in conftest

  1. Call register(UserFactory) in conftest.py to create the user_factory fixture.
  2. Ensure the factories module is importable and imported where register runs.
  3. Confirm the Django model the factory targets imports cleanly in CI.
conftest.py
from pytest_factoryboy import register
from tests.factories import UserFactory

register(UserFactory)  # exposes the 'user_factory' fixture

How to prevent it

  • Register every factory you intend to use as a fixture.
  • Keep factory registration in a conftest that pytest loads.
  • Verify model imports succeed in the CI settings.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →