Skip to main content

Django application providing isolation for model instances created during `setUpTestData`.

Project description

License Latest Version Build Status Coverage Status Supported Python Versions Wheel Status

Django application providing isolation for model instances created during setUpTestData.

Installation

pip install django-testdata

Motivation

Django 1.8 introduced TestCase.setUpTestData to allow costly generation of model fixtures to be executed only once per test class in order to speed up testcase instances execution.

One gotcha of setUpTestData though is that test instances all share the same model instances and have to be careful not to alter them to prevent breaking test isolation. Per Django’s documentation:

Be careful not to modify any objects created in setUpTestData() in your
test methods. Modifications to in-memory objects from setup work done at
the class level will persist between test methods. If you do need to modify
them, you could reload them in the setUp() method with refresh_from_db(),
for example.

Reloading objects in setUp() certainly works but it kind of defeats the purpose of avoiding database hits to speed up tests execution in the first place. It makes little sense to fetch model instances from the database given all

This package offers a different alternative to work around this quirk of setUpTestData. Instead of reloading objects from the database the model instances assigned as class attributes during setUpTestData are lazily deep copied on testcase instance accesses from their original definition. All of deep copying is done by sharing a memo which makes sure in-memory relationships between objects is preserved.

Usage

The test data can be either wrapped manually by using testdata.

from django.test import TestCase
from testdata import testdata

from .models import Author, Book

class BookTests(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.author = testdata(Author.objects.create(
            name='Milan Kundera',
        ))
        cls.book = testdata(cls.author.books.create(
            title='Nesnesitelná lehkost bytí',
        ))

Or automatically by using the wrap_testdata decorator.

from django.test import TestCase
from testdata import testdata

from .models import Author, Book

class BookTests(TestCase):
    @classmethod
    @wrap_testdata
    def setUpTestData(cls):
        cls.author = Author.objects.create(
            name='Milan Kundera',
        )
        cls.book = cls.author.books.create(
            title='Nesnesitelná lehkost bytí',
        )

Under the hood wrap_testdata simply wraps all attributes added to cls during the execution of setUpTestData() into testdata(attr, name=name) which has also the nice side effect of speeding subsequent accesses.

Once test data is wrapped the testcase instances methods can alter objects retrieved from self without worrying about cross-tests isolation.

from django.test import TestCase
from testdata import testdata

from .models import Author, Book

class BookTests(TestCase):
    @classmethod
    @wrap_testdata
    def setUpTestData(cls):
        cls.author = Author.objects.create(
            name='Milan Kundera',
        )
        cls.book = cls.author.books.create(
            title='Nesnesitelná lehkost bytí',
        )

    def test_book_name_english(self):
        self.assertEqual(self.book.title, 'Nesnesitelná lehkost bytí')
        self.book.title = 'The Unbearable Lightness of Being'
        self.book.save()

    def test_book_name_french(self):
        self.assertEqual(self.book.title, 'Nesnesitelná lehkost bytí')
        self.book.title = "L'Insoutenable Légèreté de l'être"
        self.book.save()

Project details


Download files

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

Source Distribution

django-testdata-1.0.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

django_testdata-1.0.1-py2.py3-none-any.whl (5.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-testdata-1.0.1.tar.gz.

File metadata

  • Download URL: django-testdata-1.0.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/2.7.15rc1

File hashes

Hashes for django-testdata-1.0.1.tar.gz
Algorithm Hash digest
SHA256 db134f2b0ecdbbc667a9a781fbf8bbcc3538c16d4fd7722362ae976c272579a6
MD5 1cfab4a4df947917f27a10eddadad5ac
BLAKE2b-256 ebe804289f751c0930d47a576dae4aa707d398112290ed3e8354b3da0dc78b0f

See more details on using hashes here.

File details

Details for the file django_testdata-1.0.1-py2.py3-none-any.whl.

File metadata

  • Download URL: django_testdata-1.0.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/2.7.15rc1

File hashes

Hashes for django_testdata-1.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7b72b7d7d9b7dcdd27e48fedf80de748b0b0fe1b4e37ad7a6da57bc4746685d6
MD5 68d48ef70a0bbf34fd67dfb2fba295d3
BLAKE2b-256 9e4e9fc91762a5c2a5eac060705d4b8f84db69bafb6afcc778200cc4977c79f0

See more details on using hashes here.

Supported by

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