pytest-factoryboy
Factory Boy support for pytest.
What it is and what it does
pytest-factoryboy bridges factory_boy and pytest by automatically converting factory classes into pytest fixtures. Instead of manually instantiating test objects or writing fixture boilerplate, you register a factory once and pytest injects instances directly into test functions. The package creates both model fixtures (instances) and factory fixtures (the factory itself), plus attribute fixtures that let you override individual fields via pytest parametrization.
The integration handles factory_boy's advanced patterns: SubFactory relationships become fixture dependencies, related factories are injected automatically, and post-generation hooks work as fixtures. You can register the same factory multiple times with different names, specialize fixtures with keyword arguments during registration, and use LazyFixture to inject other fixtures as attribute values. This approach reduces boilerplate and makes test setup declarative and composable.
Use it for:
- Create reusable test fixtures for model objects without writing fixture functions.
- Parametrize test object attributes via pytest.mark.parametrize without modifying factory definitions.
- Build complex object graphs with SubFactory relationships that automatically resolve as fixture dependencies.
- Register multiple instances of the same factory with different names for testing relationships between objects.
- Override factory attributes at registration time to create specialized fixture variants for different test scenarios.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Integrates factory_boy with pytest to register factories as fixtures, enabling dependency injection and parametrization of test object creation.
Yes. This is a mature, actively maintained package (first release 2015-04-09, latest 2025-07-01) with no known vulnerabilities, low install friction, and permissive MIT licensing. It solves a real pain point—combining factory_boy's declarative test data with pytest's fixture system—and is widely used (top 5000 PyPI packages). Install it if you use factory_boy and pytest together.
Install
pytest-factoryboy on PyPI
pip
pip install pytest-factoryboyuv
uv add pytest-factoryboypoetry
poetry add pytest-factoryboyInstalling pytest-factoryboy
Before you install
Low friction: pure Python wheel with five runtime dependencies (factory_boy, inflection, packaging, pytest, typing_extensions), all common and stable. Active maintenance with latest release 2025-07-01 and no known vulnerabilities.
License in practice
MIT license is permissive; you can use, modify, and distribute this package freely in commercial and open-source projects with minimal restrictions.
Quickstart
pip install pytest-factoryboy
from pytest_factoryboy import register
import factory
@register
class AuthorFactory(factory.Factory):
class Meta:
model = Author
name = "Charles Dickens"
def test_author(author):
assert author.name == "Charles Dickens"
Requires Python 3.9 or later; factory_boy and pytest must be installed.
Verify before relying
- Whether the package supports factory_boy's full feature set (SubFactory, RelatedFactory, post-generation) without limitations.
- Performance characteristics when registering many factories or running parametrized tests at scale.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.9) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 5 — factory_boy, inflection, packaging, pytest, typing_extensions |
| Maintenance | actively maintained — 409 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 2,833,269/month — #2,873 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pytest_factoryboy-2.8.1-py3-none-any.whl
Tags
More Libraries packages
urllib3 is an HTTP client library that provides…
permissive · top 100 on PyPI
requestsRequests is a Python HTTP library that…
permissive · top 100 on PyPI
pluggyPluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
python-dateutilProvides parsing, arithmetic, and recurrence…
permissive · top 100 on PyPI
sixSix provides utility functions to write Python…
permissive · top 100 on PyPI
pytestpytest is a testing framework that lets you…
permissive · top 100 on PyPI
factory-boyfactory_boy replaces static test fixtures with…
permissive · top 1,000 on PyPI
pytest-docker-toolsProvides pytest fixture factories for…
permissive · top 5,000 on PyPI
pytest-fakerProvides pytest fixtures that integrate the…
permissive · top 15,000 on PyPI
async-factory-boyExtends factory_boy to generate test fixtures…
permissive · top 15,000 on PyPI
wagtail-factoriesProvides factory-boy classes for generating…
permissive · top 15,000 on PyPI
pytest-lazy-fixturesAllows you to reference pytest fixtures…
permissive · top 5,000 on PyPI
model-mommyModel Mommy generates Django model instances…
permissive · top 15,000 on PyPI
pytest-lazy-fixtureAllows you to use pytest fixtures directly as…
permissive · top 15,000 on PyPI
pytest-djangopytest-django integrates Django with pytest,…
permissive · top 1,000 on PyPI
phx-class-registryImplements the Registry and Factory patterns to…
permissive · top 15,000 on PyPI