[Solved]-How can I run django unit tests (through manage.py) faster


Note that you don’t have to run the entire test suite every time. You can just run the test suite for a single app by running manage.py test appname (or for multiple apps at once with manage.py test app1 app2 ...).

My usual workflow is to just run the tests for the app I’m working on as I work, and the run the full suite before I commit my next set of changes.


I haven’t tried it, but there was a recent check in that is supposed to help by allowing the tests to be run inside transactions that are then rolled back:

Documentation and developer commentary


I use a ram-disk for my MySQL database, fast does not even describe it, combined with only testing the specific test within the specific app you are working on it can cut testing time dramatically. There are some scripts out there that will automate the creation of the ramdisk for your database, I use mysql-ramdisk.py which one of my co-workers cooked up, this version is for Mac OS X. There is also a version for Linux here. Takes me about 30 sec to set the whole thing up for django dev, and my tests sometimes run in under five seconds including the creation of the default test database. 😉


You can prevent your test DB from being destroyed between test runs in Django 1.8+ by using the flag:



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


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)


You can run your tests using the sqlite database backend. It’s not appropriate for a full test run (since the db is different), but for sanity checks it saves a lot of time. See: http://mindlesstechnology.wordpress.com/2008/08/16/faster-django-unit-tests/



As posted in this thread: How do I run a unit test against the production database?, I create a test suite that can run against either the production db (on my local dev machine, via “manage.py shell”), or the regular django “manage.py test” test suite. It’s been a real time-saver for me for doing quick sanity checking and commit time validation during development. In either case I’m running against the same db (MySQL) and get django ORM in my tests.

Leave a comment