Skip to main content

Factory Boy support for pytest.

Project description

factory_boy integration the pytest runner

https://api.travis-ci.org/pytest-dev/pytest-factoryboy.png https://pypip.in/v/pytest-factoryboy/badge.png https://coveralls.io/repos/pytest-dev/pytest-factoryboy/badge.png?branch=master 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

Example

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 isinstance(book_factory, 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.name == "PyTest for Dummies"
    assert book.author.name == "Bill Gates"

License

This software is licensed under the MIT license.

© 2015 Oleg Pidsadnyi, Anatoly Bubenkov and others

Authors

Oleg Pidsadnyi

original idea and implementation

These people have contributed to pytest-factoryboy, in alphabetical order:

Changelog

1.0.0

  • initial release

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-factoryboy-1.0.0.tar.gz (5.1 kB view details)

Uploaded Source

File details

Details for the file pytest-factoryboy-1.0.0.tar.gz.

File metadata

File hashes

Hashes for pytest-factoryboy-1.0.0.tar.gz
Algorithm Hash digest
SHA256 358da52aff92b2a4cf27cd726bd14c65ec494762290b98b5d40be3c385a0fc1c
MD5 8221c0d0cafaaa8406633456dbc89211
BLAKE2b-256 dbe62d5c962c4c58778e3f06dfbabf38d7aa194f7eba67ab83050ec3b98ca104

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page