Run Django tests with optimization options to decrease runtime while keeping them trustable.
In our tests using SQL Server, a 30 minutes runtime was reduced by 90%.
Most of these ideas were based on the article 7 Ways to Speed Up Your Django Test Suite by Tobin Brown.
This package removes the following functionalities from Django, which greatly reduces tests runtime:
Unless you test your migration files, there is no need to run all migrations everytime you run your test suite. So this option creates all necessary database structure without looking at your migration files.
Supress warnings to reduce print statements and make your test output cleaner.
If your tests create a lot of users, it may be useful to use a simple password hasher.
Adds only necessary middlewares to the request object.
Decreases Django overhead when debugging is activated.
Run tests using SQLite.
Add this line to your requirements.txt
:
-e git+https://github.com/luisccf/django-lightweight-tests.git#egg=django-lightweight-tests
Then simply run
pip install -r requirements.txt
Add the following code to your manage.py
:
import sys
from django_lightweight_tests import LightweightTest
...
is_testing = 'test' in sys.argv
if is_testing:
LightweightTest()
...
execute_from_command_line(sys.argv)
How you check if you are running your tests depends on your test runner. We run our tests using python manage.py test
, so this check works for us.
When you run your tests, pass the option --light
:
python manage.py test --light
You can change the arg name when instantiating the class:
LightweightTest(cmd_option='--opt')
python manage.py test --opt