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.
The tests cover:
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 Testing Django applications for an explanation of how to write new tests.
Running the tests requires a Django settings module that defines the databases to use. To make it easy to get started, Django provides a sample settings module that uses the SQLite database. To run the tests with this sample settings module, cd into the Django tests/ directory and run:
./runtests.py --settings=test_sqlite
If you get an ImportError: No module named django.contrib error, you need to add your install of Django to your PYTHONPATH.
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, cd to the tests/ directory and type:
./runtests.py --settings=path.to.django.settings
The DATABASES setting in this test settings module needs to define two databases:
If you’re using a backend that isn’t SQLite, you will need to provide other details for each database:
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 TEST_CHARSET in the settings dictionary for the applicable database.
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 command line.
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/modeltests and tests/regressiontests — each directory name there is the name of a test. Contrib app names are also valid test names.
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 TranslationTests of the i18n module, type:
./runtests.py --settings=path.to.settings i18n.TranslationTests
Going beyond that, you can specify an individual test method like this:
./runtests.py --settings=path.to.settings i18n.TranslationTests.test_lazy_objects
Some admin tests require Selenium 2, Firefox and Python >= 2.6 to work via a real Web browser. To allow those tests to run and not be skipped, you must install the selenium package (version > 2.13) into your Python path.
Then, run the tests normally, for example:
./runtests.py --settings=test_sqlite admin_inlines
If you want to run the full suite of tests, you’ll need to install a number of dependencies:
If you want to test the memcached cache backend, you’ll also need to define a CACHES setting that points at your memcached instance.
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.
To run coverage on the Django test suite using the standard test settings:
coverage run ./runtests.py --settings=test_sqlite
After running coverage, generate the html report by running:
coverage html
When running coverage for the Django tests, the included .coveragerc 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 go in their respective directories under django/contrib, in a tests.py file. You can split the tests over multiple modules by using a tests directory in the normal Python way.
For the tests to be found, a models.py file must exist, even if it’s empty. If you have URLs that need to be mapped, put them in tests/urls.py.
To run tests for just one contrib app (e.g. markup), use the same method as above:
./runtests.py --settings=settings markup
Dec 23, 2012