{"id":3241,"library":"pytest-factoryboy","title":"pytest-factoryboy","description":"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.","status":"active","version":"2.8.1","language":"en","source_language":"en","source_url":"https://github.com/pytest-dev/pytest-factoryboy","tags":["pytest","testing","factories","factoryboy","fixtures"],"install":[{"cmd":"pip install pytest-factoryboy","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Provides the testing framework for the plugin.","package":"pytest"},{"reason":"Provides the core factory definition capabilities.","package":"factory_boy"},{"reason":"Requires Python 3.9 or newer.","package":"python","optional":false}],"imports":[{"note":"The `factory` fixture is automatically discovered and injected by pytest. You do not need to import it explicitly.","wrong":"from pytest_factoryboy import factory","symbol":"factory (fixture)","correct":"def test_something(factory): ..."}],"quickstart":{"code":"import pytest\nimport factory\n\n# Assume a simple data model for demonstration\nclass User:\n    def __init__(self, username, email):\n        self.username = username\n        self.email = email\n\n    def __repr__(self):\n        return f\"User(username='{self.username}', email='{self.email}')\"\n\n# Define your factory_boy factory (e.g., in factories.py or conftest.py)\nclass UserFactory(factory.Factory):\n    class Meta:\n        model = User # Link to your actual model/class\n\n    username = factory.Sequence(lambda n: f'user{n}')\n    email = factory.LazyAttribute(lambda o: f'{o.username}@example.com')\n\n# In your conftest.py or test file, define a fixture that uses `pytest-factoryboy`'s `factory` fixture\n@pytest.fixture\ndef user_factory(factory):\n    # `factory` is provided by pytest-factoryboy, it returns a callable bound to UserFactory\n    return factory(UserFactory)\n\n# Now use your `user_factory` in tests\ndef test_create_single_user(user_factory):\n    user = user_factory()\n    assert user.username.startswith('user')\n    assert user.email.endswith('@example.com')\n    assert isinstance(user, User)\n\ndef test_create_multiple_users(user_factory):\n    users = user_factory.create_batch(3)\n    assert len(users) == 3\n    assert all(isinstance(u, User) for u in users)\n\ndef test_create_user_with_override(user_factory):\n    user = user_factory(username='admin_user', email='admin@example.com')\n    assert user.username == 'admin_user'\n    assert user.email == 'admin@example.com'","lang":"python","description":"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."},"warnings":[{"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)()`.","message":"The `fb` fixture, common in older versions, was removed in `pytest-factoryboy` 2.0.0. Attempting to use it will result in a `FixtureLookupError`.","severity":"breaking","affected_versions":">=2.0.0"},{"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.","message":"Factory 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.","severity":"breaking","affected_versions":"1.x to 2.x"},{"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.","message":"Factories defined directly within test functions or nested scopes might not be properly discovered or utilized by `pytest-factoryboy`.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}