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 foundCommon 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
- Call
register(UserFactory)in conftest.py to create theuser_factoryfixture. - Ensure the factories module is importable and imported where register runs.
- 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' fixtureHow 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
Python "freezegun ImportError / not installed" in CIFix "ModuleNotFoundError: No module named 'freezegun'" in CI - the test imports freeze_time but freezegun is…
Python "sqlalchemy.exc.IntegrityError: UNIQUE constraint failed" in CIFix "sqlalchemy.exc.IntegrityError: UNIQUE constraint failed" in tests in CI - a row violated a unique constr…
pytest "fixture not found" in CIFix pytest "fixture 'X' not found" in CI - a test requested a fixture pytest could not locate, usually a conf…