Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating test database for Django Unit testing

How do I populate the test database created while testing the Django test cases with the values from some other database(for ex: the Production database.)

In detail:

when I run the below command,

$ python manage.py test

a test data base is created for the testing purpose, but it doesn't have any data in it. I want the test database created to be populated with some initial values.

Thanks..

like image 819
user5496885 Avatar asked Oct 24 '25 05:10

user5496885


1 Answers

You can use dumpdata to get a file with data from your live db.

Then you can load data from a file automatically for a test (see Django tests):

from django.test import TestCase
from django.core.management import call_command

class Tests(TestCase):
    @classmethod
    def setUpTestData(cls):
        call_command('loaddata', 'myfile', verbosity=0)
like image 75
Wtower Avatar answered Oct 26 '25 17:10

Wtower



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!