skillfed

pytest-factoryboy

Factory Boy support for pytest.

pytest-factoryboy v2.8.1 2.8M downloads/30d#2,873 on PyPI399
Permissive license MIT Active released

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-factoryboy

uv

uv add pytest-factoryboy

poetry

poetry add pytest-factoryboy

Installing 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

Development Status :: 6 - MatureIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseOperating System :: MacOS :: MacOS XOperating System :: Microsoft :: WindowsOperating System :: POSIXProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.9Topic :: Software Development :: LibrariesTopic :: Software Development :: TestingTopic :: Utilities

Tags

pytest factory fixturesfactory_boy pytest integrationtest data factory fixturespytest dependency injectionfactory pattern testingpytest model fixturesparametrized test factories
pytest-plugintest-fixturesfactory-pattern

More Libraries packages