{"title":"单元测试集","version":"4.1","locale":"zh-hans","docname":"internals/contributing/writing-code/unit-tests","url":"/zh-hans/4.1/internals/contributing/writing-code/unit-tests/","canonical":"https://djangodocs.dev/zh-hans/4.1/internals/contributing/writing-code/unit-tests/","summary":"Django在代码基本库的 tests 目录中带有自己的测试套件。 我们的政策是确保所有测试始终通过。 我们感谢对测试套件的所有贡献！ The Django tests all use the testing infrastructure that ships with Django for testing…","html":"<h1>单元测试集<a class=\"heading-anchor\" href=\"#unit-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Django在代码基本库的 <code class=\"docutils literal notranslate\"><span class=\"pre\">tests</span></code> 目录中带有自己的测试套件。 我们的政策是确保所有测试始终通过。</p>\n<p>我们感谢对测试套件的所有贡献！</p>\n<p>The Django tests all use the testing infrastructure that ships with Django for\ntesting applications. See <a class=\"reference internal\" href=\"/zh-hans/4.1/topics/testing/overview/\"><span class=\"doc\">编写并运行测试</span></a> for an explanation of\nhow to write new tests.</p>\n<section id=\"running-the-unit-tests\">\n<span id=\"running-unit-tests\"></span><h2>运行单元测试<a class=\"heading-anchor\" href=\"#running-the-unit-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"quickstart\">\n<h3>快速上手<a class=\"heading-anchor\" href=\"#quickstart\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>First, <a class=\"reference external\" href=\"https://github.com/django/django/fork\">fork Django on GitHub</a>.</p>\n<p>Second, create and activate a virtual environment. If you're not familiar with\nhow to do that, read our <a class=\"reference internal\" href=\"/zh-hans/4.1/intro/contributing/\"><span class=\"doc\">contributing tutorial</span></a>.</p>\n<p>Next, clone your fork, install some requirements, and run the tests:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-0-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>git<span class=\"w\"> </span>clone<span class=\"w\"> </span>https://github.com/YourGitHubName/django.git<span class=\"w\"> </span>django-repo\n<span class=\"gp\">$ </span><span class=\"nb\">cd</span><span class=\"w\"> </span>django-repo/tests\n<span class=\"gp\">$ </span>python<span class=\"w\"> </span>-m<span class=\"w\"> </span>pip<span class=\"w\"> </span>install<span class=\"w\"> </span>-e<span class=\"w\"> </span>..\n<span class=\"gp\">$ </span>python<span class=\"w\"> </span>-m<span class=\"w\"> </span>pip<span class=\"w\"> </span>install<span class=\"w\"> </span>-r<span class=\"w\"> </span>requirements/py3.txt\n<span class=\"gp\">$ </span>./runtests.py\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-0-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> git clone https://github.com/YourGitHubName/django.git django-repo\n<span class=\"gp\">...\\&gt;</span> <span class=\"k\">cd</span> django-repo\\tests\n<span class=\"gp\">...\\&gt;</span> py -m pip install -e ..\n<span class=\"gp\">...\\&gt;</span> py -m pip install -r requirements\\py3.txt\n<span class=\"gp\">...\\&gt;</span> runtests.py \n</code></pre></div></div></div>\n<p>Installing the requirements will likely require some operating system packages\nthat your computer doesn't have installed. You can usually figure out which\npackage to install by doing a web search for the last line or so of the error\nmessage. Try adding your operating system to the search query if needed.</p>\n<p>If you have trouble installing the requirements, you can skip that step. See\n<a class=\"reference internal\" href=\"#running-unit-tests-dependencies\"><span class=\"std std-ref\">运行所有测试</span></a> for details on installing the optional\ntest dependencies. If you don't have an optional dependency installed, the\ntests that require it will be skipped.</p>\n<p>Running the tests requires a Django settings module that defines the databases\nto use. To help you get started, Django provides and uses a sample settings\nmodule that uses the SQLite database. See <a class=\"reference internal\" href=\"#running-unit-tests-settings\"><span class=\"std std-ref\">使用另一个 settings 配置模块</span></a> to\nlearn how to use a different settings module to run the tests with a different\ndatabase.</p>\n<p>Having problems? See <a class=\"reference internal\" href=\"#troubleshooting-unit-tests\"><span class=\"std std-ref\">错误调试</span></a> for some common issues.</p>\n</section>\n<section id=\"running-tests-using-tox\">\n<h3>Running tests using <code class=\"docutils literal notranslate\"><span class=\"pre\">tox</span></code><a class=\"heading-anchor\" href=\"#running-tests-using-tox\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><a class=\"reference external\" href=\"https://tox.wiki/\">Tox</a> is a tool for running tests in different virtual\nenvironments. Django includes a basic <code class=\"docutils literal notranslate\"><span class=\"pre\">tox.ini</span></code> that automates some checks\nthat our build server performs on pull requests. To run the unit tests and\nother checks (such as <a class=\"reference internal\" href=\"/zh-hans/4.1/internals/contributing/writing-code/coding-style/#coding-style-imports\"><span class=\"std std-ref\">import sorting</span></a>, the\n<a class=\"reference internal\" href=\"/zh-hans/4.1/internals/contributing/writing-documentation/#documentation-spelling-check\"><span class=\"std std-ref\">documentation spelling checker</span></a>, and\n<a class=\"reference internal\" href=\"/zh-hans/4.1/internals/contributing/writing-code/coding-style/#coding-style-python\"><span class=\"std std-ref\">code formatting</span></a>), install and run the <code class=\"docutils literal notranslate\"><span class=\"pre\">tox</span></code>\ncommand from any place in the Django source tree:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-1-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>python<span class=\"w\"> </span>-m<span class=\"w\"> </span>pip<span class=\"w\"> </span>install<span class=\"w\"> </span>tox\n<span class=\"gp\">$ </span>tox\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-1-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> py -m pip install tox\n<span class=\"gp\">...\\&gt;</span> tox\n</code></pre></div></div></div>\n<p>By default, <code class=\"docutils literal notranslate\"><span class=\"pre\">tox</span></code> runs the test suite with the bundled test settings file for\nSQLite, <code class=\"docutils literal notranslate\"><span class=\"pre\">black</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">flake8</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">isort</span></code>, and the documentation spelling\nchecker. In addition to the system dependencies noted elsewhere in this\ndocumentation, the command <code class=\"docutils literal notranslate\"><span class=\"pre\">python3</span></code> must be on your path and linked to the\nappropriate version of Python. A list of default environments can be seen as\nfollows:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-2-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>tox<span class=\"w\"> </span>-l\n<span class=\"go\">py3</span>\n<span class=\"go\">black</span>\n<span class=\"go\">flake8&gt;=3.7.0</span>\n<span class=\"go\">docs</span>\n<span class=\"go\">isort&gt;=5.1.0</span>\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-2-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> tox -l\n<span class=\"go\">py3</span>\n<span class=\"go\">black</span>\n<span class=\"gp\">flake8&gt;</span>=3.7.0\n<span class=\"go\">docs</span>\n<span class=\"gp\">isort&gt;</span>=5.1.0\n</code></pre></div></div></div>\n<section id=\"testing-other-python-versions-and-database-backends\">\n<h4>Testing other Python versions and database backends<a class=\"heading-anchor\" href=\"#testing-other-python-versions-and-database-backends\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>In addition to the default environments, <code class=\"docutils literal notranslate\"><span class=\"pre\">tox</span></code> supports running unit tests\nfor other versions of Python and other database backends. Since Django's test\nsuite doesn't bundle a settings file for database backends other than SQLite,\nhowever, you must <a class=\"reference internal\" href=\"#running-unit-tests-settings\"><span class=\"std std-ref\">create and provide your own test settings</span></a>. For example, to run the tests on Python 3.9\nusing PostgreSQL:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-3-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>tox<span class=\"w\"> </span>-e<span class=\"w\"> </span>py39-postgres<span class=\"w\"> </span>--<span class=\"w\"> </span>--settings<span class=\"o\">=</span>my_postgres_settings\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-3-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> tox -e py39-postgres -- --settings=my_postgres_settings\n</code></pre></div></div></div>\n<p>This command sets up a Python 3.9 virtual environment, installs Django's\ntest suite dependencies (including those for PostgreSQL), and calls\n<code class=\"docutils literal notranslate\"><span class=\"pre\">runtests.py</span></code> with the supplied arguments (in this case,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">--settings=my_postgres_settings</span></code>).</p>\n<p>The remainder of this documentation shows commands for running tests without\n<code class=\"docutils literal notranslate\"><span class=\"pre\">tox</span></code>, however, any option passed to <code class=\"docutils literal notranslate\"><span class=\"pre\">runtests.py</span></code> can also be passed to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">tox</span></code> by prefixing the argument list with <code class=\"docutils literal notranslate\"><span class=\"pre\">--</span></code>, as above.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">Tox</span></code> also respects the <span class=\"target\" id=\"index-0\"></span><a class=\"reference internal\" href=\"/zh-hans/4.1/topics/settings/#envvar-DJANGO_SETTINGS_MODULE\"><code class=\"xref std std-envvar docutils literal notranslate\"><span class=\"pre\">DJANGO_SETTINGS_MODULE</span></code></a> environment\nvariable, if set. For example, the following is equivalent to the command\nabove:</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span><span class=\"nv\">DJANGO_SETTINGS_MODULE</span><span class=\"o\">=</span>my_postgres_settings<span class=\"w\"> </span>tox<span class=\"w\"> </span>-e<span class=\"w\"> </span>py39-postgres\n</code></pre></div>\n<p>对于 Windows 用户应使用：</p>\n<div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows code\"><code><span class=\"gp\">...\\&gt;</span> <span class=\"k\">set</span> <span class=\"nv\">DJANGO_SETTINGS_MODULE</span><span class=\"p\">=</span>my_postgres_settings\n<span class=\"gp\">...\\&gt;</span> tox -e py39-postgres\n</code></pre></div>\n</section>\n<section id=\"running-the-javascript-tests\">\n<h4>运行 JavaScript 测试集<a class=\"heading-anchor\" href=\"#running-the-javascript-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Django includes a set of <a class=\"reference internal\" href=\"/zh-hans/4.1/internals/contributing/writing-code/javascript/#javascript-tests\"><span class=\"std std-ref\">JavaScript unit tests</span></a> for\nfunctions in certain contrib apps. The JavaScript tests aren't run by default\nusing <code class=\"docutils literal notranslate\"><span class=\"pre\">tox</span></code> because they require <code class=\"docutils literal notranslate\"><span class=\"pre\">Node.js</span></code> to be installed and aren't\nnecessary for the majority of patches. To run the JavaScript tests using\n<code class=\"docutils literal notranslate\"><span class=\"pre\">tox</span></code>:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-4-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>tox<span class=\"w\"> </span>-e<span class=\"w\"> </span>javascript\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-4-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> tox -e javascript\n</code></pre></div></div></div>\n<p>This command runs <code class=\"docutils literal notranslate\"><span class=\"pre\">npm</span> <span class=\"pre\">install</span></code> to ensure test requirements are up to\ndate and then runs <code class=\"docutils literal notranslate\"><span class=\"pre\">npm</span> <span class=\"pre\">test</span></code>.</p>\n</section>\n</section>\n<section id=\"running-tests-using-django-docker-box\">\n<h3>Running tests using <code class=\"docutils literal notranslate\"><span class=\"pre\">django-docker-box</span></code><a class=\"heading-anchor\" href=\"#running-tests-using-django-docker-box\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><a class=\"reference external\" href=\"https://github.com/django/django-docker-box/\">django-docker-box</a> allows you to run the Django's test suite across all\nsupported databases and python versions. See the <a class=\"reference external\" href=\"https://github.com/django/django-docker-box/\">django-docker-box</a> project\npage for installation and usage instructions.</p>\n</section>\n<section id=\"using-another-settings-module\">\n<span id=\"running-unit-tests-settings\"></span><h3>使用另一个 <code class=\"docutils literal notranslate\"><span class=\"pre\">settings</span></code> 配置模块<a class=\"heading-anchor\" href=\"#using-another-settings-module\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The included settings module (<code class=\"docutils literal notranslate\"><span class=\"pre\">tests/test_sqlite.py</span></code>) allows you to run the\ntest suite using SQLite. If you want to run the tests using a different\ndatabase, you'll need to define your own settings file. Some tests, such as\nthose for <code class=\"docutils literal notranslate\"><span class=\"pre\">contrib.postgres</span></code>, are specific to a particular database backend\nand will be skipped if run with a different backend. Some tests are skipped or\nexpected failures on a particular database backend (see\n<code class=\"docutils literal notranslate\"><span class=\"pre\">DatabaseFeatures.django_test_skips</span></code> and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">DatabaseFeatures.django_test_expected_failures</span></code> on each backend).</p>\n<p>To run the tests with different settings, ensure that the module is on your\n<span class=\"target\" id=\"index-1\"></span><a class=\"reference external\" href=\"https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPATH\" title=\"(in Python v3.14)\"><code class=\"xref std std-envvar docutils literal notranslate\"><span class=\"pre\">PYTHONPATH</span></code></a> and pass the module with <code class=\"docutils literal notranslate\"><span class=\"pre\">--settings</span></code>.</p>\n<p>The <a class=\"reference internal\" href=\"/zh-hans/4.1/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DATABASES</span></code></a> setting in any test settings module needs to define\ntwo databases:</p>\n<ul class=\"simple\">\n<li><p>A <code class=\"docutils literal notranslate\"><span class=\"pre\">default</span></code> database. This database should use the backend that\nyou want to use for primary testing.</p></li>\n<li><p>A database with the alias <code class=\"docutils literal notranslate\"><span class=\"pre\">other</span></code>. The <code class=\"docutils literal notranslate\"><span class=\"pre\">other</span></code> database is used to test\nthat queries can be directed to different databases. This database should use\nthe same backend as the <code class=\"docutils literal notranslate\"><span class=\"pre\">default</span></code>, and it must have a different name.</p></li>\n</ul>\n<p>如果您使用的不是SQLite后端，则需要为每个数据库提供其他详细信息：</p>\n<ul class=\"simple\">\n<li><p>The <a class=\"reference internal\" href=\"/zh-hans/4.1/ref/settings/#std-setting-USER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USER</span></code></a> option needs to specify an existing user account\nfor the database. That user needs permission to execute <code class=\"docutils literal notranslate\"><span class=\"pre\">CREATE</span> <span class=\"pre\">DATABASE</span></code>\nso that the test database can be created.</p></li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/4.1/ref/settings/#std-setting-PASSWORD\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">PASSWORD</span></code></a> 配置选项需要为指定的配置:setting:USER 提供密码。</p></li>\n</ul>\n<p>Test databases get their names by prepending <code class=\"docutils literal notranslate\"><span class=\"pre\">test_</span></code> to the value of the\n<a class=\"reference internal\" href=\"/zh-hans/4.1/ref/settings/#std-setting-NAME\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">NAME</span></code></a> settings for the databases defined in <a class=\"reference internal\" href=\"/zh-hans/4.1/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DATABASES</span></code></a>.\nThese test databases are deleted when the tests are finished.</p>\n<p>You will also need to ensure that your database uses UTF-8 as the default\ncharacter set. If your database server doesn't use UTF-8 as a default charset,\nyou will need to include a value for <a class=\"reference internal\" href=\"/zh-hans/4.1/ref/settings/#std-setting-TEST_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CHARSET</span></code></a> in the\ntest settings dictionary for the applicable database.</p>\n</section>\n<section id=\"running-only-some-of-the-tests\">\n<span id=\"runtests-specifying-labels\"></span><h3>执行一部分测试<a class=\"heading-anchor\" href=\"#running-only-some-of-the-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django的整个测试套件需要花一些时间才能运行，并且，例如，如果您刚刚向Django添加了一个想要快速运行而不运行其他所有功能的测试，则运行每个测试可能是多余的。您可以通过在命令行上将测试模块的名称附加到 <code class=\"docutils literal notranslate\"><span class=\"pre\">runtests.py</span></code> 上来运行单元测试的子集。</p>\n<p>For example, if you'd like to run tests only for generic relations and\ninternationalization, type:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-5-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>--settings<span class=\"o\">=</span>path.to.settings<span class=\"w\"> </span>generic_relations<span class=\"w\"> </span>i18n\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-5-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> runtests.py --settings=path.to.settings generic_relations i18n\n</code></pre></div></div></div>\n<p>How do you find out the names of individual tests? Look in <code class=\"docutils literal notranslate\"><span class=\"pre\">tests/</span></code> — each\ndirectory name there is the name of a test.</p>\n<p>If you want to run only a particular class of tests, you can specify a list of\npaths to individual test classes. For example, to run the <code class=\"docutils literal notranslate\"><span class=\"pre\">TranslationTests</span></code>\nof the <code class=\"docutils literal notranslate\"><span class=\"pre\">i18n</span></code> module, type:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-6-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>--settings<span class=\"o\">=</span>path.to.settings<span class=\"w\"> </span>i18n.tests.TranslationTests\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-6-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> runtests.py --settings=path.to.settings i18n.tests.TranslationTests\n</code></pre></div></div></div>\n<p>Going beyond that, you can specify an individual test method like this:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-7-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>--settings<span class=\"o\">=</span>path.to.settings<span class=\"w\"> </span>i18n.tests.TranslationTests.test_lazy_objects\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-7-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> runtests.py --settings=path.to.settings i18n.tests.TranslationTests.test_lazy_objects\n</code></pre></div></div></div>\n<p>You can run tests starting at a specified top-level module with <code class=\"docutils literal notranslate\"><span class=\"pre\">--start-at</span></code>\noption. For example:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-8-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>--start-at<span class=\"o\">=</span>wsgi\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-8-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> runtests.py --start-at=wsgi\n</code></pre></div></div></div>\n<p>You can also run tests starting after a specified top-level module with\n<code class=\"docutils literal notranslate\"><span class=\"pre\">--start-after</span></code> option. For example:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-9-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>--start-after<span class=\"o\">=</span>wsgi\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-9-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> runtests.py --start-after=wsgi\n</code></pre></div></div></div>\n<p>Note that the <code class=\"docutils literal notranslate\"><span class=\"pre\">--reverse</span></code> option doesn't impact on <code class=\"docutils literal notranslate\"><span class=\"pre\">--start-at</span></code> or\n<code class=\"docutils literal notranslate\"><span class=\"pre\">--start-after</span></code> options. Moreover these options cannot be used with test\nlabels.</p>\n</section>\n<section id=\"running-the-selenium-tests\">\n<h3>运行 Selenium 测试<a class=\"heading-anchor\" href=\"#running-the-selenium-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Some tests require Selenium and a web browser. To run these tests, you must\ninstall the <a class=\"reference external\" href=\"https://pypi.org/project/selenium/\">selenium</a> package and run the tests with the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">--selenium=&lt;BROWSERS&gt;</span></code> option. For example, if you have Firefox and Google\nChrome installed:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-10-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>--selenium<span class=\"o\">=</span>firefox,chrome\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-10-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> runtests.py --selenium=firefox,chrome\n</code></pre></div></div></div>\n<p>See the <a class=\"reference external\" href=\"https://github.com/SeleniumHQ/selenium/tree/trunk/py/selenium/webdriver\">selenium.webdriver</a> package for the list of available browsers.</p>\n<p>Specifying <code class=\"docutils literal notranslate\"><span class=\"pre\">--selenium</span></code> automatically sets <code class=\"docutils literal notranslate\"><span class=\"pre\">--tags=selenium</span></code> to run only\nthe tests that require selenium.</p>\n<p>Some browsers (e.g. Chrome or Firefox) support headless testing, which can be\nfaster and more stable. Add the <code class=\"docutils literal notranslate\"><span class=\"pre\">--headless</span></code> option to enable this mode.</p>\n</section>\n<section id=\"running-all-the-tests\">\n<span id=\"running-unit-tests-dependencies\"></span><h3>运行所有测试<a class=\"heading-anchor\" href=\"#running-all-the-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>如果要运行全套测试，则需要安装许多依赖项：</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/aiosmtpd/\">aiosmtpd</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/argon2-cffi/\">argon2-cffi</a> 19.1.0+</p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/asgiref/\">asgiref</a> 3.5.2+ (required)</p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/bcrypt/\">bcrypt</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/colorama/\">colorama</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/docutils/\">docutils</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/geoip2/\">geoip2</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/Jinja2/\">jinja2</a> 2.7+</p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/numpy/\">numpy</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/Pillow/\">Pillow</a> 6.2.0+</p></li>\n<li><p><a class=\"reference external\" href=\"https://pyyaml.org/wiki/PyYAML\">PyYAML</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/pytz/\">pytz</a> (required)</p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/pywatchman/\">pywatchman</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/redis/\">redis</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/setuptools/\">setuptools</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://memcached.org/\">memcached</a>, 加上:ref:supported Python binding &lt;memcached&gt;</p></li>\n<li><p><a class=\"reference external\" href=\"https://www.gnu.org/software/gettext/manual/gettext.html\">gettext</a> (<a class=\"reference internal\" href=\"/zh-hans/4.1/topics/i18n/translation/#gettext-on-windows\"><span class=\"std std-ref\">Windows 上的 gettext</span></a>)</p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/selenium/\">selenium</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/sqlparse/\">sqlparse</a> 0.2.2+ (required)</p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/tblib/\">tblib</a> 1.5.0+</p></li>\n</ul>\n<p>You can find these dependencies in <a class=\"reference external\" href=\"https://pip.pypa.io/en/latest/user_guide/#requirements-files\">pip requirements files</a> inside the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">tests/requirements</span></code> directory of the Django source tree and install them\nlike so:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-11-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>python<span class=\"w\"> </span>-m<span class=\"w\"> </span>pip<span class=\"w\"> </span>install<span class=\"w\"> </span>-r<span class=\"w\"> </span>tests/requirements/py3.txt\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-11-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> py -m pip install -r tests\\requirements\\py3.txt\n</code></pre></div></div></div>\n<p>如果你在安装过程中遇到错误，你的系统可能缺少一个或多个 Python 包的依赖。请查阅失败软件包的文档，或者用你遇到的错误信息在网上搜索。</p>\n<p>You can also install the database adapter(s) of your choice using\n<code class=\"docutils literal notranslate\"><span class=\"pre\">oracle.txt</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">mysql.txt</span></code>, or <code class=\"docutils literal notranslate\"><span class=\"pre\">postgres.txt</span></code>.</p>\n<p>If you want to test the memcached or Redis cache backends, you'll also need to\ndefine a <a class=\"reference internal\" href=\"/zh-hans/4.1/ref/settings/#std-setting-CACHES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CACHES</span></code></a> setting that points at your memcached or Redis\ninstance respectively.</p>\n<p>To run the GeoDjango tests, you will need to <a class=\"reference internal\" href=\"/zh-hans/4.1/ref/contrib/gis/install/\"><span class=\"doc\">set up a spatial database\nand install the Geospatial libraries</span></a>.</p>\n<p>这些依赖项中的每一个都是可选的。 如果您缺少其中的任何一个，则将跳过关联的测试。</p>\n<p>To run some of the autoreload tests, you'll need to install the <a class=\"reference external\" href=\"https://facebook.github.io/watchman/\">Watchman</a>\nservice.</p>\n</section>\n<section id=\"code-coverage\">\n<h3>代码覆盖率<a class=\"heading-anchor\" href=\"#code-coverage\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>鼓励贡献者对测试套件进行覆盖率测试，以确定需要额外测试的区域。在:ref:testing code coverage 1. 中介绍了coverage代码覆盖度工具的安装和使用。</p>\n<p>Coverage should be run in a single process to obtain accurate statistics. To\nrun coverage on the Django test suite using the standard test settings:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-12-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>coverage<span class=\"w\"> </span>run<span class=\"w\"> </span>./runtests.py<span class=\"w\"> </span>--settings<span class=\"o\">=</span>test_sqlite<span class=\"w\"> </span>--parallel<span class=\"o\">=</span><span class=\"m\">1</span>\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-12-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> coverage run runtests.py --settings=test_sqlite --parallel=1\n</code></pre></div></div></div>\n<p>After running coverage, generate the html report by running:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-13-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>coverage<span class=\"w\"> </span>html\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-13-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> coverage html\n</code></pre></div></div></div>\n<p>运行Django测试的覆盖率时，随附的 <code class=\"docutils literal notranslate\"><span class=\"pre\">.coveragerc</span></code> 配置文件将 <code class=\"docutils literal notranslate\"><span class=\"pre\">coverage_html</span></code> 定义为报告的输出目录，并且还排除了与结果无关的几个目录（Django中包含的测试代码或外部代码）。</p>\n</section>\n</section>\n<section id=\"contrib-apps\">\n<span id=\"id1\"></span><h2>Contrib 应用程序<a class=\"heading-anchor\" href=\"#contrib-apps\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Tests for contrib apps can be found in the <code class=\"docutils literal notranslate\"><span class=\"pre\">tests/</span></code> directory, typically\nunder <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;app_name&gt;_tests</span></code>. For example, tests for <code class=\"docutils literal notranslate\"><span class=\"pre\">contrib.auth</span></code> are located\nin <code class=\"docutils literal notranslate\"><span class=\"pre\">tests/auth_tests</span></code>.</p>\n</section>\n<section id=\"troubleshooting\">\n<span id=\"troubleshooting-unit-tests\"></span><h2>错误调试<a class=\"heading-anchor\" href=\"#troubleshooting\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"test-suite-hangs-or-shows-failures-on-main-branch\">\n<h3>Test suite hangs or shows failures on <code class=\"docutils literal notranslate\"><span class=\"pre\">main</span></code> branch<a class=\"heading-anchor\" href=\"#test-suite-hangs-or-shows-failures-on-main-branch\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Ensure you have the latest point release of a <a class=\"reference internal\" href=\"/zh-hans/4.1/faq/install/#faq-python-version-support\"><span class=\"std std-ref\">supported Python version</span></a>, since there are often bugs in earlier versions\nthat may cause the test suite to fail or hang.</p>\n<p>On <strong>macOS</strong> (High Sierra and newer versions), you might see this message\nlogged, after which the tests hang:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">objc</span><span class=\"p\">[</span><span class=\"mi\">42074</span><span class=\"p\">]:</span> <span class=\"o\">+</span><span class=\"p\">[</span><span class=\"n\">__NSPlaceholderDate</span> <span class=\"n\">initialize</span><span class=\"p\">]</span> <span class=\"n\">may</span> <span class=\"n\">have</span> <span class=\"n\">been</span> <span class=\"ow\">in</span> <span class=\"n\">progress</span> <span class=\"ow\">in</span>\n<span class=\"n\">another</span> <span class=\"n\">thread</span> <span class=\"n\">when</span> <span class=\"n\">fork</span><span class=\"p\">()</span> <span class=\"n\">was</span> <span class=\"n\">called</span><span class=\"o\">.</span>\n</code></pre></div>\n<p>To avoid this set a <code class=\"docutils literal notranslate\"><span class=\"pre\">OBJC_DISABLE_INITIALIZE_FORK_SAFETY</span></code> environment\nvariable, for example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES ./runtests.py\n</code></pre></div>\n<p>Or add <code class=\"docutils literal notranslate\"><span class=\"pre\">export</span> <span class=\"pre\">OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES</span></code> to your shell's\nstartup file (e.g. <code class=\"docutils literal notranslate\"><span class=\"pre\">~/.profile</span></code>).</p>\n</section>\n<section id=\"many-test-failures-with-unicodeencodeerror\">\n<h3>Many test failures with <code class=\"docutils literal notranslate\"><span class=\"pre\">UnicodeEncodeError</span></code><a class=\"heading-anchor\" href=\"#many-test-failures-with-unicodeencodeerror\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If the <code class=\"docutils literal notranslate\"><span class=\"pre\">locales</span></code> package is not installed, some tests will fail with a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">UnicodeEncodeError</span></code>.</p>\n<p>You can resolve this on Debian-based systems, for example, by running:</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>apt-get<span class=\"w\"> </span>install<span class=\"w\"> </span>locales\n<span class=\"gp\">$ </span>dpkg-reconfigure<span class=\"w\"> </span>locales\n</code></pre></div>\n<p>You can resolve this for macOS systems by configuring your shell's locale:</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span><span class=\"nb\">export</span><span class=\"w\"> </span><span class=\"nv\">LANG</span><span class=\"o\">=</span><span class=\"s2\">&quot;en_US.UTF-8&quot;</span>\n<span class=\"gp\">$ </span><span class=\"nb\">export</span><span class=\"w\"> </span><span class=\"nv\">LC_ALL</span><span class=\"o\">=</span><span class=\"s2\">&quot;en_US.UTF-8&quot;</span>\n</code></pre></div>\n<p>Run the <code class=\"docutils literal notranslate\"><span class=\"pre\">locale</span></code> command to confirm the change. Optionally, add those export\ncommands to your shell's startup file (e.g. <code class=\"docutils literal notranslate\"><span class=\"pre\">~/.bashrc</span></code> for Bash) to avoid\nhaving to retype them.</p>\n</section>\n<section id=\"tests-that-only-fail-in-combination\">\n<h3>Tests that only fail in combination<a class=\"heading-anchor\" href=\"#tests-that-only-fail-in-combination\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>In case a test passes when run in isolation but fails within the whole suite,\nwe have some tools to help analyze the problem.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">--bisect</span></code> option of <code class=\"docutils literal notranslate\"><span class=\"pre\">runtests.py</span></code> will run the failing test while\nhalving the test set it is run together with on each iteration, often making\nit possible to identify a small number of tests that may be related to the\nfailure.</p>\n<p>For example, suppose that the failing test that works on its own is\n<code class=\"docutils literal notranslate\"><span class=\"pre\">ModelTest.test_eq</span></code>, then using:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-14-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>--bisect<span class=\"w\"> </span>basic.tests.ModelTest.test_eq\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-14-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> runtests.py --bisect basic.tests.ModelTest.test_eq\n</code></pre></div></div></div>\n<p>will try to determine a test that interferes with the given one. First, the\ntest is run with the first half of the test suite. If a failure occurs, the\nfirst half of the test suite is split in two groups and each group is then run\nwith the specified test. If there is no failure with the first half of the test\nsuite, the second half of the test suite is run with the specified test and\nsplit appropriately as described earlier. The process repeats until the set of\nfailing tests is minimized.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">--pair</span></code> option runs the given test alongside every other test from the\nsuite, letting you check if another test has side-effects that cause the\nfailure. So:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-15-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>--pair<span class=\"w\"> </span>basic.tests.ModelTest.test_eq\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-15-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> runtests.py --pair basic.tests.ModelTest.test_eq\n</code></pre></div></div></div>\n<p>will pair <code class=\"docutils literal notranslate\"><span class=\"pre\">test_eq</span></code> with every test label.</p>\n<p>With both <code class=\"docutils literal notranslate\"><span class=\"pre\">--bisect</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">--pair</span></code>, if you already suspect which cases\nmight be responsible for the failure, you may limit tests to be cross-analyzed\nby <a class=\"reference internal\" href=\"#runtests-specifying-labels\"><span class=\"std std-ref\">specifying further test labels</span></a> after\nthe first one:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-16-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>--pair<span class=\"w\"> </span>basic.tests.ModelTest.test_eq<span class=\"w\"> </span>queries<span class=\"w\"> </span>transactions\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-16-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> runtests.py --pair basic.tests.ModelTest.test_eq queries transactions\n</code></pre></div></div></div>\n<p>You can also try running any set of tests in a random or reverse order using\nthe <code class=\"docutils literal notranslate\"><span class=\"pre\">--shuffle</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">--reverse</span></code> options. This can help verify that\nexecuting tests in a different order does not cause any trouble:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-17-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>basic<span class=\"w\"> </span>--shuffle\n<span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>basic<span class=\"w\"> </span>--reverse\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-17-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> runtests.py basic --shuffle\n<span class=\"gp\">...\\&gt;</span> runtests.py basic --reverse\n</code></pre></div></div></div>\n</section>\n<section id=\"seeing-the-sql-queries-run-during-a-test\">\n<h3>Seeing the SQL queries run during a test<a class=\"heading-anchor\" href=\"#seeing-the-sql-queries-run-during-a-test\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you wish to examine the SQL being run in failing tests, you can turn on\n<a class=\"reference internal\" href=\"/zh-hans/4.1/ref/logging/#django-db-logger\"><span class=\"std std-ref\">SQL logging</span></a> using the <code class=\"docutils literal notranslate\"><span class=\"pre\">--debug-sql</span></code> option. If you\ncombine this with <code class=\"docutils literal notranslate\"><span class=\"pre\">--verbosity=2</span></code>, all SQL queries will be output:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-18-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>basic<span class=\"w\"> </span>--debug-sql\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-18-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> runtests.py basic --debug-sql\n</code></pre></div></div></div>\n</section>\n<section id=\"seeing-the-full-traceback-of-a-test-failure\">\n<h3>Seeing the full traceback of a test failure<a class=\"heading-anchor\" href=\"#seeing-the-full-traceback-of-a-test-failure\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>By default tests are run in parallel with one process per core. When the tests\nare run in parallel, however, you'll only see a truncated traceback for any\ntest failures. You can adjust this behavior with the <code class=\"docutils literal notranslate\"><span class=\"pre\">--parallel</span></code> option:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-19-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>basic<span class=\"w\"> </span>--parallel<span class=\"o\">=</span><span class=\"m\">1</span>\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-19-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> runtests.py basic --parallel=1\n</code></pre></div></div></div>\n<p>You can also use the <span class=\"target\" id=\"index-2\"></span><a class=\"reference internal\" href=\"/zh-hans/4.1/ref/django-admin/#envvar-DJANGO_TEST_PROCESSES\"><code class=\"xref std std-envvar docutils literal notranslate\"><span class=\"pre\">DJANGO_TEST_PROCESSES</span></code></a> environment variable for\nthis purpose.</p>\n</section>\n</section>\n<section id=\"tips-for-writing-tests\">\n<h2>Tips for writing tests<a class=\"heading-anchor\" href=\"#tips-for-writing-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"isolating-model-registration\">\n<h3>Isolating model registration<a class=\"heading-anchor\" href=\"#isolating-model-registration\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>To avoid polluting the global <a class=\"reference internal\" href=\"/zh-hans/4.1/ref/applications/#django.apps.apps\" title=\"django.apps.apps\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">apps</span></code></a> registry and prevent\nunnecessary table creation, models defined in a test method should be bound to\na temporary <code class=\"docutils literal notranslate\"><span class=\"pre\">Apps</span></code> instance. To do this, use the\n<a class=\"reference internal\" href=\"/zh-hans/4.1/topics/testing/tools/#django.test.utils.isolate_apps\" title=\"django.test.utils.isolate_apps\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">isolate_apps()</span></code></a> decorator:</p>\n<div class=\"code-block\" data-language=\"python\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.db</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">models</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.test</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">SimpleTestCase</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.test.utils</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">isolate_apps</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">TestModelDefinition</span><span class=\"p\">(</span><span class=\"n\">SimpleTestCase</span><span class=\"p\">):</span>\n    <span class=\"nd\">@isolate_apps</span><span class=\"p\">(</span><span class=\"s1\">&#39;app_label&#39;</span><span class=\"p\">)</span>\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">test_model_definition</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">TestModel</span><span class=\"p\">(</span><span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">Model</span><span class=\"p\">):</span>\n            <span class=\"k\">pass</span>\n        <span class=\"o\">...</span>\n</code></pre></div>\n<aside class=\"admonition-setting-app-label admonition\">\n<p class=\"admonition-title\">Setting <code class=\"docutils literal notranslate\"><span class=\"pre\">app_label</span></code></p>\n<p>Models defined in a test method with no explicit\n<a class=\"reference internal\" href=\"/zh-hans/4.1/ref/models/options/#django.db.models.Options.app_label\" title=\"django.db.models.Options.app_label\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">app_label</span></code></a> are automatically assigned the\nlabel of the app in which their test class is located.</p>\n<p>In order to make sure the models defined within the context of\n<a class=\"reference internal\" href=\"/zh-hans/4.1/topics/testing/tools/#django.test.utils.isolate_apps\" title=\"django.test.utils.isolate_apps\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">isolate_apps()</span></code></a> instances are correctly\ninstalled, you should pass the set of targeted <code class=\"docutils literal notranslate\"><span class=\"pre\">app_label</span></code> as arguments:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\"><span class=\"pre\">tests/app_label/tests.py</span></code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.db</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">models</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.test</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">SimpleTestCase</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.test.utils</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">isolate_apps</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">TestModelDefinition</span><span class=\"p\">(</span><span class=\"n\">SimpleTestCase</span><span class=\"p\">):</span>\n    <span class=\"nd\">@isolate_apps</span><span class=\"p\">(</span><span class=\"s1\">&#39;app_label&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;other_app_label&#39;</span><span class=\"p\">)</span>\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">test_model_definition</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"c1\"># This model automatically receives app_label=&#39;app_label&#39;</span>\n        <span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">TestModel</span><span class=\"p\">(</span><span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">Model</span><span class=\"p\">):</span>\n            <span class=\"k\">pass</span>\n\n        <span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">OtherAppModel</span><span class=\"p\">(</span><span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">Model</span><span class=\"p\">):</span>\n            <span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Meta</span><span class=\"p\">:</span>\n                <span class=\"n\">app_label</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;other_app_label&#39;</span>\n        <span class=\"o\">...</span>\n</code></pre></figure>\n</aside>\n</section>\n</section>","rootId":"unit-tests","toc":[{"title":"运行单元测试","anchor":"running-the-unit-tests","children":[{"title":"快速上手","anchor":"quickstart","children":[]},{"title":"Running tests using tox","anchor":"running-tests-using-tox","children":[{"title":"Testing other Python versions and database backends","anchor":"testing-other-python-versions-and-database-backends","children":[]},{"title":"运行 JavaScript 测试集","anchor":"running-the-javascript-tests","children":[]}]},{"title":"Running tests using django-docker-box","anchor":"running-tests-using-django-docker-box","children":[]},{"title":"使用另一个 settings 配置模块","anchor":"using-another-settings-module","children":[]},{"title":"执行一部分测试","anchor":"running-only-some-of-the-tests","children":[]},{"title":"运行 Selenium 测试","anchor":"running-the-selenium-tests","children":[]},{"title":"运行所有测试","anchor":"running-all-the-tests","children":[]},{"title":"代码覆盖率","anchor":"code-coverage","children":[]}]},{"title":"Contrib 应用程序","anchor":"contrib-apps","children":[]},{"title":"错误调试","anchor":"troubleshooting","children":[{"title":"Test suite hangs or shows failures on main branch","anchor":"test-suite-hangs-or-shows-failures-on-main-branch","children":[]},{"title":"Many test failures with UnicodeEncodeError","anchor":"many-test-failures-with-unicodeencodeerror","children":[]},{"title":"Tests that only fail in combination","anchor":"tests-that-only-fail-in-combination","children":[]},{"title":"Seeing the SQL queries run during a test","anchor":"seeing-the-sql-queries-run-during-a-test","children":[]},{"title":"Seeing the full traceback of a test failure","anchor":"seeing-the-full-traceback-of-a-test-failure","children":[]}]},{"title":"Tips for writing tests","anchor":"tips-for-writing-tests","children":[{"title":"Isolating model registration","anchor":"isolating-model-registration","children":[]}]}],"breadcrumbs":[{"docname":"internals/index","title":"Django internals","url":"/zh-hans/4.1/internals/"},{"docname":"internals/contributing/index","title":"为 Django 做贡献","url":"/zh-hans/4.1/internals/contributing/"},{"docname":"internals/contributing/writing-code/index","title":"编写代码","url":"/zh-hans/4.1/internals/contributing/writing-code/"}],"prev":{"docname":"internals/contributing/writing-code/coding-style","title":"编码风格","url":"/zh-hans/4.1/internals/contributing/writing-code/coding-style/"},"next":{"docname":"internals/contributing/writing-code/submitting-patches","title":"提交补丁","url":"/zh-hans/4.1/internals/contributing/writing-code/submitting-patches/"},"formats":{"html":"/zh-hans/4.1/internals/contributing/writing-code/unit-tests/","markdown":"/zh-hans/4.1/internals/contributing/writing-code/unit-tests.md","json":"/zh-hans/4.1/internals/contributing/writing-code/unit-tests.json"},"source":"https://github.com/django/django/blob/stable/4.1.x/docs/internals/contributing/writing-code/unit-tests.txt","official":"https://docs.djangoproject.com/zh-hans/4.1/internals/contributing/writing-code/unit-tests/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}