• Stars
    star
    358
  • Rank 118,855 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created over 9 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

factory_boy integration the pytest runner

factory_boy integration with the pytest runner

Documentation Status

pytest-factoryboy makes it easy to combine factory approach to the test setup with the dependency injection, heart of the pytest fixtures.

Install pytest-factoryboy

pip install pytest-factoryboy

Concept

Library exports a function to register factories as fixtures. Fixtures are contributed to the same module where register function is called.

Factory Fixture

Factory fixtures allow using factories without importing them. The fixture name convention is to use the lowercase-underscore form of the class name.

import factory
from pytest_factoryboy import register

class AuthorFactory(factory.Factory):
    class Meta:
        model = Author


register(AuthorFactory)  # => author_factory


def test_factory_fixture(author_factory):
    author = author_factory(name="Charles Dickens")
    assert author.name == "Charles Dickens"

Model Fixture

Model fixture implements an instance of a model created by the factory. Name convention is model's lowercase-underscore class name.

import factory
from pytest_factoryboy import register

@register
class AuthorFactory(factory.Factory):
    class Meta:
        model = Author

    name = "Charles Dickens"


def test_model_fixture(author):
    assert author.name == "Charles Dickens"

Model fixtures can be registered with specific names. For example, if you address instances of some collection by the name like "first", "second" or of another parent as "other":

register(AuthorFactory)  # author
register(AuthorFactory, "second_author")  # second_author

# `register(...)` can be used as a decorator too
@register  # book
@register(_name="second_book")  # second_book
@register(_name="other_book")  # other_book, book of another author
class BookFactory(factory.Factory):
    class Meta:
        model = Book


@pytest.fixture
def other_book__author(second_author):
    """Make the relation of the second_book to another (second) author."""
    return second_author

Attributes are Fixtures

There are fixtures created for factory attributes. Attribute names are prefixed with the model fixture name and double underscore (similar to the convention used by factory_boy).

@pytest.mark.parametrize("author__name", ["Bill Gates"])
def test_model_fixture(author):
    assert author.name == "Bill Gates"

SubFactory

Sub-factory attribute points to the model fixture of the sub-factory. Attributes of sub-factories are injected as dependencies to the model fixture and can be overridden via the parametrization.

Related Factory

Related factory attribute points to the model fixture of the related factory. Attributes of related factories are injected as dependencies to the model fixture and can be overridden via the parametrization.

post-generation

Post-generation attribute fixture implements only the extracted value for the post generation function.

Integration

An example of factory_boy and pytest integration.

# factories/__init__.py

import factory
from faker import Factory as FakerFactory

faker = FakerFactory.create()


class AuthorFactory(factory.django.DjangoModelFactory):
    """Author factory."""

    name = factory.LazyAttribute(lambda x: faker.name())

    class Meta:
        model = 'app.Author'


class BookFactory(factory.django.DjangoModelFactory):
    """Book factory."""

    title = factory.LazyAttribute(lambda x: faker.sentence(nb_words=4))

    class Meta:
        model = 'app.Book'

    author = factory.SubFactory(AuthorFactory)
# tests/conftest.py

from pytest_factoryboy import register

from factories import AuthorFactory, BookFactory

register(AuthorFactory)
register(BookFactory)
# tests/test_models.py

from app.models import Book
from factories import BookFactory


def test_book_factory(book_factory):
    """Factories become fixtures automatically."""
    assert book_factory is BookFactory


def test_book(book):
    """Instances become fixtures automatically."""
    assert isinstance(book, Book)


@pytest.mark.parametrize("book__title", ["PyTest for Dummies"])
@pytest.mark.parametrize("author__name", ["Bill Gates"])
def test_parametrized(book):
    """You can set any factory attribute as a fixture using naming convention."""
    assert book.title == "PyTest for Dummies"
    assert book.author.name == "Bill Gates"

Fixture partial specialization

There is a possibility to pass keyword parameters in order to override factory attribute values during fixture registration. This comes in handy when your test case is requesting a lot of fixture flavors. Too much for the regular pytest parametrization. In this case, you can register fixture flavors in the local test module and specify value deviations inside register function calls.

register(AuthorFactory, "male_author", gender="M", name="John Doe")
register(AuthorFactory, "female_author", gender="F")


@pytest.fixture
def female_author__name():
    """Override female author name as a separate fixture."""
    return "Jane Doe"


@pytest.mark.parametrize("male_author__age", [42])  # Override even more
def test_partial(male_author, female_author):
    """Test fixture partial specialization."""
    assert male_author.gender == "M"
    assert male_author.name == "John Doe"
    assert male_author.age == 42

    assert female_author.gender == "F"
    assert female_author.name == "Jane Doe"

Fixture attributes

Sometimes it is necessary to pass an instance of another fixture as an attribute value to the factory. It is possible to override the generated attribute fixture where desired values can be requested as fixture dependencies. There is also a lazy wrapper for the fixture that can be used in the parametrization without defining fixtures in a module.

LazyFixture constructor accepts either existing fixture name or callable with dependencies:

import pytest
from pytest_factoryboy import register, LazyFixture


@pytest.mark.parametrize("book__author", [LazyFixture("another_author")])
def test_lazy_fixture_name(book, another_author):
    """Test that book author is replaced with another author by fixture name."""
    assert book.author == another_author


@pytest.mark.parametrize("book__author", [LazyFixture(lambda another_author: another_author)])
def test_lazy_fixture_callable(book, another_author):
    """Test that book author is replaced with another author by callable."""
    assert book.author == another_author


# Can also be used in the partial specialization during the registration.
register(BookFactory, "another_book", author=LazyFixture("another_author"))

Generic container classes as models

It's often useful to create factories for dict or other common generic container classes. In that case, you should wrap the container class around named_model(...), so that pytest-factoryboy can correctly determine the model name when using it in a SubFactory or RelatedFactory.

Pytest-factoryboy will otherwise raise a warning.

For example:

import factory
from pytest_factoryboy import named_model, register

@register
class JSONPayload(factory.Factory):
    class Meta:
        model = named_model("JSONPayload", dict)

    name = "foo"


def test_foo(json_payload):
    assert json_payload.name == "foo"

As a bonus, factory is automatically registering the json_payload fixture (rather than dict), so there is no need to override @register(_name="json_payload")).

Post-generation dependencies

Unlike factory_boy which binds related objects using an internal container to store results of lazy evaluations, pytest-factoryboy relies on the PyTest request.

Circular dependencies between objects can be resolved using post-generation hooks/related factories in combination with passing the SelfAttribute, but in the case of PyTest request fixture functions have to return values in order to be cached in the request and to become available to other fixtures.

That's why evaluation of the post-generation declaration in pytest-factoryboy is deferred until calling the test function. This solves circular dependency resolution for situations like:

o->[ A ]-->[ B ]<--[ C ]-o
|                        |
o----(C depends on A)----o

On the other hand, deferring the evaluation of post-generation declarations evaluation makes their result unavailable during the generation of objects that are not in the circular dependency, but they rely on the post-generation action.

pytest-factoryboy is trying to detect cycles and resolve post-generation dependencies automatically.

from pytest_factoryboy import register


class Foo(object):
    def __init__(self, value):
        self.value = value


class Bar(object):
    def __init__(self, foo):
        self.foo = foo


@register
class FooFactory(factory.Factory):
    """Foo factory."""

    class Meta:
        model = Foo

    value = 0

    @factory.post_generation
    def set1(foo, create, value, **kwargs):
        foo.value = 1


class BarFactory(factory.Factory):
    """Bar factory."""

    foo = factory.SubFactory(FooFactory)

    @classmethod
    def _create(cls, model_class, foo):
        assert foo.value == 1  # Assert that set1 is evaluated before object generation
        return super(BarFactory, cls)._create(model_class, foo=foo)

    class Meta:
        model = Bar


register(BarFactory, "bar")
"""Forces 'set1' to be evaluated first."""


def test_depends_on_set1(bar):
    """Test that post-generation hooks are done and the value is 2."""
    assert depends_on_1.foo.value == 1

Hooks

pytest-factoryboy exposes several pytest hooks which might be helpful for e.g. controlling database transaction, for reporting etc:

  • pytest_factoryboy_done(request) - Called after all factory-based fixtures and their post-generation actions have been evaluated.

License

This software is licensed under the MIT license.

Β© 2015 Oleg Pidsadnyi, Anatoly Bubenkov and others

More Repositories

1

pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
Python
11,750
star
2

pytest-testinfra

Testinfra test your infrastructures
Python
2,359
star
3

pytest-mock

Thin-wrapper around the mock package for easier use with pytest
Python
1,804
star
4

pytest-cov

Coverage plugin for pytest.
Python
1,722
star
5

pytest-xdist

pytest plugin for distributed testing and loop-on-failures testing modes.
Python
1,421
star
6

pytest-asyncio

Asyncio support for pytest
Python
1,378
star
7

pytest-django

A Django plugin for pytest.
Python
1,350
star
8

pytest-bdd

BDD library for the py.test runner
Python
1,281
star
9

pluggy

A minimalist production ready plugin system
Python
1,235
star
10

pytest-html

Plugin for generating HTML reports for pytest results
Python
689
star
11

pyfakefs

Provides a fake file system that mocks the Python file system modules.
Python
635
star
12

pytest-randomly

🎲 Pytest plugin to randomly order tests and control random.seed
Python
600
star
13

pytest-flask

A set of pytest fixtures to test Flask applications
Python
483
star
14

pytest-qt

pytest plugin for Qt (PyQt5/PyQt6 and PySide2/PySide6) application testing
Python
401
star
15

pytest-rerunfailures

a pytest plugin that re-runs failed tests up to -n times to eliminate flakey failures
Python
373
star
16

pytest-selenium

Plugin for running Selenium with pytest
Python
331
star
17

cookiecutter-pytest-plugin

A Cookiecutter template for pytest plugins πŸ’»
Python
292
star
18

pytest-splinter

pytest splinter and selenium integration for anyone interested in browser interaction in tests
Python
253
star
19

pytest-describe

Describe-style plugin for the pytest framework
Python
208
star
20

pytest-timeout

Python
206
star
21

pytest-subtests

unittest subTest() support and subtests fixture
Python
199
star
22

pytest-repeat

pytest plugin for repeating test execution
Python
168
star
23

pytest-order

pytest plugin that allows to customize the test execution order
Python
158
star
24

pytest-instafail

py.test plugin to show failures instantly
Python
135
star
25

unittest2pytest

helps rewriting Python `unittest` test-cases into `pytest` test-cases
Python
128
star
26

pytest-env

pytest plugin to set environment variables in pytest.ini or pyproject.toml file
Python
128
star
27

pytest-github-actions-annotate-failures

Pytest plugin to annotate failed tests with a workflow command for GitHub Actions
Python
127
star
28

pytest-cpp

Use pytest's runner to discover and execute C++ tests
C++
117
star
29

pytest-xprocess

pytest external process plugin
Python
97
star
30

pytest-reportlog

Replacement for the --resultlog option, focused in simplicity and extensibility
Python
89
star
31

execnet

distributed Python deployment and communication
Python
78
star
32

pytest-variables

Plugin for providing variables to pytest tests/fixtures
Python
74
star
33

pytest-play

pytest plugin that let you automate actions and assertions with test metrics reporting executing plain YAML files
Python
68
star
34

py

Python development support library (note: maintenance only)
Python
67
star
35

pytest-print

pytest-print adds the printer fixture you can use to print messages to the user (directly to the pytest runner, not stdout)
Python
67
star
36

pytest-messenger

Pytest-messenger report plugin for all popular messengers like: Slack, DingTalk, Telegram
Python
67
star
37

pytest-random-order

pytest plugin to randomise the order of tests with some control over the randomness
Python
65
star
38

pytest-forked

extracted --boxed from pytest-xdist to ensure backward compat
Python
62
star
39

pytest-mimesis

Mimesis integration with the pytest test runner. This plugin provider useful fixtures based on providers from Mimesis.
Python
62
star
40

pytest-services

Collection of fixtures and utility functions to run service processes for your tests
Python
57
star
41

pytest-runner

Python
56
star
42

pytest-metadata

Plugin for accessing test session metadata
Python
56
star
43

apipkg

Python
55
star
44

iniconfig

Python
50
star
45

pytest-twisted

test twisted code with pytest
Python
46
star
46

pytest-freezer

Pytest plugin providing a fixture interface for spulec/freezegun
Python
45
star
47

pytest-incremental

py-test plugin: an incremental test runner
Python
42
star
48

pytest-stress

A Pytest plugin that allows you to loop tests for a user defined amount of time.
Python
41
star
49

nose2pytest

Scripts to convert Python Nose tests to PyTest
Python
38
star
50

pytest-base-url

pytest plugin for URL based tests
Python
38
star
51

pytest-faker

faker integration the pytest test runner
Python
38
star
52

pytest-fixture-tools

Pytest fixture tools
Python
36
star
53

pytest-cloud

Distributed tests planner plugin for pytest testing framework.
Python
35
star
54

plugincompat

Test execution and compatibility checks for pytest plugins
CSS
34
star
55

pytest-faulthandler

py.test plugin that activates the fault handler module during testing
Python
27
star
56

pytest-localserver

py.test plugin to test server connections locally. This repository was migrated from Bitbucket.
Python
23
star
57

pygments-pytest

A pygments lexer for pytest output
Python
23
star
58

pytest-echo

pytest plugin to dump environment variables, package version and generic attributes.
Python
22
star
59

pytest-inline

pytest-inline is a pytest plugin for writing inline tests.
Python
16
star
60

pytest-nunit

An Nunit output plugin for Pytest
Python
10
star
61

pytest-plus

pytest-plus adds new features to pytest
Python
10
star
62

design

Graphic design for Pytest project
9
star
63

pytest-iam

A fully functional OAUTH2 / OpenID Connect (OIDC) server to be used in your testsuite
Python
7
star
64

sprint

pytest development sprint 2024
7
star
65

pytest-bpdb

pytest plugin for dropping to bpdb on test failures
Python
6
star
66

blog.pytest.org

Repository for the official pytest blog
Python
4
star
67

pytest-tcpclient

pytest mocking of TCP clients
Python
3
star
68

regendoc

Python
2
star
69

meta

Used for generic pytest organization issues, stuff that can impact multiple projects.
2
star
70

pytest-libfaketime

Prepare pytest for python-libfaketime - https://github.com/simon-weber/python-libfaketime
Python
2
star
71

pytest-talks

public pytest talks and workshops - meant to help user groups spin up talks and workshops
HTML
1
star