pytest-factoryboy is a pytest plugin that integrates factory_boy for easily creating test data within your pytest tests. It provides fixtures for generating instances of your factory_boy factories, streamlining setup for database models or complex objects. As of version 2.8.1, it's actively maintained with a stable release cadence.
pip install pytest-factoryboyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a `factory_boy` factory for a simple `User` class, then use the `pytest-factoryboy` provided `factory` fixture to create a specific `user_factory` fixture. This `user_factory` can then be used in your tests to create single instances, batches, or instances with overridden attributes.
Migrate your tests to use the `factory` fixture instead. For example, change `def my_fixture(fb):` to `def my_fixture(factory):` and `fb.MyFactory()` to `factory(MyFactory)()`.
Ensure your factories are explicitly linked or passed to the `factory` fixture, e.g., `factory(UserFactory)`. Review the official documentation for the updated factory discovery process if you're experiencing issues with factories not being found.
Define your `factory_boy` factories in `conftest.py` or dedicated `factories.py` modules that are importable and visible to your pytest suite. This ensures consistent discovery and reuse across your tests.