Solution details

The first solution we will take a look at is what is given in the Django documentation itself, that is, test fixtures. Here, a test fixture is a file that contains a set of data that can be imported into your database to bring it to a known state. Typically, they are YAML or JSON files previously exported from the same database when it had some data.

For example, consider the following test case, which uses a test fixture:

from django.test import TestCase 
 
class PostTestCase(TestCase): 
    fixtures = ['posts'] 
 
    def setUp(self): 
        # Create additional common objects 
        pass 
 
    def test_some_post_functionality(self): 
        # By now fixtures and setUp() objects are loaded 
        pass 

Before setUp() gets called in each test case, the specified fixture, ...

Get Django Design Patterns and Best Practices - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.