Skip to main content

Run Django tests with optimization options to decrease runtime while keeping them trustable

Project description

django-lightweight-tests

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.

How it works

This package removes the following functionalities from Django, which greatly reduces tests runtime:

Migrations

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.

Warnings

Supress warnings to reduce print statements and make your test output cleaner.

Password hasher

If your tests create a lot of users, it may be useful to use a simple password hasher.

Middlewares

Adds only necessary middlewares to the request object.

DEBUG = False

Decreases Django overhead when debugging is activated.

SQLite

Run tests using SQLite.

How to install

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

How to use

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-lightweight-tests-0.0.1.tar.gz (4.4 kB view hashes)

Uploaded Source

Built Distribution

django_lightweight_tests-0.0.1-py3-none-any.whl (5.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page