Registry / testing / pytest-factoryboy

pytest-factoryboy

JSON →
library2.8.1pypypi✓ verified 25d ago

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-factoryboy
INSTALL
IMPORT
SIG · PYTEST-FACTORYBOY
P
pytest-factoryboy
testingpythonv2.8.1
Install
4.2s avg
Import
Disk
55MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.8.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 56.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 4.2s · import 0.000s · 57MB
55MB installed
● package 55MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

factory (fixture)
def test_something(factory): ...
from pytest_factoryboy import factory
The `factory` fixture is automatically discovered and injected by pytest. You do not need to import it explicitly.

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.

import pytest import factory # Assume a simple data model for demonstration class User: def __init__(self, username, email): self.username = username self.email = email def __repr__(self): return f"User(username='{self.username}', email='{self.email}')" # Define your factory_boy factory (e.g., in factories.py or conftest.py) class UserFactory(factory.Factory): class Meta: model = User # Link to your actual model/class username = factory.Sequence(lambda n: f'user{n}') email = factory.LazyAttribute(lambda o: f'{o.username}@example.com') # In your conftest.py or test file, define a fixture that uses `pytest-factoryboy`'s `factory` fixture @pytest.fixture def user_factory(factory): # `factory` is provided by pytest-factoryboy, it returns a callable bound to UserFactory return factory(UserFactory) # Now use your `user_factory` in tests def test_create_single_user(user_factory): user = user_factory() assert user.username.startswith('user') assert user.email.endswith('@example.com') assert isinstance(user, User) def test_create_multiple_users(user_factory): users = user_factory.create_batch(3) assert len(users) == 3 assert all(isinstance(u, User) for u in users) def test_create_user_with_override(user_factory): user = user_factory(username='admin_user', email='admin@example.com') assert user.username == 'admin_user' assert user.email == 'admin@example.com'
Debug
Known issues
breakingThe `fb` fixture, common in older versions, was removed in `pytest-factoryboy` 2.0.0. Attempting to use it will result in a `FixtureLookupError`.
fix
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)()`.
affects: >=2.0.0
breakingFactory resolution and discovery mechanisms underwent significant changes in `pytest-factoryboy` 2.0.0. Tests relying on implicit factory discovery or specific naming conventions from 1.x might break.
fix
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.
affects: 1.x to 2.x
gotchaFactories defined directly within test functions or nested scopes might not be properly discovered or utilized by `pytest-factoryboy`.
fix
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.
affects: All
Upgrade
Version history
2.8.1latest on PyPI · released Jul 1, 2025
Audit
Dependencies
pytestrequiredProvides the testing framework for the plugin.
factory_boyrequiredProvides the core factory definition capabilities.
pythonrequiredRequires Python 3.9 or newer.
Agent activity
10 hits · last 30 days
node
8
Resources
pytest-factoryboy — pip install pytest-factoryboy · libregistry