How can we make Django tests faster? How can we make Django tests faster? postgresql postgresql

How can we make Django tests faster?


In Django 1.9 if you have a multi-core processor a great option is the flag:

--parallel

This requires you to pip install tblib but will let you run your unit tests simultaneously on multiple cores. (https://docs.djangoproject.com/en/1.10/ref/django-admin/#cmdoption-test-parallel)

Another great option for Django 1.8+ is the flag:

--keepdb

It reuses your test database, stopping the long wait time caused by creating a new test database each time you run tests. (https://docs.djangoproject.com/en/1.10/ref/django-admin/#cmdoption-test-keepdb


The best option is to have a separate settings file for your tests. In settings_test.py you tell it to use sqlite which by default use an in-memory database:

from base_settings import *DATABASES = {  'default': {    'ENGINE': 'django.db.backends.sqlite3',    'NAME': ':memory'  }}

And then run your tests by adding --settings=settings_test

See also the Django docs:
https://docs.djangoproject.com/en/dev/topics/testing/overview/#the-test-database


There are a couple of SO threads that are helpful:

I definitely do use the SQLite trick to do sanity checks, but if you're doing anything database-specific it will drive you nuts: certain SQL differences, differences is data precision, etc. It also undercuts the point of testing: if you're using the tests to reassure you the change will work once pushed to production, running them against a different database isn't a good way to do that. Try using nose to skip database recreation when possible and optimize your local Postgres setup. You can try avoiding the DB altogether as well.

The thing that works best for me is trying to see the downtime caused by testing as an opportunity to come up with better changes and a way to encourage me to think about what I'm changing before firing up the test runner.