{"title":"Advanced testing topics","version":"1.11","locale":"ja","docname":"topics/testing/advanced","url":"/ja/1.11/topics/testing/advanced/","canonical":"https://djangodocs.dev/ja/1.11/topics/testing/advanced/","summary":"The RequestFactory shares the same API as the test client. However, instead of behaving like a browser, the RequestFactory provides a way to generate a…","html":"<h1>Advanced testing topics<a class=\"heading-anchor\" href=\"#advanced-testing-topics\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<section id=\"the-request-factory\">\n<h2>The request factory<a class=\"heading-anchor\" href=\"#the-request-factory\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.test.RequestFactory\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">RequestFactory</span><a class=\"heading-anchor\" href=\"#django.test.RequestFactory\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The <a class=\"reference internal\" href=\"#django.test.RequestFactory\" title=\"django.test.RequestFactory\"><code class=\"xref py py-class docutils literal notranslate\">RequestFactory</code></a> shares the same API as\nthe test client. However, instead of behaving like a browser, the\nRequestFactory provides a way to generate a request instance that can\nbe used as the first argument to any view. This means you can test a\nview function the same way as you would test any other function -- as\na black box, with exactly known inputs, testing for specific outputs.</p>\n<p>The API for the <a class=\"reference internal\" href=\"#django.test.RequestFactory\" title=\"django.test.RequestFactory\"><code class=\"xref py py-class docutils literal notranslate\">RequestFactory</code></a> is a slightly\nrestricted subset of the test client API:</p>\n<ul class=\"simple\">\n<li><p>It only has access to the HTTP methods <a class=\"reference internal\" href=\"/ja/1.11/topics/testing/tools/#django.test.Client.get\" title=\"django.test.Client.get\"><code class=\"xref py py-meth docutils literal notranslate\">get()</code></a>,\n<a class=\"reference internal\" href=\"/ja/1.11/topics/testing/tools/#django.test.Client.post\" title=\"django.test.Client.post\"><code class=\"xref py py-meth docutils literal notranslate\">post()</code></a>, <a class=\"reference internal\" href=\"/ja/1.11/topics/testing/tools/#django.test.Client.put\" title=\"django.test.Client.put\"><code class=\"xref py py-meth docutils literal notranslate\">put()</code></a>,\n<a class=\"reference internal\" href=\"/ja/1.11/topics/testing/tools/#django.test.Client.delete\" title=\"django.test.Client.delete\"><code class=\"xref py py-meth docutils literal notranslate\">delete()</code></a>, <a class=\"reference internal\" href=\"/ja/1.11/topics/testing/tools/#django.test.Client.head\" title=\"django.test.Client.head\"><code class=\"xref py py-meth docutils literal notranslate\">head()</code></a>,\n<a class=\"reference internal\" href=\"/ja/1.11/topics/testing/tools/#django.test.Client.options\" title=\"django.test.Client.options\"><code class=\"xref py py-meth docutils literal notranslate\">options()</code></a>, and <a class=\"reference internal\" href=\"/ja/1.11/topics/testing/tools/#django.test.Client.trace\" title=\"django.test.Client.trace\"><code class=\"xref py py-meth docutils literal notranslate\">trace()</code></a>.</p></li>\n<li><p>These methods accept all the same arguments <em>except</em> for\n<code class=\"docutils literal notranslate\">follow</code>. Since this is just a factory for producing\nrequests, it's up to you to handle the response.</p></li>\n<li><p>It does not support middleware. Session and authentication\nattributes must be supplied by the test itself if required\nfor the view to function properly.</p></li>\n</ul>\n<section id=\"example\">\n<h3>カスタマイズ例<a class=\"heading-anchor\" href=\"#example\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The following is a simple unit test using the request factory:</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=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.models</span> <span class=\"kn\">import</span> AnonymousUser<span class=\"p\">,</span> User\n<span class=\"kn\">from</span> <span class=\"nn\">django.test</span> <span class=\"kn\">import</span> TestCase<span class=\"p\">,</span> RequestFactory\n\n<span class=\"kn\">from</span> <span class=\"nn\">.views</span> <span class=\"kn\">import</span> MyView<span class=\"p\">,</span> my_view\n\n<span class=\"k\">class</span> <span class=\"nc\">SimpleTest</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">setUp</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"c1\"># Every test needs access to the request factory.</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>factory <span class=\"o\">=</span> RequestFactory<span class=\"p\">()</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>user <span class=\"o\">=</span> User<span class=\"o\">.</span>objects<span class=\"o\">.</span>create_user<span class=\"p\">(</span>\n            username<span class=\"o\">=</span><span class=\"s1\">&#39;jacob&#39;</span><span class=\"p\">,</span> email<span class=\"o\">=</span><span class=\"s1\">&#39;jacob@…&#39;</span><span class=\"p\">,</span> password<span class=\"o\">=</span><span class=\"s1\">&#39;top_secret&#39;</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">test_details</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"c1\"># Create an instance of a GET request.</span>\n        request <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>factory<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s1\">&#39;/customer/details&#39;</span><span class=\"p\">)</span>\n\n        <span class=\"c1\"># Recall that middleware are not supported. You can simulate a</span>\n        <span class=\"c1\"># logged-in user by setting request.user manually.</span>\n        request<span class=\"o\">.</span>user <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>user\n\n        <span class=\"c1\"># Or you can simulate an anonymous user by setting request.user to</span>\n        <span class=\"c1\"># an AnonymousUser instance.</span>\n        request<span class=\"o\">.</span>user <span class=\"o\">=</span> AnonymousUser<span class=\"p\">()</span>\n\n        <span class=\"c1\"># Test my_view() as if it were deployed at /customer/details</span>\n        response <span class=\"o\">=</span> my_view<span class=\"p\">(</span>request<span class=\"p\">)</span>\n        <span class=\"c1\"># Use this syntax for class-based views.</span>\n        response <span class=\"o\">=</span> MyView<span class=\"o\">.</span>as_view<span class=\"p\">()(</span>request<span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>assertEqual<span class=\"p\">(</span>response<span class=\"o\">.</span>status_code<span class=\"p\">,</span> <span class=\"mi\">200</span><span class=\"p\">)</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"tests-and-multiple-host-names\">\n<span id=\"topics-testing-advanced-multiple-hosts\"></span><h2>Tests and multiple host names<a class=\"heading-anchor\" href=\"#tests-and-multiple-host-names\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-ALLOWED_HOSTS\"><code class=\"xref std std-setting docutils literal notranslate\">ALLOWED_HOSTS</code></a> setting is validated when running tests. This\nallows the test client to differentiate between internal and external URLs.</p>\n<p>Projects that support multitenancy or otherwise alter business logic based on\nthe request's host and use custom host names in tests must include those hosts\nin <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-ALLOWED_HOSTS\"><code class=\"xref std std-setting docutils literal notranslate\">ALLOWED_HOSTS</code></a>.</p>\n<p>The first and simplest option to do so is to add the hosts to your settings\nfile. For example, the test suite for docs.djangoproject.com includes the\nfollowing:</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=\"kn\">from</span> <span class=\"nn\">django.test</span> <span class=\"kn\">import</span> TestCase\n\n<span class=\"k\">class</span> <span class=\"nc\">SearchFormTestCase</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_empty_get</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        response <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>client<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s1\">&#39;/en/dev/search/&#39;</span><span class=\"p\">,</span> HTTP_HOST<span class=\"o\">=</span><span class=\"s1\">&#39;docs.djangoproject.dev:8000&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>assertEqual<span class=\"p\">(</span>response<span class=\"o\">.</span>status_code<span class=\"p\">,</span> <span class=\"mi\">200</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>and the settings file includes a list of the domains supported by the project:</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>ALLOWED_HOSTS <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"s1\">&#39;www.djangoproject.dev&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;docs.djangoproject.dev&#39;</span><span class=\"p\">,</span>\n    <span class=\"o\">...</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>Another option is to add the required hosts to <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-ALLOWED_HOSTS\"><code class=\"xref std std-setting docutils literal notranslate\">ALLOWED_HOSTS</code></a> using\n<a class=\"reference internal\" href=\"/ja/1.11/topics/testing/tools/#django.test.override_settings\" title=\"django.test.override_settings\"><code class=\"xref py py-meth docutils literal notranslate\">override_settings()</code></a> or\n<a class=\"reference internal\" href=\"/ja/1.11/topics/testing/tools/#django.test.SimpleTestCase.modify_settings\" title=\"django.test.SimpleTestCase.modify_settings\"><code class=\"xref py py-attr docutils literal notranslate\">modify_settings()</code></a>. This option may be\npreferable in standalone apps that can't package their own settings file or\nfor projects where the list of domains is not static (e.g., subdomains for\nmultitenancy). For example, you could write a test for the domain\n<code class=\"docutils literal notranslate\">http://otherserver/</code> as follows:</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=\"kn\">from</span> <span class=\"nn\">django.test</span> <span class=\"kn\">import</span> TestCase<span class=\"p\">,</span> override_settings\n\n<span class=\"k\">class</span> <span class=\"nc\">MultiDomainTestCase</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    <span class=\"nd\">@override_settings</span><span class=\"p\">(</span>ALLOWED_HOSTS<span class=\"o\">=</span><span class=\"p\">[</span><span class=\"s1\">&#39;otherserver&#39;</span><span class=\"p\">])</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_other_domain</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        response <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>client<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s1\">&#39;http://otherserver/foo/bar/&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Disabling <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-ALLOWED_HOSTS\"><code class=\"xref std std-setting docutils literal notranslate\">ALLOWED_HOSTS</code></a> checking (<code class=\"docutils literal notranslate\">ALLOWED_HOSTS = ['*']</code>) when\nrunning tests prevents the test client from raising a helpful error message if\nyou follow a redirect to an external URL.</p>\n<aside class=\"version-note version-changed\" data-version=\"1.11\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 1.11</p><p>Older versions didn't validate <code class=\"docutils literal notranslate\">ALLOWED_HOSTS</code> while testing so these\ntechniques weren't necessary.</p>\n</aside>\n</section>\n<section id=\"tests-and-multiple-databases\">\n<span id=\"topics-testing-advanced-multidb\"></span><h2>Tests and multiple databases<a class=\"heading-anchor\" href=\"#tests-and-multiple-databases\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"testing-primary-replica-configurations\">\n<span id=\"topics-testing-primaryreplica\"></span><h3>Testing primary/replica configurations<a class=\"heading-anchor\" href=\"#testing-primary-replica-configurations\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you're testing a multiple database configuration with primary/replica\n(referred to as master/slave by some databases) replication, this strategy of\ncreating test databases poses a problem.\nWhen the test databases are created, there won't be any replication,\nand as a result, data created on the primary won't be seen on the\nreplica.</p>\n<p>To compensate for this, Django allows you to define that a database is\na <em>test mirror</em>. Consider the following (simplified) example database\nconfiguration:</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>DATABASES <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"s1\">&#39;default&#39;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s1\">&#39;ENGINE&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;django.db.backends.mysql&#39;</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;NAME&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;myproject&#39;</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;HOST&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;dbprimary&#39;</span><span class=\"p\">,</span>\n         <span class=\"c1\"># ... plus some other settings</span>\n    <span class=\"p\">},</span>\n    <span class=\"s1\">&#39;replica&#39;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s1\">&#39;ENGINE&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;django.db.backends.mysql&#39;</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;NAME&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;myproject&#39;</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;HOST&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;dbreplica&#39;</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;TEST&#39;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s1\">&#39;MIRROR&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;default&#39;</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n        <span class=\"c1\"># ... plus some other settings</span>\n    <span class=\"p\">}</span>\n<span class=\"p\">}</span>\n</code></pre></div>\n<p>In this setup, we have two database servers: <code class=\"docutils literal notranslate\">dbprimary</code>, described\nby the database alias <code class=\"docutils literal notranslate\">default</code>, and <code class=\"docutils literal notranslate\">dbreplica</code> described by the\nalias <code class=\"docutils literal notranslate\">replica</code>. As you might expect, <code class=\"docutils literal notranslate\">dbreplica</code> has been configured\nby the database administrator as a read replica of <code class=\"docutils literal notranslate\">dbprimary</code>, so in\nnormal activity, any write to <code class=\"docutils literal notranslate\">default</code> will appear on <code class=\"docutils literal notranslate\">replica</code>.</p>\n<p>If Django created two independent test databases, this would break any\ntests that expected replication to occur. However, the <code class=\"docutils literal notranslate\">replica</code>\ndatabase has been configured as a test mirror (using the\n<a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-TEST_MIRROR\"><code class=\"xref std std-setting docutils literal notranslate\">MIRROR</code></a> test setting), indicating that under\ntesting, <code class=\"docutils literal notranslate\">replica</code> should be treated as a mirror of <code class=\"docutils literal notranslate\">default</code>.</p>\n<p>When the test environment is configured, a test version of <code class=\"docutils literal notranslate\">replica</code>\nwill <em>not</em> be created. Instead the connection to <code class=\"docutils literal notranslate\">replica</code>\nwill be redirected to point at <code class=\"docutils literal notranslate\">default</code>. As a result, writes to\n<code class=\"docutils literal notranslate\">default</code> will appear on <code class=\"docutils literal notranslate\">replica</code> -- but because they are actually\nthe same database, not because there is data replication between the\ntwo databases.</p>\n</section>\n<section id=\"controlling-creation-order-for-test-databases\">\n<span id=\"topics-testing-creation-dependencies\"></span><h3>Controlling creation order for test databases<a class=\"heading-anchor\" href=\"#controlling-creation-order-for-test-databases\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>By default, Django will assume all databases depend on the <code class=\"docutils literal notranslate\">default</code>\ndatabase and therefore always create the <code class=\"docutils literal notranslate\">default</code> database first.\nHowever, no guarantees are made on the creation order of any other\ndatabases in your test setup.</p>\n<p>If your database configuration requires a specific creation order, you\ncan specify the dependencies that exist using the <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-TEST_DEPENDENCIES\"><code class=\"xref std std-setting docutils literal notranslate\">DEPENDENCIES</code></a> test setting. Consider the following (simplified)\nexample database configuration:</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>DATABASES <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"s1\">&#39;default&#39;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"c1\"># ... db settings</span>\n        <span class=\"s1\">&#39;TEST&#39;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s1\">&#39;DEPENDENCIES&#39;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s1\">&#39;diamonds&#39;</span><span class=\"p\">],</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n    <span class=\"s1\">&#39;diamonds&#39;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"c1\"># ... db settings</span>\n        <span class=\"s1\">&#39;TEST&#39;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s1\">&#39;DEPENDENCIES&#39;</span><span class=\"p\">:</span> <span class=\"p\">[],</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n    <span class=\"s1\">&#39;clubs&#39;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"c1\"># ... db settings</span>\n        <span class=\"s1\">&#39;TEST&#39;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s1\">&#39;DEPENDENCIES&#39;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s1\">&#39;diamonds&#39;</span><span class=\"p\">],</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n    <span class=\"s1\">&#39;spades&#39;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"c1\"># ... db settings</span>\n        <span class=\"s1\">&#39;TEST&#39;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s1\">&#39;DEPENDENCIES&#39;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s1\">&#39;diamonds&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;hearts&#39;</span><span class=\"p\">],</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n    <span class=\"s1\">&#39;hearts&#39;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"c1\"># ... db settings</span>\n        <span class=\"s1\">&#39;TEST&#39;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s1\">&#39;DEPENDENCIES&#39;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s1\">&#39;diamonds&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;clubs&#39;</span><span class=\"p\">],</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">}</span>\n<span class=\"p\">}</span>\n</code></pre></div>\n<p>Under this configuration, the <code class=\"docutils literal notranslate\">diamonds</code> database will be created first,\nas it is the only database alias without dependencies. The <code class=\"docutils literal notranslate\">default</code> and\n<code class=\"docutils literal notranslate\">clubs</code> alias will be created next (although the order of creation of this\npair is not guaranteed), then <code class=\"docutils literal notranslate\">hearts</code>, and finally <code class=\"docutils literal notranslate\">spades</code>.</p>\n<p>If there are any circular dependencies in the <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-TEST_DEPENDENCIES\"><code class=\"xref std std-setting docutils literal notranslate\">DEPENDENCIES</code></a> definition, an\n<a class=\"reference internal\" href=\"/ja/1.11/ref/exceptions/#django.core.exceptions.ImproperlyConfigured\" title=\"django.core.exceptions.ImproperlyConfigured\"><code class=\"xref py py-exc docutils literal notranslate\">ImproperlyConfigured</code></a> exception will be raised.</p>\n</section>\n</section>\n<section id=\"advanced-features-of-transactiontestcase\">\n<h2>Advanced features of <code class=\"docutils literal notranslate\">TransactionTestCase</code><a class=\"heading-anchor\" href=\"#advanced-features-of-transactiontestcase\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.test.TransactionTestCase.available_apps\">\n<span class=\"sig-prename descclassname\">TransactionTestCase.</span><span class=\"sig-name descname\">available_apps</span><a class=\"heading-anchor\" href=\"#django.test.TransactionTestCase.available_apps\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p>This attribute is a private API. It may be changed or removed without\na deprecation period in the future, for instance to accommodate changes\nin application loading.</p>\n<p>It's used to optimize Django's own test suite, which contains hundreds\nof models but no relations between models in different applications.</p>\n</aside>\n<p>By default, <code class=\"docutils literal notranslate\">available_apps</code> is set to <code class=\"docutils literal notranslate\">None</code>. After each test, Django\ncalls <a class=\"reference internal\" href=\"/ja/1.11/ref/django-admin/#django-admin-flush\"><code class=\"xref std std-djadmin docutils literal notranslate\">flush</code></a> to reset the database state. This empties all tables\nand emits the <a class=\"reference internal\" href=\"/ja/1.11/ref/signals/#django.db.models.signals.post_migrate\" title=\"django.db.models.signals.post_migrate\"><code class=\"xref py py-data docutils literal notranslate\">post_migrate</code></a> signal, which\nre-creates one content type and three permissions for each model. This\noperation gets expensive proportionally to the number of models.</p>\n<p>Setting <code class=\"docutils literal notranslate\">available_apps</code> to a list of applications instructs Django to\nbehave as if only the models from these applications were available. The\nbehavior of <code class=\"docutils literal notranslate\">TransactionTestCase</code> changes as follows:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/ja/1.11/ref/signals/#django.db.models.signals.post_migrate\" title=\"django.db.models.signals.post_migrate\"><code class=\"xref py py-data docutils literal notranslate\">post_migrate</code></a> is fired before each\ntest to create the content types and permissions for each model in\navailable apps, in case they're missing.</p></li>\n<li><p>After each test, Django empties only tables corresponding to models in\navailable apps. However, at the database level, truncation may cascade to\nrelated models in unavailable apps. Furthermore\n<a class=\"reference internal\" href=\"/ja/1.11/ref/signals/#django.db.models.signals.post_migrate\" title=\"django.db.models.signals.post_migrate\"><code class=\"xref py py-data docutils literal notranslate\">post_migrate</code></a> isn't fired; it will be\nfired by the next <code class=\"docutils literal notranslate\">TransactionTestCase</code>, after the correct set of\napplications is selected.</p></li>\n</ul>\n<p>Since the database isn't fully flushed, if a test creates instances of\nmodels not included in <code class=\"docutils literal notranslate\">available_apps</code>, they will leak and they may\ncause unrelated tests to fail. Be careful with tests that use sessions;\nthe default session engine stores them in the database.</p>\n<p>Since <a class=\"reference internal\" href=\"/ja/1.11/ref/signals/#django.db.models.signals.post_migrate\" title=\"django.db.models.signals.post_migrate\"><code class=\"xref py py-data docutils literal notranslate\">post_migrate</code></a> isn't emitted after\nflushing the database, its state after a <code class=\"docutils literal notranslate\">TransactionTestCase</code> isn't the\nsame as after a <code class=\"docutils literal notranslate\">TestCase</code>: it's missing the rows created by listeners\nto <a class=\"reference internal\" href=\"/ja/1.11/ref/signals/#django.db.models.signals.post_migrate\" title=\"django.db.models.signals.post_migrate\"><code class=\"xref py py-data docutils literal notranslate\">post_migrate</code></a>. Considering the\n<a class=\"reference internal\" href=\"/ja/1.11/topics/testing/overview/#order-of-tests\"><span class=\"std std-ref\">order in which tests are executed</span></a>, this isn't an\nissue, provided either all <code class=\"docutils literal notranslate\">TransactionTestCase</code> in a given test suite\ndeclare <code class=\"docutils literal notranslate\">available_apps</code>, or none of them.</p>\n<p><code class=\"docutils literal notranslate\">available_apps</code> is mandatory in Django's own test suite.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.test.TransactionTestCase.reset_sequences\">\n<span class=\"sig-prename descclassname\">TransactionTestCase.</span><span class=\"sig-name descname\">reset_sequences</span><a class=\"heading-anchor\" href=\"#django.test.TransactionTestCase.reset_sequences\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Setting <code class=\"docutils literal notranslate\">reset_sequences = True</code> on a <code class=\"docutils literal notranslate\">TransactionTestCase</code> will make\nsure sequences are always reset before the test run:</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=\"k\">class</span> <span class=\"nc\">TestsThatDependsOnPrimaryKeySequences</span><span class=\"p\">(</span>TransactionTestCase<span class=\"p\">):</span>\n    reset_sequences <span class=\"o\">=</span> <span class=\"kc\">True</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">test_animal_pk</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        lion <span class=\"o\">=</span> Animal<span class=\"o\">.</span>objects<span class=\"o\">.</span>create<span class=\"p\">(</span>name<span class=\"o\">=</span><span class=\"s2\">&quot;lion&quot;</span><span class=\"p\">,</span> sound<span class=\"o\">=</span><span class=\"s2\">&quot;roar&quot;</span><span class=\"p\">)</span>\n        <span class=\"c1\"># lion.pk is guaranteed to always be 1</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>assertEqual<span class=\"p\">(</span>lion<span class=\"o\">.</span>pk<span class=\"p\">,</span> <span class=\"mi\">1</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Unless you are explicitly testing primary keys sequence numbers, it is\nrecommended that you do not hard code primary key values in tests.</p>\n<p>Using <code class=\"docutils literal notranslate\">reset_sequences = True</code> will slow down the test, since the primary\nkey reset is an relatively expensive database operation.</p>\n</dd></dl>\n\n</section>\n<section id=\"using-the-django-test-runner-to-test-reusable-applications\">\n<span id=\"testing-reusable-applications\"></span><h2>Using the Django test runner to test reusable applications<a class=\"heading-anchor\" href=\"#using-the-django-test-runner-to-test-reusable-applications\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>If you are writing a <a class=\"reference internal\" href=\"/ja/1.11/intro/reusable-apps/\"><span class=\"doc\">reusable application</span></a>\nyou may want to use the Django test runner to run your own test suite\nand thus benefit from the Django testing infrastructure.</p>\n<p>A common practice is a <em>tests</em> directory next to the application code, with the\nfollowing structure:</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>runtests<span class=\"o\">.</span>py\npolls<span class=\"o\">/</span>\n    <span class=\"fm\">__init__</span><span class=\"o\">.</span>py\n    models<span class=\"o\">.</span>py\n    <span class=\"o\">...</span>\ntests<span class=\"o\">/</span>\n    <span class=\"fm\">__init__</span><span class=\"o\">.</span>py\n    models<span class=\"o\">.</span>py\n    test_settings<span class=\"o\">.</span>py\n    tests<span class=\"o\">.</span>py\n</code></pre></div>\n<p>Let's take a look inside a couple of those files:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>runtests.py</code></figcaption><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=\"runtests.py\"><code><span class=\"ch\">#!/usr/bin/env python</span>\n<span class=\"kn\">import</span> <span class=\"nn\">os</span>\n<span class=\"kn\">import</span> <span class=\"nn\">sys</span>\n\n<span class=\"kn\">import</span> <span class=\"nn\">django</span>\n<span class=\"kn\">from</span> <span class=\"nn\">django.conf</span> <span class=\"kn\">import</span> settings\n<span class=\"kn\">from</span> <span class=\"nn\">django.test.utils</span> <span class=\"kn\">import</span> get_runner\n\n<span class=\"k\">if</span> <span class=\"vm\">__name__</span> <span class=\"o\">==</span> <span class=\"s2\">&quot;__main__&quot;</span><span class=\"p\">:</span>\n    os<span class=\"o\">.</span>environ<span class=\"p\">[</span><span class=\"s1\">&#39;DJANGO_SETTINGS_MODULE&#39;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;tests.test_settings&#39;</span>\n    django<span class=\"o\">.</span>setup<span class=\"p\">()</span>\n    TestRunner <span class=\"o\">=</span> get_runner<span class=\"p\">(</span>settings<span class=\"p\">)</span>\n    test_runner <span class=\"o\">=</span> TestRunner<span class=\"p\">()</span>\n    failures <span class=\"o\">=</span> test_runner<span class=\"o\">.</span>run_tests<span class=\"p\">([</span><span class=\"s2\">&quot;tests&quot;</span><span class=\"p\">])</span>\n    sys<span class=\"o\">.</span>exit<span class=\"p\">(</span><span class=\"nb\">bool</span><span class=\"p\">(</span>failures<span class=\"p\">))</span>\n</code></pre></figure>\n<p>This is the script that you invoke to run the test suite. It sets up the\nDjango environment, creates the test database and runs the tests.</p>\n<p>For the sake of clarity, this example contains only the bare minimum\nnecessary to use the Django test runner. You may want to add\ncommand-line options for controlling verbosity, passing in specific test\nlabels to run, etc.</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>tests/test_settings.py</code></figcaption><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=\"tests/test_settings.py\"><code>SECRET_KEY <span class=\"o\">=</span> <span class=\"s1\">&#39;fake-key&#39;</span>\nINSTALLED_APPS <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"s2\">&quot;tests&quot;</span><span class=\"p\">,</span>\n<span class=\"p\">]</span>\n</code></pre></figure>\n<p>This file contains the <a class=\"reference internal\" href=\"/ja/1.11/topics/settings/\"><span class=\"doc\">Django settings</span></a>\nrequired to run your app's tests.</p>\n<p>Again, this is a minimal example; your tests may require additional\nsettings to run.</p>\n<p>Since the <em>tests</em> package is included in <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a> when\nrunning your tests, you can define test-only models in its <code class=\"docutils literal notranslate\">models.py</code>\nfile.</p>\n</section>\n<section id=\"using-different-testing-frameworks\">\n<span id=\"other-testing-frameworks\"></span><h2>Using different testing frameworks<a class=\"heading-anchor\" href=\"#using-different-testing-frameworks\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Clearly, <a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#module-unittest\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\">unittest</code></a> is not the only Python testing framework. While Django\ndoesn't provide explicit support for alternative frameworks, it does provide a\nway to invoke tests constructed for an alternative framework as if they were\nnormal Django tests.</p>\n<p>When you run <code class=\"docutils literal notranslate\">./manage.py test</code>, Django looks at the <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-TEST_RUNNER\"><code class=\"xref std std-setting docutils literal notranslate\">TEST_RUNNER</code></a>\nsetting to determine what to do. By default, <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-TEST_RUNNER\"><code class=\"xref std std-setting docutils literal notranslate\">TEST_RUNNER</code></a> points to\n<code class=\"docutils literal notranslate\">'django.test.runner.DiscoverRunner'</code>. This class defines the default Django\ntesting behavior. This behavior involves:</p>\n<ol class=\"arabic simple\">\n<li><p>Performing global pre-test setup.</p></li>\n<li><p>Looking for tests in any file below the current directory whose name matches\nthe pattern <code class=\"docutils literal notranslate\">test*.py</code>.</p></li>\n<li><p>Creating the test databases.</p></li>\n<li><p>Running <code class=\"docutils literal notranslate\">migrate</code> to install models and initial data into the test\ndatabases.</p></li>\n<li><p>Running the <a class=\"reference internal\" href=\"/ja/1.11/topics/checks/\"><span class=\"doc\">system checks</span></a>.</p></li>\n<li><p>Running the tests that were found.</p></li>\n<li><p>Destroying the test databases.</p></li>\n<li><p>Performing global post-test teardown.</p></li>\n</ol>\n<aside class=\"version-note version-changed\" data-version=\"1.11\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 1.11</p><p>Running the system checks was added.</p>\n</aside>\n<p>If you define your own test runner class and point <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-TEST_RUNNER\"><code class=\"xref std std-setting docutils literal notranslate\">TEST_RUNNER</code></a> at\nthat class, Django will execute your test runner whenever you run\n<code class=\"docutils literal notranslate\">./manage.py test</code>. In this way, it is possible to use any test framework\nthat can be executed from Python code, or to modify the Django test execution\nprocess to satisfy whatever testing requirements you may have.</p>\n<section id=\"defining-a-test-runner\">\n<span id=\"topics-testing-test-runner\"></span><h3>Defining a test runner<a class=\"heading-anchor\" href=\"#defining-a-test-runner\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>A test runner is a class defining a <code class=\"docutils literal notranslate\">run_tests()</code> method. Django ships\nwith a <code class=\"docutils literal notranslate\">DiscoverRunner</code> class that defines the default Django testing\nbehavior. This class defines the <code class=\"docutils literal notranslate\">run_tests()</code> entry point, plus a\nselection of other methods that are used to by <code class=\"docutils literal notranslate\">run_tests()</code> to set up,\nexecute and tear down the test suite.</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.test.runner.DiscoverRunner\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">DiscoverRunner</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">pattern<span class=\"o\">=</span><span class=\"default_value\">'test*.py'</span></em>, <em class=\"sig-param\">top_level<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">verbosity<span class=\"o\">=</span><span class=\"default_value\">1</span></em>, <em class=\"sig-param\">interactive<span class=\"o\">=</span><span class=\"default_value\">True</span></em>, <em class=\"sig-param\">failfast<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">keepdb<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">reverse<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">debug_mode<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">debug_sql<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>kwargs</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.runner.DiscoverRunner\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p><code class=\"docutils literal notranslate\">DiscoverRunner</code> will search for tests in any file matching <code class=\"docutils literal notranslate\">pattern</code>.</p>\n<p><code class=\"docutils literal notranslate\">top_level</code> can be used to specify the directory containing your\ntop-level Python modules. Usually Django can figure this out automatically,\nso it's not necessary to specify this option. If specified, it should\ngenerally be the directory containing your <code class=\"docutils literal notranslate\">manage.py</code> file.</p>\n<p><code class=\"docutils literal notranslate\">verbosity</code> determines the amount of notification and debug information\nthat will be printed to the console; <code class=\"docutils literal notranslate\">0</code> is no output, <code class=\"docutils literal notranslate\">1</code> is normal\noutput, and <code class=\"docutils literal notranslate\">2</code> is verbose output.</p>\n<p>If <code class=\"docutils literal notranslate\">interactive</code> is <code class=\"docutils literal notranslate\">True</code>, the test suite has permission to ask the\nuser for instructions when the test suite is executed. An example of this\nbehavior would be asking for permission to delete an existing test\ndatabase. If <code class=\"docutils literal notranslate\">interactive</code> is <code class=\"docutils literal notranslate\">False</code>, the test suite must be able to\nrun without any manual intervention.</p>\n<p>If <code class=\"docutils literal notranslate\">failfast</code> is <code class=\"docutils literal notranslate\">True</code>, the test suite will stop running after the\nfirst test failure is detected.</p>\n<p>If <code class=\"docutils literal notranslate\">keepdb</code> is <code class=\"docutils literal notranslate\">True</code>, the test suite will use the existing database,\nor create one if necessary. If <code class=\"docutils literal notranslate\">False</code>, a new database will be created,\nprompting the user to remove the existing one, if present.</p>\n<p>If <code class=\"docutils literal notranslate\">reverse</code> is <code class=\"docutils literal notranslate\">True</code>, test cases will be executed in the opposite\norder. This could be useful to debug tests that aren't properly isolated\nand have side effects. <a class=\"reference internal\" href=\"/ja/1.11/topics/testing/overview/#order-of-tests\"><span class=\"std std-ref\">Grouping by test class</span></a> is\npreserved when using this option.</p>\n<p><code class=\"docutils literal notranslate\">debug_mode</code> specifies what the <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\">DEBUG</code></a> setting should be\nset to prior to running tests.</p>\n<p>If <code class=\"docutils literal notranslate\">debug_sql</code> is <code class=\"docutils literal notranslate\">True</code>, failing test cases will output SQL queries\nlogged to the <a class=\"reference internal\" href=\"/ja/1.11/topics/logging/#django-db-logger\"><span class=\"std std-ref\">django.db.backends logger</span></a> as well\nas the traceback. If <code class=\"docutils literal notranslate\">verbosity</code> is <code class=\"docutils literal notranslate\">2</code>, then queries in all tests are\noutput.</p>\n<p>Django may, from time to time, extend the capabilities of the test runner\nby adding new arguments. The <code class=\"docutils literal notranslate\">**kwargs</code> declaration allows for this\nexpansion. If you subclass <code class=\"docutils literal notranslate\">DiscoverRunner</code> or write your own test\nrunner, ensure it accepts <code class=\"docutils literal notranslate\">**kwargs</code>.</p>\n<p>Your test runner may also define additional command-line options.\nCreate or override an <code class=\"docutils literal notranslate\">add_arguments(cls, parser)</code> class method and add\ncustom arguments by calling <code class=\"docutils literal notranslate\">parser.add_argument()</code> inside the method, so\nthat the <a class=\"reference internal\" href=\"/ja/1.11/ref/django-admin/#django-admin-test\"><code class=\"xref std std-djadmin docutils literal notranslate\">test</code></a> command will be able to use those arguments.</p>\n<aside class=\"version-note version-added\" data-version=\"1.11\" role=\"note\">\n<p class=\"version-note-title\">New in Django 1.11</p><p>The <code class=\"docutils literal notranslate\">debug_mode</code> keyword argument was added.</p>\n</aside>\n</dd></dl>\n\n<section id=\"attributes\">\n<h4>属性<a class=\"heading-anchor\" href=\"#attributes\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.test.runner.DiscoverRunner.test_suite\">\n<span class=\"sig-prename descclassname\">DiscoverRunner.</span><span class=\"sig-name descname\">test_suite</span><a class=\"heading-anchor\" href=\"#django.test.runner.DiscoverRunner.test_suite\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The class used to build the test suite. By default it is set to\n<code class=\"docutils literal notranslate\">unittest.TestSuite</code>. This can be overridden if you wish to implement\ndifferent logic for collecting tests.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.test.runner.DiscoverRunner.test_runner\">\n<span class=\"sig-prename descclassname\">DiscoverRunner.</span><span class=\"sig-name descname\">test_runner</span><a class=\"heading-anchor\" href=\"#django.test.runner.DiscoverRunner.test_runner\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>This is the class of the low-level test runner which is used to execute\nthe individual tests and format the results. By default it is set to\n<code class=\"docutils literal notranslate\">unittest.TextTestRunner</code>. Despite the unfortunate similarity in\nnaming conventions, this is not the same type of class as\n<code class=\"docutils literal notranslate\">DiscoverRunner</code>, which covers a broader set of responsibilities. You\ncan override this attribute to modify the way tests are run and reported.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.test.runner.DiscoverRunner.test_loader\">\n<span class=\"sig-prename descclassname\">DiscoverRunner.</span><span class=\"sig-name descname\">test_loader</span><a class=\"heading-anchor\" href=\"#django.test.runner.DiscoverRunner.test_loader\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>This is the class that loads tests, whether from TestCases or modules or\notherwise and bundles them into test suites for the runner to execute.\nBy default it is set to <code class=\"docutils literal notranslate\">unittest.defaultTestLoader</code>. You can override\nthis attribute if your tests are going to be loaded in unusual ways.</p>\n</dd></dl>\n\n</section>\n<section id=\"methods\">\n<h4>メソッド<a class=\"heading-anchor\" href=\"#methods\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.runner.DiscoverRunner.run_tests\">\n<span class=\"sig-prename descclassname\">DiscoverRunner.</span><span class=\"sig-name descname\">run_tests</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">test_labels</em>, <em class=\"sig-param\">extra_tests<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>kwargs</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.runner.DiscoverRunner.run_tests\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Run the test suite.</p>\n<p><code class=\"docutils literal notranslate\">test_labels</code> allows you to specify which tests to run and supports\nseveral formats (see <a class=\"reference internal\" href=\"#django.test.runner.DiscoverRunner.build_suite\" title=\"django.test.runner.DiscoverRunner.build_suite\"><code class=\"xref py py-meth docutils literal notranslate\">DiscoverRunner.build_suite()</code></a> for a list of\nsupported formats).</p>\n<p><code class=\"docutils literal notranslate\">extra_tests</code> is a list of extra <code class=\"docutils literal notranslate\">TestCase</code> instances to add to the\nsuite that is executed by the test runner. These extra tests are run\nin addition to those discovered in the modules listed in <code class=\"docutils literal notranslate\">test_labels</code>.</p>\n<p>This method should return the number of tests that failed.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.runner.DiscoverRunner.add_arguments\">\n<em class=\"property\"><span class=\"k\">classmethod</span> </em><span class=\"sig-prename descclassname\">DiscoverRunner.</span><span class=\"sig-name descname\">add_arguments</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">parser</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.runner.DiscoverRunner.add_arguments\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Override this class method to add custom arguments accepted by the\n<a class=\"reference internal\" href=\"/ja/1.11/ref/django-admin/#django-admin-test\"><code class=\"xref std std-djadmin docutils literal notranslate\">test</code></a> management command. See\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_argument\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\">argparse.ArgumentParser.add_argument()</code></a> for details about adding\narguments to a parser.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.runner.DiscoverRunner.setup_test_environment\">\n<span class=\"sig-prename descclassname\">DiscoverRunner.</span><span class=\"sig-name descname\">setup_test_environment</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">**</span>kwargs</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.runner.DiscoverRunner.setup_test_environment\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Sets up the test environment by calling\n<a class=\"reference internal\" href=\"#django.test.utils.setup_test_environment\" title=\"django.test.utils.setup_test_environment\"><code class=\"xref py py-func docutils literal notranslate\">setup_test_environment()</code></a> and setting\n<a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\">DEBUG</code></a> to <code class=\"docutils literal notranslate\">self.debug_mode</code> (defaults to <code class=\"docutils literal notranslate\">False</code>).</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.runner.DiscoverRunner.build_suite\">\n<span class=\"sig-prename descclassname\">DiscoverRunner.</span><span class=\"sig-name descname\">build_suite</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">test_labels</em>, <em class=\"sig-param\">extra_tests<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>kwargs</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.runner.DiscoverRunner.build_suite\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Constructs a test suite that matches the test labels provided.</p>\n<p><code class=\"docutils literal notranslate\">test_labels</code> is a list of strings describing the tests to be run. A test\nlabel can take one of four forms:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">path.to.test_module.TestCase.test_method</code> -- Run a single test method\nin a test case.</p></li>\n<li><p><code class=\"docutils literal notranslate\">path.to.test_module.TestCase</code> -- Run all the test methods in a test\ncase.</p></li>\n<li><p><code class=\"docutils literal notranslate\">path.to.module</code> -- Search for and run all tests in the named Python\npackage or module.</p></li>\n<li><p><code class=\"docutils literal notranslate\">path/to/directory</code> -- Search for and run all tests below the named\ndirectory.</p></li>\n</ul>\n<p>If <code class=\"docutils literal notranslate\">test_labels</code> has a value of <code class=\"docutils literal notranslate\">None</code>, the test runner will search for\ntests in all files below the current directory whose names match its\n<code class=\"docutils literal notranslate\">pattern</code> (see above).</p>\n<p><code class=\"docutils literal notranslate\">extra_tests</code> is a list of extra <code class=\"docutils literal notranslate\">TestCase</code> instances to add to the\nsuite that is executed by the test runner. These extra tests are run\nin addition to those discovered in the modules listed in <code class=\"docutils literal notranslate\">test_labels</code>.</p>\n<p>Returns a <code class=\"docutils literal notranslate\">TestSuite</code> instance ready to be run.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.runner.DiscoverRunner.setup_databases\">\n<span class=\"sig-prename descclassname\">DiscoverRunner.</span><span class=\"sig-name descname\">setup_databases</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">**</span>kwargs</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.runner.DiscoverRunner.setup_databases\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Creates the test databases by calling\n<a class=\"reference internal\" href=\"#django.test.utils.setup_databases\" title=\"django.test.utils.setup_databases\"><code class=\"xref py py-func docutils literal notranslate\">setup_databases()</code></a>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.runner.DiscoverRunner.run_checks\">\n<span class=\"sig-prename descclassname\">DiscoverRunner.</span><span class=\"sig-name descname\">run_checks</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.runner.DiscoverRunner.run_checks\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><aside class=\"version-note version-added\" data-version=\"1.11\" role=\"note\">\n<p class=\"version-note-title\">New in Django 1.11</p></aside>\n<p>Runs the <a class=\"reference internal\" href=\"/ja/1.11/topics/checks/\"><span class=\"doc\">system checks</span></a>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.runner.DiscoverRunner.run_suite\">\n<span class=\"sig-prename descclassname\">DiscoverRunner.</span><span class=\"sig-name descname\">run_suite</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">suite</em>, <em class=\"sig-param\"><span class=\"o\">**</span>kwargs</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.runner.DiscoverRunner.run_suite\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Runs the test suite.</p>\n<p>Returns the result produced by the running the test suite.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.runner.DiscoverRunner.get_test_runner_kwargs\">\n<span class=\"sig-prename descclassname\">DiscoverRunner.</span><span class=\"sig-name descname\">get_test_runner_kwargs</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.runner.DiscoverRunner.get_test_runner_kwargs\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><aside class=\"version-note version-added\" data-version=\"1.11\" role=\"note\">\n<p class=\"version-note-title\">New in Django 1.11</p></aside>\n<p>Returns the keyword arguments to instantiate the\n<code class=\"docutils literal notranslate\">DiscoverRunner.test_runner</code> with.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.runner.DiscoverRunner.teardown_databases\">\n<span class=\"sig-prename descclassname\">DiscoverRunner.</span><span class=\"sig-name descname\">teardown_databases</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">old_config</em>, <em class=\"sig-param\"><span class=\"o\">**</span>kwargs</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.runner.DiscoverRunner.teardown_databases\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Destroys the test databases, restoring pre-test conditions by calling\n<a class=\"reference internal\" href=\"#django.test.utils.teardown_databases\" title=\"django.test.utils.teardown_databases\"><code class=\"xref py py-func docutils literal notranslate\">teardown_databases()</code></a>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.runner.DiscoverRunner.teardown_test_environment\">\n<span class=\"sig-prename descclassname\">DiscoverRunner.</span><span class=\"sig-name descname\">teardown_test_environment</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">**</span>kwargs</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.runner.DiscoverRunner.teardown_test_environment\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Restores the pre-test environment.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.runner.DiscoverRunner.suite_result\">\n<span class=\"sig-prename descclassname\">DiscoverRunner.</span><span class=\"sig-name descname\">suite_result</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">suite</em>, <em class=\"sig-param\">result</em>, <em class=\"sig-param\"><span class=\"o\">**</span>kwargs</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.runner.DiscoverRunner.suite_result\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Computes and returns a return code based on a test suite, and the result\nfrom that test suite.</p>\n</dd></dl>\n\n</section>\n</section>\n<section id=\"testing-utilities\">\n<h3>Testing utilities<a class=\"heading-anchor\" href=\"#testing-utilities\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<section id=\"module-django.test.utils\">\n<span id=\"django-test-utils\"></span><h4><code class=\"docutils literal notranslate\">django.test.utils</code><a class=\"heading-anchor\" href=\"#module-django.test.utils\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>To assist in the creation of your own test runner, Django provides a number of\nutility methods in the <code class=\"docutils literal notranslate\">django.test.utils</code> module.</p>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.test.utils.setup_test_environment\">\n<span class=\"sig-name descname\">setup_test_environment</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">debug<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.utils.setup_test_environment\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Performs global pre-test setup, such as installing instrumentation for the\ntemplate rendering system and setting up the dummy email outbox.</p>\n<p>If <code class=\"docutils literal notranslate\">debug</code> isn't <code class=\"docutils literal notranslate\">None</code>, the <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\">DEBUG</code></a> setting is updated to its\nvalue.</p>\n<aside class=\"version-note version-changed\" data-version=\"1.11\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 1.11</p><p>The <code class=\"docutils literal notranslate\">debug</code> argument was added.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.test.utils.teardown_test_environment\">\n<span class=\"sig-name descname\">teardown_test_environment</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.utils.teardown_test_environment\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Performs global post-test teardown, such as removing instrumentation from\nthe template system and restoring normal email services.</p>\n</dd></dl>\n\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.test.utils.setup_databases\">\n<span class=\"sig-name descname\">setup_databases</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">verbosity</em>, <em class=\"sig-param\">interactive</em>, <em class=\"sig-param\">keepdb<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">debug_sql<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">parallel<span class=\"o\">=</span><span class=\"default_value\">0</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>kwargs</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.utils.setup_databases\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><aside class=\"version-note version-added\" data-version=\"1.11\" role=\"note\">\n<p class=\"version-note-title\">New in Django 1.11</p></aside>\n<p>Creates the test databases.</p>\n<p>Returns a data structure that provides enough detail to undo the changes\nthat have been made. This data will be provided to the\n<a class=\"reference internal\" href=\"#django.test.utils.teardown_databases\" title=\"django.test.utils.teardown_databases\"><code class=\"xref py py-func docutils literal notranslate\">teardown_databases()</code></a> function at the conclusion of testing.</p>\n</dd></dl>\n\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.test.utils.teardown_databases\">\n<span class=\"sig-name descname\">teardown_databases</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">old_config</em>, <em class=\"sig-param\">parallel<span class=\"o\">=</span><span class=\"default_value\">0</span></em>, <em class=\"sig-param\">keepdb<span class=\"o\">=</span><span class=\"default_value\">False</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.utils.teardown_databases\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><aside class=\"version-note version-added\" data-version=\"1.11\" role=\"note\">\n<p class=\"version-note-title\">New in Django 1.11</p></aside>\n<p>Destroys the test databases, restoring pre-test conditions.</p>\n<p><code class=\"docutils literal notranslate\">old_config</code> is a data structure defining the changes in the database\nconfiguration that need to be reversed. It's the return value of the\n<a class=\"reference internal\" href=\"#django.test.utils.setup_databases\" title=\"django.test.utils.setup_databases\"><code class=\"xref py py-meth docutils literal notranslate\">setup_databases()</code></a> method.</p>\n</dd></dl>\n\n</section>\n<section id=\"django-db-connection-creation\">\n<h4><code class=\"docutils literal notranslate\">django.db.connection.creation</code><a class=\"heading-anchor\" href=\"#django-db-connection-creation\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>The creation module of the database backend also provides some utilities that\ncan be useful during testing.</p>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.db.connection.creation.create_test_db\">\n<span class=\"sig-name descname\">create_test_db</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">verbosity<span class=\"o\">=</span><span class=\"default_value\">1</span></em>, <em class=\"sig-param\">autoclobber<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">serialize<span class=\"o\">=</span><span class=\"default_value\">True</span></em>, <em class=\"sig-param\">keepdb<span class=\"o\">=</span><span class=\"default_value\">False</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.connection.creation.create_test_db\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Creates a new test database and runs <code class=\"docutils literal notranslate\">migrate</code> against it.</p>\n<p><code class=\"docutils literal notranslate\">verbosity</code> has the same behavior as in <code class=\"docutils literal notranslate\">run_tests()</code>.</p>\n<p><code class=\"docutils literal notranslate\">autoclobber</code> describes the behavior that will occur if a\ndatabase with the same name as the test database is discovered:</p>\n<ul class=\"simple\">\n<li><p>If <code class=\"docutils literal notranslate\">autoclobber</code> is <code class=\"docutils literal notranslate\">False</code>, the user will be asked to\napprove destroying the existing database. <code class=\"docutils literal notranslate\">sys.exit</code> is\ncalled if the user does not approve.</p></li>\n<li><p>If autoclobber is <code class=\"docutils literal notranslate\">True</code>, the database will be destroyed\nwithout consulting the user.</p></li>\n</ul>\n<p><code class=\"docutils literal notranslate\">serialize</code> determines if Django serializes the database into an\nin-memory JSON string before running tests (used to restore the database\nstate between tests if you don't have transactions). You can set this to\n<code class=\"docutils literal notranslate\">False</code> to speed up creation time if you don't have any test classes\nwith <a class=\"reference internal\" href=\"/ja/1.11/topics/testing/overview/#test-case-serialized-rollback\"><span class=\"std std-ref\">serialized_rollback=True</span></a>.</p>\n<p>If you are using the default test runner, you can control this with the\nthe <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-TEST_SERIALIZE\"><code class=\"xref std std-setting docutils literal notranslate\">SERIALIZE</code></a> entry in the <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-DATABASE-TEST\"><code class=\"xref std std-setting docutils literal notranslate\">TEST</code></a> dictionary.</p>\n<p><code class=\"docutils literal notranslate\">keepdb</code> determines if the test run should use an existing\ndatabase, or create a new one. If <code class=\"docutils literal notranslate\">True</code>, the existing\ndatabase will be used, or created if not present. If <code class=\"docutils literal notranslate\">False</code>,\na new database will be created, prompting the user to remove\nthe existing one, if present.</p>\n<p>Returns the name of the test database that it created.</p>\n<p><code class=\"docutils literal notranslate\">create_test_db()</code> has the side effect of modifying the value of\n<a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-NAME\"><code class=\"xref std std-setting docutils literal notranslate\">NAME</code></a> in <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\">DATABASES</code></a> to match the name of the test\ndatabase.</p>\n</dd></dl>\n\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.db.connection.creation.destroy_test_db\">\n<span class=\"sig-name descname\">destroy_test_db</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">old_database_name</em>, <em class=\"sig-param\">verbosity<span class=\"o\">=</span><span class=\"default_value\">1</span></em>, <em class=\"sig-param\">keepdb<span class=\"o\">=</span><span class=\"default_value\">False</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.connection.creation.destroy_test_db\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Destroys the database whose name is the value of <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-NAME\"><code class=\"xref std std-setting docutils literal notranslate\">NAME</code></a> in\n<a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\">DATABASES</code></a>, and sets <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-NAME\"><code class=\"xref std std-setting docutils literal notranslate\">NAME</code></a> to the value of\n<code class=\"docutils literal notranslate\">old_database_name</code>.</p>\n<p>The <code class=\"docutils literal notranslate\">verbosity</code> argument has the same behavior as for\n<a class=\"reference internal\" href=\"#django.test.runner.DiscoverRunner\" title=\"django.test.runner.DiscoverRunner\"><code class=\"xref py py-class docutils literal notranslate\">DiscoverRunner</code></a>.</p>\n<p>If the <code class=\"docutils literal notranslate\">keepdb</code> argument is <code class=\"docutils literal notranslate\">True</code>, then the connection to the\ndatabase will be closed, but the database will not be destroyed.</p>\n</dd></dl>\n\n</section>\n</section>\n</section>\n<section id=\"integration-with-coverage-py\">\n<span id=\"topics-testing-code-coverage\"></span><h2>Integration with <code class=\"docutils literal notranslate\">coverage.py</code><a class=\"heading-anchor\" href=\"#integration-with-coverage-py\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Code coverage describes how much source code has been tested. It shows which\nparts of your code are being exercised by tests and which are not. It's an\nimportant part of testing applications, so it's strongly recommended to check\nthe coverage of your tests.</p>\n<p>Django can be easily integrated with <a class=\"reference external\" href=\"http://coverage.readthedocs.io/\">coverage.py</a>, a tool for measuring code\ncoverage of Python programs. First, <a class=\"reference external\" href=\"https://pypi.python.org/pypi/coverage\">install coverage.py</a>. Next, run the\nfollowing from your project folder containing <code class=\"docutils literal notranslate\">manage.py</code>:</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>coverage run <span class=\"o\">--</span>source<span class=\"o\">=</span><span class=\"s1\">&#39;.&#39;</span> manage<span class=\"o\">.</span>py test myapp\n</code></pre></div>\n<p>This runs your tests and collects coverage data of the executed files in your\nproject. You can see a report of this data by typing following command:</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>coverage report\n</code></pre></div>\n<p>Note that some Django code was executed while running tests, but it is not\nlisted here because of the <code class=\"docutils literal notranslate\">source</code> flag passed to the previous command.</p>\n<p>For more options like annotated HTML listings detailing missed lines, see the\n<a class=\"reference external\" href=\"http://coverage.readthedocs.io/\">coverage.py</a> docs.</p>\n</section>","rootId":"advanced-testing-topics","toc":[{"title":"The request factory","anchor":"the-request-factory","children":[{"title":"カスタマイズ例","anchor":"example","children":[]}]},{"title":"Tests and multiple host names","anchor":"tests-and-multiple-host-names","children":[]},{"title":"Tests and multiple databases","anchor":"tests-and-multiple-databases","children":[{"title":"Testing primary/replica configurations","anchor":"testing-primary-replica-configurations","children":[]},{"title":"Controlling creation order for test databases","anchor":"controlling-creation-order-for-test-databases","children":[]}]},{"title":"Advanced features of TransactionTestCase","anchor":"advanced-features-of-transactiontestcase","children":[]},{"title":"Using the Django test runner to test reusable applications","anchor":"using-the-django-test-runner-to-test-reusable-applications","children":[]},{"title":"Using different testing frameworks","anchor":"using-different-testing-frameworks","children":[{"title":"Defining a test runner","anchor":"defining-a-test-runner","children":[{"title":"属性","anchor":"attributes","children":[]},{"title":"メソッド","anchor":"methods","children":[]}]},{"title":"Testing utilities","anchor":"testing-utilities","children":[{"title":"django.test.utils","anchor":"module-django.test.utils","children":[]},{"title":"django.db.connection.creation","anchor":"django-db-connection-creation","children":[]}]}]},{"title":"Integration with coverage.py","anchor":"integration-with-coverage-py","children":[]}],"breadcrumbs":[{"docname":"topics/index","title":"Django を使う","url":"/ja/1.11/topics/"},{"docname":"topics/testing/index","title":"Django におけるテスト","url":"/ja/1.11/topics/testing/"}],"prev":{"docname":"topics/testing/tools","title":"テストツール","url":"/ja/1.11/topics/testing/tools/"},"next":{"docname":"topics/auth/index","title":"Django でのユーザー認証","url":"/ja/1.11/topics/auth/"},"formats":{"html":"/ja/1.11/topics/testing/advanced/","markdown":"/ja/1.11/topics/testing/advanced.md","json":"/ja/1.11/topics/testing/advanced.json"},"source":"https://github.com/django/django/blob/stable/1.11.x/docs/topics/testing/advanced.txt","official":"https://docs.djangoproject.com/ja/1.11/topics/testing/advanced/","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","1.11","1.10","1.9"],"inLocales":["en","fr","ja","id","pt-br","ko","es","el","pl"]}