Git Product home page Git Product logo

pytest-django-factories's Introduction

pytest-django-factories

Python package

Factories for your Django models that can be used as Pytest fixtures. Re-use them implicitly for instantiating related objects without much boilerplate code.

>>> book = book_factory(title="Calvin & Hobbes", author__name="Bill Watterson")
>>> book
<Book: Calvin & Hobbes>
>>> book.author.name
<Author: Bill Watterson>

Introduction

Write a fixture for your Django model by using the Factory class:

# conftest.py
import pytest
from django_factories import Factory
from .models import Author

@pytest.fixture
def author_factory(request):
    factory = Factory(Author)
    return factory(request)
# tests.py
def test_function(author_factory):
    author = author_factory(name="Bill Watterson")
    assert author.name

๐Ÿ““ Note
The request passed to the fixture function is a pytest fixture itself and provides information about the test function requesting the fixture. See pytest documentation.

Default values

A plain Factory will, of course, instantiate the object with the defaults given at the model definition. If you want to set defaults for the factory specifically, you can assign pass them to the Factory:

@pytest.fixture
def author_factory(request):
    defaults = {
        "first_name": "William",
        "last_name": "Watterson",
        "birthdate": "1958-07-05",
    }
    return Factory(Author, **defaults)(request)

Related objects

If you want to test a model which depends on another object being present, the Factory class will try to look up a matching factory fixture for that ForeignKey field and create the related object automatically for you. Attributes for the related object can be specified in the same double-underscore syntax that you're familiar with from Django's queryset lookups:

@pytest.fixture
def author_factory(request):
    return Factory(Author)(request)

@pytest.fixture
def book_factory(request):
    return Factory(Book)(request)

def test(book_factory):
    book = book_factory(
        author__first_name="Astrid", 
        author__last_name="Lindgren"
    )

This only works if there is a factory fixture available to create the related object. Factory will look for a fixture named <field>_factory. If you have a fixture that you named differently (or you have multiple fixtures for that particular model), you can specify a custom fixture name:

@pytest.fixture
def book_factory(request):
    return Factory(
        Book, 
        author=SubFactory("my_author_fixture")
    )(request)

Passing object instances as keyword arguments instead works as well, of course:

book = book_factory(
    author=Author(
        first_name="Astrid", 
        last_name="Lindgren"
    )
)

Database usage

You can use Factory to instantiate objects in memory and also to create them in the database directly via Model.objects.create(). If your test function is marked to use the database, the objects will be saved to the database. Unmarked tests will only create objects in memory.

Installation

pip install pytest-django-factories

Contributing

All contributions are welcome. To check out the development version and run the tests, follow these steps:

git clone https://github.com/jnns/pytest-django-factories.git
cd pytest-django-factories
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
pytest

pytest-django-factories's People

Contributors

jnns avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.