Factory Boy support for pytest.
Project description
factory_boy integration the pytest runner
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
Changelog
1.0.1
use inflection package to convert camel case to underscore (bubenkoff)
1.0.0
initial release
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file pytest-factoryboy-1.0.1.tar.gz.
File metadata
- Download URL: pytest-factoryboy-1.0.1.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e27eea3c254b31ff6211b741734038b809975f20710823619924f547363e747
|
|
| MD5 |
75b99bb3b476760856e9b7f6d24fe829
|
|
| BLAKE2b-256 |
9b1d5d4bc06a8c4b8ea098c42a229f25666ef2d3b8d8bf23abe9cd4d6dccb21b
|