单元测试集

Django在代码基本库的 tests 目录中带有自己的测试套件。 我们的政策是确保所有测试始终通过。

我们感谢对测试套件的所有贡献!

The Django tests all use the testing infrastructure that ships with Django for testing applications. See 编写并运行测试 for an explanation of how to write new tests.

运行单元测试

快速上手

First, fork Django on GitHub.

Second, create and activate a virtual environment. If you’re not familiar with how to do that, read our contributing tutorial.

Next, clone your fork, install some requirements, and run the tests:

Linux/MacOS     Windows

  1. $ git clone https://github.com/YourGitHubName/django.git django-repo
  2. $ cd django-repo/tests
  3. $ python -m pip install -e ..
  4. $ python -m pip install -r requirements/py3.txt
  5. $ ./runtests.py
  1. ...\> git clone https://github.com/YourGitHubName/django.git django-repo
  2. ...\> cd django-repo\tests
  3. ...\> py -m pip install -e ..
  4. ...\> py -m pip install -r requirements\py3.txt
  5. ...\> runtests.py

Installing the requirements will likely require some operating system packages that your computer doesn’t have installed. You can usually figure out which package to install by doing a web search for the last line or so of the error message. Try adding your operating system to the search query if needed.

If you have trouble installing the requirements, you can skip that step. See 运行所有测试 for details on installing the optional test dependencies. If you don’t have an optional dependency installed, the tests that require it will be skipped.

Running the tests requires a Django settings module that defines the databases to use. To help you get started, Django provides and uses a sample settings module that uses the SQLite database. See 使用另一个 settings 配置模块 to learn how to use a different settings module to run the tests with a different database.

Having problems? See 错误调试 for some common issues.

Running tests using tox

Tox is a tool for running tests in different virtual environments. Django includes a basic tox.ini that automates some checks that our build server performs on pull requests. To run the unit tests and other checks (such as import sorting, the documentation spelling checker, and code formatting), install and run the tox command from any place in the Django source tree:

Linux/MacOS     Windows

  1. $ python -m pip install tox
  2. $ tox
  1. ...\> py -m pip install tox
  2. ...\> tox

By default, tox runs the test suite with the bundled test settings file for SQLite, black, blacken-docs, flake8, isort, and the documentation spelling checker. In addition to the system dependencies noted elsewhere in this documentation, the command python3 must be on your path and linked to the appropriate version of Python. A list of default environments can be seen as follows:

Linux/MacOS     Windows

  1. $ tox -l
  2. py3
  3. black
  4. blacken-docs
  5. flake8>=3.7.0
  6. docs
  7. isort>=5.1.0
  1. ...\> tox -l
  2. py3
  3. black
  4. blacken-docs
  5. flake8>=3.7.0
  6. docs
  7. isort>=5.1.0

Testing other Python versions and database backends

In addition to the default environments, tox supports running unit tests for other versions of Python and other database backends. Since Django’s test suite doesn’t bundle a settings file for database backends other than SQLite, however, you must create and provide your own test settings. For example, to run the tests on Python 3.10 using PostgreSQL:

Linux/MacOS     Windows

  1. $ tox -e py310-postgres -- --settings=my_postgres_settings
  1. ...\> tox -e py310-postgres -- --settings=my_postgres_settings

This command sets up a Python 3.10 virtual environment, installs Django’s test suite dependencies (including those for PostgreSQL), and calls runtests.py with the supplied arguments (in this case, --settings=my_postgres_settings).

The remainder of this documentation shows commands for running tests without tox, however, any option passed to runtests.py can also be passed to tox by prefixing the argument list with --, as above.

Tox also respects the DJANGO_SETTINGS_MODULE environment variable, if set. For example, the following is equivalent to the command above:

  1. $ DJANGO_SETTINGS_MODULE=my_postgres_settings tox -e py310-postgres

对于 Windows 用户应使用:

  1. ...\> set DJANGO_SETTINGS_MODULE=my_postgres_settings
  2. ...\> tox -e py310-postgres

运行 JavaScript 测试集

Django includes a set of JavaScript unit tests for functions in certain contrib apps. The JavaScript tests aren’t run by default using tox because they require Node.js to be installed and aren’t necessary for the majority of patches. To run the JavaScript tests using tox:

Linux/MacOS     Windows

  1. $ tox -e javascript
  1. ...\> tox -e javascript

This command runs npm install to ensure test requirements are up to date and then runs npm test.

Running tests using django-docker-box

django-docker-box allows you to run the Django’s test suite across all supported databases and python versions. See the django-docker-box project page for installation and usage instructions.

使用另一个 settings 配置模块

The included settings module (tests/test_sqlite.py) allows you to run the test suite using SQLite. If you want to run the tests using a different database, you’ll need to define your own settings file. Some tests, such as those for contrib.postgres, are specific to a particular database backend and will be skipped if run with a different backend. Some tests are skipped or expected failures on a particular database backend (see DatabaseFeatures.django_test_skips and DatabaseFeatures.django_test_expected_failures on each backend).

To run the tests with different settings, ensure that the module is on your PYTHONPATH and pass the module with --settings.

The DATABASES setting in any test settings module needs to define two databases:

  • A default database. This database should use the backend that you want to use for primary testing.
  • A database with the alias other. The other database is used to test that queries can be directed to different databases. This database should use the same backend as the default, and it must have a different name.

如果您使用的不是SQLite后端,则需要为每个数据库提供其他详细信息:

  • The USER option needs to specify an existing user account for the database. That user needs permission to execute CREATE DATABASE so that the test database can be created.
  • PASSWORD 配置选项需要为指定的配置:setting:USER 提供密码。

Test databases get their names by prepending test_ to the value of the NAME settings for the databases defined in DATABASES. These test databases are deleted when the tests are finished.

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.

执行一部分测试

Django的整个测试套件需要花一些时间才能运行,并且,例如,如果您刚刚向Django添加了一个想要快速运行而不运行其他所有功能的测试,则运行每个测试可能是多余的。您可以通过在命令行上将测试模块的名称附加到 runtests.py 上来运行单元测试的子集。

For example, if you’d like to run tests only for generic relations and internationalization, type:

Linux/MacOS     Windows

  1. $ ./runtests.py --settings=path.to.settings generic_relations i18n
  1. ...\> 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 want to run only 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:

Linux/MacOS     Windows

  1. $ ./runtests.py --settings=path.to.settings i18n.tests.TranslationTests
  1. ...\> runtests.py --settings=path.to.settings i18n.tests.TranslationTests

Going beyond that, you can specify an individual test method like this:

Linux/MacOS     Windows

  1. $ ./runtests.py --settings=path.to.settings i18n.tests.TranslationTests.test_lazy_objects
  1. ...\> runtests.py --settings=path.to.settings i18n.tests.TranslationTests.test_lazy_objects

You can run tests starting at a specified top-level module with --start-at option. For example:

Linux/MacOS     Windows

  1. $ ./runtests.py --start-at=wsgi
  1. ...\> runtests.py --start-at=wsgi

You can also run tests starting after a specified top-level module with --start-after option. For example:

Linux/MacOS     Windows

  1. $ ./runtests.py --start-after=wsgi
  1. ...\> runtests.py --start-after=wsgi

Note that the --reverse option doesn’t impact on --start-at or --start-after options. Moreover these options cannot be used with test labels.

运行 Selenium 测试

Some tests require Selenium and a web browser. To run these tests, you must install the selenium package and run the tests with the --selenium=<BROWSERS> option. For example, if you have Firefox and Google Chrome installed:

Linux/MacOS     Windows

  1. $ ./runtests.py --selenium=firefox,chrome
  1. ...\> runtests.py --selenium=firefox,chrome

See the selenium.webdriver package for the list of available browsers.

Specifying --selenium automatically sets --tags=selenium to run only the tests that require selenium.

Some browsers (e.g. Chrome or Firefox) support headless testing, which can be faster and more stable. Add the --headless option to enable this mode.

运行所有测试

如果要运行全套测试,则需要安装许多依赖项:

You can find these dependencies in pip requirements files inside the tests/requirements directory of the Django source tree and install them like so:

Linux/MacOS     Windows

  1. $ python -m pip install -r tests/requirements/py3.txt
  1. ...\> py -m pip install -r tests\requirements\py3.txt

如果你在安装过程中遇到错误,你的系统可能缺少一个或多个 Python 包的依赖。请查阅失败软件包的文档,或者用你遇到的错误信息在网上搜索。

You can also install the database adapter(s) of your choice using oracle.txt, mysql.txt, or postgres.txt.

If you want to test the memcached or Redis cache backends, you’ll also need to define a CACHES setting that points at your memcached or Redis instance respectively.

To run the GeoDjango tests, you will need to set up a spatial database and install the Geospatial libraries.

这些依赖项中的每一个都是可选的。 如果您缺少其中的任何一个,则将跳过关联的测试。

To run some of the autoreload tests, you’ll need to install the Watchman service.

代码覆盖率

鼓励贡献者对测试套件进行覆盖率测试,以确定需要额外测试的区域。在:ref:testing code coverage 1. 中介绍了coverage代码覆盖度工具的安装和使用。

To run coverage on the Django test suite using the standard test settings:

Linux/MacOS     Windows

  1. $ coverage run ./runtests.py --settings=test_sqlite
  1. ...\> coverage run runtests.py --settings=test_sqlite

After running coverage, combine all coverage statistics by running:

Linux/MacOS     Windows

  1. $ coverage combine
  1. ...\> coverage combine

After that generate the html report by running:

Linux/MacOS     Windows

  1. $ coverage html
  1. ...\> coverage html

运行Django测试的覆盖率时,随附的 .coveragerc 配置文件将 coverage_html 定义为报告的输出目录,并且还排除了与结果无关的几个目录(Django中包含的测试代码或外部代码)。

Contrib 应用程序

Tests for contrib apps can be found in the tests/ directory, typically under <app_name>_tests. For example, tests for contrib.auth are located in tests/auth_tests.

错误调试

Test suite hangs or shows failures on main branch

Ensure you have the latest point release of a supported Python version, since there are often bugs in earlier versions that may cause the test suite to fail or hang.

On macOS (High Sierra and newer versions), you might see this message logged, after which the tests hang:

  1. objc[42074]: +[__NSPlaceholderDate initialize] may have been in progress in
  2. another thread when fork() was called.

To avoid this set a OBJC_DISABLE_INITIALIZE_FORK_SAFETY environment variable, for example:

  1. $ OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES ./runtests.py

Or add export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES to your shell’s startup file (e.g. ~/.profile).

Many test failures with UnicodeEncodeError

If the locales package is not installed, some tests will fail with a UnicodeEncodeError.

You can resolve this on Debian-based systems, for example, by running:

  1. $ apt-get install locales
  2. $ dpkg-reconfigure locales

You can resolve this for macOS systems by configuring your shell’s locale:

  1. $ export LANG="en_US.UTF-8"
  2. $ export LC_ALL="en_US.UTF-8"

Run the locale command to confirm the change. Optionally, add those export commands to your shell’s startup file (e.g. ~/.bashrc for Bash) to avoid having to retype them.

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.

The --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 failure.

For example, suppose that the failing test that works on its own is ModelTest.test_eq, then using:

Linux/MacOS     Windows

  1. $ ./runtests.py --bisect basic.tests.ModelTest.test_eq
  1. ...\> 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.

The --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 failure. So:

Linux/MacOS     Windows

  1. $ ./runtests.py --pair basic.tests.ModelTest.test_eq
  1. ...\> runtests.py --pair basic.tests.ModelTest.test_eq

will pair test_eq with every test label.

With both --bisect and --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:

Linux/MacOS     Windows

  1. $ ./runtests.py --pair basic.tests.ModelTest.test_eq queries transactions
  1. ...\> runtests.py --pair basic.tests.ModelTest.test_eq queries transactions

You can also try running any set of tests in a random or reverse order using the --shuffle and --reverse options. This can help verify that executing tests in a different order does not cause any trouble:

Linux/MacOS     Windows

  1. $ ./runtests.py basic --shuffle
  2. $ ./runtests.py basic --reverse
  1. ...\> runtests.py basic --shuffle
  2. ...\> 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:

Linux/MacOS     Windows

  1. $ ./runtests.py basic --debug-sql
  1. ...\> runtests.py basic --debug-sql

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 --parallel option:

Linux/MacOS     Windows

  1. $ ./runtests.py basic --parallel=1
  1. ...\> runtests.py basic --parallel=1

You can also use the DJANGO_TEST_PROCESSES environment variable for this purpose.

Tips for writing tests

Isolating model registration

To avoid polluting the global apps registry and prevent unnecessary table creation, models defined in a test method should be bound to a temporary Apps instance. To do this, use the isolate_apps() decorator:

  1. from django.db import models
  2. from django.test import SimpleTestCase
  3. from django.test.utils import isolate_apps
  4. class TestModelDefinition(SimpleTestCase):
  5. @isolate_apps("app_label")
  6. def test_model_definition(self):
  7. class TestModel(models.Model):
  8. pass
  9. ...

Setting app_label

Models defined in a test method with no explicit app_label are automatically assigned the label of the app in which their test class is located.

In order to make sure the models defined within the context of isolate_apps() instances are correctly installed, you should pass the set of targeted app_label as arguments:

tests/app_label/tests.py

  1. from django.db import models
  2. from django.test import SimpleTestCase
  3. from django.test.utils import isolate_apps
  4. class TestModelDefinition(SimpleTestCase):
  5. @isolate_apps("app_label", "other_app_label")
  6. def test_model_definition(self):
  7. # This model automatically receives app_label='app_label'
  8. class TestModel(models.Model):
  9. pass
  10. class OtherAppModel(models.Model):
  11. class Meta:
  12. app_label = "other_app_label"
  13. ...