Search code examples
djangodjango-modelsdjango-testing

Django Setting up a test from a populated database?


I've been running most of my unit tests from fresh instances of my model, however I would now like to test on my populated model as it's non-trivial to repopulate my database. Is this possible? I'm struggling to find documentation on the subject. Currently using the code:

def setUpTestData(cls):
    create test data for model 1
    create test data for model 2

def tests
    run some tests

Solution

  • This is the official documentation about loading initial data (a.k.a. fixtures) before testing: https://docs.djangoproject.com/en/2.0/howto/initial-data/

    Also, have a look at https://docs.djangoproject.com/en/2.0/topics/testing/tools/#django.test.TransactionTestCase.fixtures