I have a production database that contains a large set of data. I'd like to use some of that data for running unit tests, but taking all of it causes a fairly lengthy period at the start of the testing process to build the database., which I'd like to avoid.
I've created a test database using the manage.py testserver
command, then deleted all the data I didn't want to be included through the admin interface. How do I create a fixture of the data that remains in the default test database?
Now it is easier to save the test data into a fixture.
When you run the django unittests, a test
database is automatically created by putting test_
before the name of the default database. We can make django aware of this database by creating a new database on settings.py
. For example
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'CONN_MAX_AGE': 3600,
'NAME': 'mydatabase',
'USER': 'user',
'PASSWORD': 'pass',
'HOST': '127.0.0.1',
'PORT': '3306',
'OPTIONS': {'charset': 'utf8mb4'}
},
'test': {
'ENGINE': 'django.db.backends.mysql',
'CONN_MAX_AGE': 3600,
'NAME': 'test_mydatabase',
'USER': 'user',
'PASSWORD': 'pass',
'HOST': '127.0.0.1',
'PORT': '3306',
'OPTIONS': {'charset': 'utf8mb4'}
}
}
Then run a unittest setting a breakpoint on the first line, edit the test database as you wish, then run the following command:
./manage.py dumpdata app_name -o filename.json --database test
This will dump all data in the test
database into filename
. Notice that you should edit the test database while the unittest is running (hence the breakpoint). If not, even if you preserve the test database all its data will be erased upon completion of the unit test.
you can use dumpdata
to generate a json fixture, like this:
./manage.py dumpdata > fixture.json
if you want to save a fixture from your test, just serialize your qs:
# ... import your Models
from django.core.serializers import serialize
qs1 = Model1.objects.filter(...)
qs2 = Model2.objects.filter(...)
...
fixture = serialize('json', list(qs1) + list(qs2) + list(...))
with open('fixture.json', 'w') as f:
f.write(fixture)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With