Skip to main content

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

Project description

License Latest Version https://img.shields.io/github/workflow/status/charettes/django-testdata/CI/master Coverage Status Supported Python Versions Wheel Status

Django application providing isolation for model instances created during setUpTestData.

Note: This package has been merged into Django and released in version 3.2 (see PR #12608).

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 the data is available in memory.

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 test case instance accesses. All deep copying during a test is done with a shared memo which makes sure in-memory relationships between objects are 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 wrap_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 wraps all attributes added to cls during the execution of setUpTestData() into testdata(attr, name=name).

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

from django.test import TestCase
from testdata import wrap_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.3.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

django_testdata-1.0.3-py2.py3-none-any.whl (5.6 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: django-testdata-1.0.3.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.5

File hashes

Hashes for django-testdata-1.0.3.tar.gz
Algorithm Hash digest
SHA256 5e1713fabd89e4c0b4ed10f79870b17118f4f343dc9c5d233f86b97f38419db0
MD5 e6b0fc89163e1190fc62de66df906175
BLAKE2b-256 ab2203c22fd80f55c59530e8c58c59fab9eb27f6be94613a25ea614dab08b4c4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_testdata-1.0.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.5

File hashes

Hashes for django_testdata-1.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2e782d999eff3118e07353a2d9f8d0a39b58c8f365c3efa1efe0043330731eb9
MD5 2d977b096e4b611c0f342f4f9d35b8d7
BLAKE2b-256 b36652d21686d0f492ef5b6be89e61e609526a339021634b046a14726f2a016e

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