Django comes with a test suite of its own, in the
tests directory of the
code base. It’s our policy to make sure all tests pass at all times.
We appreciate any and all contributions to the test suite!
The Django tests all use the testing infrastructure that ships with Django for testing applications. See Writing and running tests for an explanation of how to write new tests.
Running the unit tests¶
If you are on Python 2, you’ll first need to install a backport of the
unittest.mock module that’s available in Python 3. See
Running all the tests for details on installing mock and
the other optional test dependencies.
Running the tests requires a Django settings module that defines the databases to use. To make it easy to get started, Django provides and uses a sample settings module that uses the SQLite database. To run the tests:
$ git clone https://github.com/django/django.git django-repo $ cd django-repo/tests $ PYTHONPATH=..:$PYTHONPATH ./runtests.py
We recommend something like Git Bash to run the tests using the above approach.
You can avoid typing the
PYTHONPATH bit each time by adding your Django
checkout to your
PYTHONPATH or by installing the source checkout using pip.
See Installing the development version.
Having problems? See Troubleshooting for some common issues.
The included settings module allows you to run the test suite using SQLite. If you want to test behavior using a different database (and if you’re proposing patches for Django, it’s a good idea to test across databases), you may need to define your own settings file.
To run the tests with different settings, ensure that the module is on your
PYTHONPATH and pass the module with
DATABASES setting in any test settings module needs to define
defaultdatabase. This database should use the backend that you want to use for primary testing.
- A database with the alias
otherdatabase is used to establish that queries can be directed to different databases. As a result, this database can use any backend you want. It doesn’t need to use the same backend as the
defaultdatabase (although it can use the same backend if you want to). It cannot be the same database as the
If you’re using a backend that isn’t SQLite, you will need to provide other details for each database:
USERoption needs to specify an existing user account for the database. That user needs permission to execute
CREATE DATABASEso that the test database can be created.
PASSWORDoption needs to provide the password for the
USERthat has been specified.
You will also need to ensure that your database uses UTF-8 as the default
character set. If your database server doesn’t use UTF-8 as a default charset,
you will need to include a value for
CHARSET in the
test settings dictionary for the applicable database.
Running only some of the tests¶
Django’s entire test suite takes a while to run, and running every single test
could be redundant if, say, you just added a test to Django that you want to
run quickly without running everything else. You can run a subset of the unit
tests by appending the names of the test modules to
runtests.py on the
For example, if you’d like to run tests only for generic relations and internationalization, type:
$ ./runtests.py --settings=path.to.settings generic_relations i18n
How do you find out the names of individual tests? Look in
tests/ — each
directory name there is the name of a test.
If you just want to run a particular class of tests, you can specify a list of
paths to individual test classes. For example, to run the
i18n module, type:
$ ./runtests.py --settings=path.to.settings i18n.tests.TranslationTests
Going beyond that, you can specify an individual test method like this:
$ ./runtests.py --settings=path.to.settings i18n.tests.TranslationTests.test_lazy_objects
Running the Selenium tests¶
Some tests require Selenium and a Web browser (Firefox, Google Chrome, or
Internet Explorer). To allow those tests to be run rather than skipped, you must
install the selenium package into your Python path and run the tests with the
$ ./runtests.py --settings=test_sqlite --selenium admin_inlines
Running all the tests¶
If you want to run the full suite of tests, you’ll need to install a number of dependencies:
- jinja2 2.7+
- memcached, plus a supported Python binding
- mock (for Python 2)
- gettext (gettext on Windows)
You can find these dependencies in pip requirements files inside the
tests/requirements directory of the Django source tree and install them
$ pip install -r tests/requirements/py3.txt # Python 2: py2.txt
You can also install the database adapter(s) of your choice using
If you want to test the memcached cache backend, you’ll also need to define
CACHES setting that points at your memcached instance.
To run the GeoDjango tests, you will need to setup a spatial database and install the Geospatial libraries.
Each of these dependencies is optional. If you’re missing any of them, the associated tests will be skipped.
Contributors are encouraged to run coverage on the test suite to identify areas that need additional tests. The coverage tool installation and use is described in testing code coverage.
Coverage should be run in a single process to obtain accurate statistics. To run coverage on the Django test suite using the standard test settings:
$ coverage run ./runtests.py --settings=test_sqlite --parallel=1
After running coverage, generate the html report by running:
$ coverage html
When running coverage for the Django tests, the included
settings file defines
coverage_html as the output directory for the report
and also excludes several directories not relevant to the results
(test code or external code included in Django).
Tests for contrib apps can be found in the
tests/ directory, typically
<app_name>_tests. For example, tests for
contrib.auth are located
Many test failures with
locales package is not installed, some tests will fail with a
You can resolve this on Debian-based systems, for example, by running:
$ apt-get install locales $ dpkg-reconfigure locales
Tests that only fail in combination¶
In case a test passes when run in isolation but fails within the whole suite, we have some tools to help analyze the problem.
--bisect option of
runtests.py will run the failing test while
halving the test set it is run together with on each iteration, often making
it possible to identify a small number of tests that may be related to the
For example, suppose that the failing test that works on its own is
ModelTest.test_eq, then using:
$ ./runtests.py --bisect basic.tests.ModelTest.test_eq
will try to determine a test that interferes with the given one. First, the test is run with the first half of the test suite. If a failure occurs, the first half of the test suite is split in two groups and each group is then run with the specified test. If there is no failure with the first half of the test suite, the second half of the test suite is run with the specified test and split appropriately as described earlier. The process repeats until the set of failing tests is minimized.
--pair option runs the given test alongside every other test from the
suite, letting you check if another test has side-effects that cause the
$ ./runtests.py --pair basic.tests.ModelTest.test_eq
test_eq with every test label.
--pair, if you already suspect which cases
might be responsible for the failure, you may limit tests to be cross-analyzed
by specifying further test labels after
the first one:
$ ./runtests.py --pair basic.tests.ModelTest.test_eq queries transactions
You can also try running any set of tests in reverse using the
option in order to verify that executing tests in a different order does not
cause any trouble:
$ ./runtests.py basic --reverse
Seeing the SQL queries run during a test¶
If you wish to examine the SQL being run in failing tests, you can turn on
SQL logging using the
--debug-sql option. If you
combine this with
--verbosity=2, all SQL queries will be output:
$ ./runtests.py basic --debug-sql
--debug-sql options were added.
Seeing the full traceback of a test failure¶
By default tests are run in parallel with one process per core. When the tests
are run in parallel, however, you’ll only see a truncated traceback for any
test failures. You can adjust this behavior with the
$ ./runtests.py basic --parallel=1
You can also use the
DJANGO_TEST_PROCESSES environment variable for this
Support for running tests in parallel and the
--parallel option were