{"title":"テストを書いて実行する","version":"1.11","locale":"ja","docname":"topics/testing/overview","url":"/ja/1.11/topics/testing/overview/","canonical":"https://djangodocs.dev/ja/1.11/topics/testing/overview/","summary":"参考 テストチュートリアル 、 テストツールリファレンス 、 テストに関する応用的なトピック も読んでください。 ドキュメントは2つの大きなセクションに分けられます。前半のパートでは、Django でのテストの書き方を説明します。後半では、テストの実行の仕方について説明します。 Writing tests Link to…","html":"<span id=\"writing-and-running-tests\"></span><h1>テストを書いて実行する<a class=\"heading-anchor\" href=\"#module-django.test\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<aside class=\"admonition admonition-seealso\">\n<p class=\"admonition-title\">参考</p>\n<p><a class=\"reference internal\" href=\"/ja/1.11/intro/tutorial05/\"><span class=\"doc\">テストチュートリアル</span></a>、 <a class=\"reference internal\" href=\"/ja/1.11/topics/testing/tools/\"><span class=\"doc\">テストツールリファレンス</span></a>、 <a class=\"reference internal\" href=\"/ja/1.11/topics/testing/advanced/\"><span class=\"doc\">テストに関する応用的なトピック</span></a> も読んでください。</p>\n</aside>\n<p>ドキュメントは2つの大きなセクションに分けられます。前半のパートでは、Django でのテストの書き方を説明します。後半では、テストの実行の仕方について説明します。</p>\n<section id=\"writing-tests\">\n<h2>Writing tests<a class=\"heading-anchor\" href=\"#writing-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django のユニットテストには、Python スタンダードライブラリのモジュール、<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\"><span class=\"pre\">unittest</span></code></a> を使用します。このモジュールは、テストをクラスベースのアプローチで定義します。</p>\n<p>次の例では、 <a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.TestCase\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">unittest.TestCase</span></code></a> のサブクラスである <a class=\"reference internal\" href=\"/ja/1.11/topics/testing/tools/#django.test.TestCase\" title=\"django.test.TestCase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.test.TestCase</span></code></a> から、テスト用の新しいサブクラスを作っています。各テストをトランザクションの内側で実行することで、独立性を実現しています。</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=\"w\"> </span><span class=\"nn\">django.test</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">TestCase</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">myapp.models</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Animal</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">AnimalTestCase</span><span class=\"p\">(</span><span class=\"n\">TestCase</span><span class=\"p\">):</span>\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">setUp</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"n\">Animal</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">create</span><span class=\"p\">(</span><span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s2\">&quot;lion&quot;</span><span class=\"p\">,</span> <span class=\"n\">sound</span><span class=\"o\">=</span><span class=\"s2\">&quot;roar&quot;</span><span class=\"p\">)</span>\n        <span class=\"n\">Animal</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">create</span><span class=\"p\">(</span><span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s2\">&quot;cat&quot;</span><span class=\"p\">,</span> <span class=\"n\">sound</span><span class=\"o\">=</span><span class=\"s2\">&quot;meow&quot;</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">test_animals_can_speak</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;Animals that can speak are correctly identified&quot;&quot;&quot;</span>\n        <span class=\"n\">lion</span> <span class=\"o\">=</span> <span class=\"n\">Animal</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s2\">&quot;lion&quot;</span><span class=\"p\">)</span>\n        <span class=\"n\">cat</span> <span class=\"o\">=</span> <span class=\"n\">Animal</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s2\">&quot;cat&quot;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertEqual</span><span class=\"p\">(</span><span class=\"n\">lion</span><span class=\"o\">.</span><span class=\"n\">speak</span><span class=\"p\">(),</span> <span class=\"s1\">&#39;The lion says &quot;roar&quot;&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertEqual</span><span class=\"p\">(</span><span class=\"n\">cat</span><span class=\"o\">.</span><span class=\"n\">speak</span><span class=\"p\">(),</span> <span class=\"s1\">&#39;The cat says &quot;meow&quot;&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p><span class=\"xref std std-ref\">自分で描いたテストを実行する &lt;running-tests&gt;`とき、テストユーティリティのデフォルトの動作は次のようなものです。まず、``test`</span> で始まる名前を持つファイルからすべてのテストケース (つまり <a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.TestCase\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">unittest.TestCase</span></code></a> のすべてのサブクラス) を見つけ出します。次に、それらテストケースのテストスイートを自動的にビルドします。そして、ビルドしたテストスイートを実行します。</p>\n<p><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\"><span class=\"pre\">unittest</span></code></a> の詳細については、Python のドキュメントを読んでください。</p>\n<aside class=\"admonition-where-should-the-tests-live admonition\">\n<p class=\"admonition-title\">どこにテストを書くべき？</p>\n<p>デフォルトの <a class=\"reference internal\" href=\"/ja/1.11/ref/django-admin/#django-admin-startapp\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">startapp</span></code></a> テンプレートは、新しいアプリケーション内に <code class=\"docutils literal notranslate\"><span class=\"pre\">tests.py</span></code> ファイルを作成します。テストの数が少ないうちは、ここに書くのがいいかもしれません。しかし、テストスイートが大きくなってきたら、テストを複数のパッケージに再構成して、<code class=\"docutils literal notranslate\"><span class=\"pre\">test_models.py</span></code> 、 <code class=\"docutils literal notranslate\"><span class=\"pre\">test_views.py</span></code> 、 <code class=\"docutils literal notranslate\"><span class=\"pre\">test_forms.py</span></code> などの異なるサブモジュールに分離すると良いでしょう。ファイル名には、ちゃんと組織的な命名規則になっていれば、自由に好きな名前をつけて構いません。</p>\n<p><a class=\"reference internal\" href=\"/ja/1.11/topics/testing/advanced/#testing-reusable-applications\"><span class=\"std std-ref\">Using the Django test runner to test reusable applications</span></a> も参照してください。</p>\n</aside>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p>作成したテストが、データの新規作成やモデルのクエリなどのデータベースアクセスを必要とするときは、<a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.TestCase\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">unittest.TestCase</span></code></a> ではなく、 <a class=\"reference internal\" href=\"/ja/1.11/topics/testing/tools/#django.test.TestCase\" title=\"django.test.TestCase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.test.TestCase</span></code></a> のサブクラスを作るようにしてください。</p>\n<p><a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.TestCase\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">unittest.TestCase</span></code></a> を使えば、各テストでデータベースのトランザクションとフラッシュに必要な実行コストを避けることができます。しかし、データベースと相互作用するテストの場合、テストランナーがテストを実行する順番によっては、異なる動作をすることがあります。そのため、孤立した環境では成功するテストユニットでも、一連のテストスイートの中で実行した時には失敗してしまうという状況が発生することがあります。</p>\n</aside>\n</section>\n<section id=\"running-tests\">\n<span id=\"id1\"></span><h2>テストの実行<a class=\"heading-anchor\" href=\"#running-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>テストが書けたら、プロジェクトの <code class=\"docutils literal notranslate\"><span class=\"pre\">manage.py</span></code> ユーティリティの <a class=\"reference internal\" href=\"/ja/1.11/ref/django-admin/#django-admin-test\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">test</span></code></a> コマンドでテストが実行できます。</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>$ ./manage.py test\n</code></pre></div>\n<p>テストの探索方法は、unittest モジュールの <a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest-test-discovery\" title=\"(in Python v3.14)\"><span class=\"xref std std-ref\">built-in test discovery</span></a> にもとづきます。デフォルトでは、カレントディレクトリにある &quot;test*.py&quot; という名前の全てのファイルからテストを探し出します。</p>\n<p>You can specify particular tests to run by supplying any number of &quot;test labels&quot; to <code class=\"docutils literal notranslate\"><span class=\"pre\">./manage.py</span> <span class=\"pre\">test</span></code> に好きな数の「テストラベル」を与えることで、特定のテストを指定することもできます。各テストラベルには、パッケージ、モジュール、 <code class=\"docutils literal notranslate\"><span class=\"pre\">TestCase</span></code> サブクラス、テストメソッドへのドット区切りの Python パスを指定します。次に例を示します。</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># Run all the tests in the animals.tests module\n$ ./manage.py test animals.tests\n\n# Run all the tests found within the &#39;animals&#39; package\n$ ./manage.py test animals\n\n# Run just one test case\n$ ./manage.py test animals.tests.AnimalTestCase\n\n# Run just one test method\n$ ./manage.py test animals.tests.AnimalTestCase.test_animals_can_speak\n</code></pre></div>\n<p>ディレクトリ下に置かれたテストを探索するために、ディレクトリのパスを指定することもできます。</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>$ ./manage.py test animals/\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">-p</span></code> (または <code class=\"docutils literal notranslate\"><span class=\"pre\">--pattern</span></code>) オプションを使って、カスタムのファイル名のパターンマッチを指定すれば、テストファイルの名前が <code class=\"docutils literal notranslate\"><span class=\"pre\">test*.py</span></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>$ ./manage.py test --pattern=&quot;tests_*.py&quot;\n</code></pre></div>\n<p>テストの実行中に <code class=\"docutils literal notranslate\"><span class=\"pre\">Ctrl-C</span></code> を押すと、テストランナーは現在実行中のテストが完了するのを待って、gracefully にテストを終了します。graceful な終了では、テストランナーは失敗したテストの詳細を出力し、実行したテストの数と、エラーおよび失敗したテストの数をレポートし、通常通りにテストデータベースを破棄します。そのため、<code class=\"docutils literal notranslate\"><span class=\"pre\">Ctrl-C</span></code> を押すのは、たとえば、<code class=\"xref std std-option docutils literal notranslate\"><span class=\"pre\">--failfast</span></code> オプションを付けるのを忘れて、思わぬテストが失敗したとき、すべてのテストが終わるのを待たずにその失敗の詳細をすぐに知りたいような場合に大変役に立ちます。</p>\n<p>現在実行中のテストの終了も待ちたくないときは、もう一度 <code class=\"docutils literal notranslate\"><span class=\"pre\">Ctrl-C</span></code> を押すことで、テストを graceful ではなく、すぐに強制終了することができます。その場合、強制終了前に実行していたテストの詳細はリポートされず、実行中に作られたテストデータベースも破棄されません。</p>\n<aside class=\"admonition-test-with-warnings-enabled admonition\">\n<p class=\"admonition-title\">警告を有効にしてテストする</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">python</span> <span class=\"pre\">-Wall</span> <span class=\"pre\">manage.py</span> <span class=\"pre\">test</span></code> というように、Python の警告表示を有効にしてテストを実行するのは良い考えです。<code class=\"docutils literal notranslate\"><span class=\"pre\">-Wall</span></code> フラグをセットすると、Python に deprecation 警告を表示するように伝えます。Django などの Python ライブラリは、機能の廃止を知らせるフラグとして、この警告を利用しています。また、この機能は、厳密には間違いではないがあまり良くないコードを知らせてくれることがあるので、実装を改善できることがあります。</p>\n</aside>\n<section id=\"the-test-database\">\n<span id=\"id2\"></span><h3>test データベース<a class=\"heading-anchor\" href=\"#the-test-database\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>データベースを必要とするテスト (すなわち、モデルテスト) には、&quot;実際の&quot; (production) 環境のデータベースは使用しません。代わりに、テスト用の空のデータベースを用意します。</p>\n<p>テストが成功したかどうかにかかわらず、すべてのテストの実行が終わった時点で、テストデータベースは破棄されます。</p>\n<p><a class=\"reference internal\" href=\"/ja/1.11/ref/django-admin/#cmdoption-test-keepdb\"><code class=\"xref std std-option docutils literal notranslate\"><span class=\"pre\">test</span> <span class=\"pre\">--keepdb</span></code></a> オプションを指定すれば、テストデータベースの破棄を防ぐことができます。これにより、複数回テストを実行しても、テストデータベースを保存することができます。データベースが存在しないときは、最初に新しく作成され、そしてデータベースが最新の状態になるように、マイグレーションが順番に実行されます。</p>\n<p>テストデータベースのデフォルトの名前は、 <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DATABASES</span></code></a> 設定内の各 <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-NAME\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">NAME</span></code></a> の値の前に <code class=\"docutils literal notranslate\"><span class=\"pre\">test_</span></code> を付けたものになります。SQLite を使っているときは、デフォルトでは、テストにはインメモリのデータベースを使います (つまり、データベースはメモリ内に作成されるため、ファイルシステムへのアクセスを完全になくすことができるのです！)。設定の <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DATABASES</span></code></a> 内の <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-DATABASE-TEST\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TEST</span></code></a> ディクショナリには、テストデータベースに対するいろいろな設定を書くことができます。例えば、別のデータベース名を指定したければ、 <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-DATABASE-TEST\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TEST</span></code></a> ディクショナリの <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-TEST_NAME\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">NAME</span></code></a> に、 <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DATABASES</span></code></a> の中から好きなデータベースを選んで指定することができます。</p>\n<p>PostgreSQL では、 <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-USER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USER</span></code></a> が、ビルトインの <code class=\"docutils literal notranslate\"><span class=\"pre\">postgres</span></code> データベースへの読み取りアクセス権も持っている必要があります。</p>\n<p>Aside from using a separate database, the test runner will otherwise\nuse all of the same database settings you have in your settings file:\n<a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-DATABASE-ENGINE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">ENGINE</span></code></a>, <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-USER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USER</span></code></a>, <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-HOST\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">HOST</span></code></a>, etc. The\ntest database is created by the user specified by <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-USER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USER</span></code></a>, so you'll\nneed to make sure that the given user account has sufficient privileges to\ncreate a new database on the system.</p>\n<p>For fine-grained control over the character encoding of your test\ndatabase, use the <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-TEST_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CHARSET</span></code></a> TEST option. If you're using\nMySQL, you can also use the <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-TEST_COLLATION\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">COLLATION</span></code></a> option to\ncontrol the particular collation used by the test database. See the\n<a class=\"reference internal\" href=\"/ja/1.11/ref/settings/\"><span class=\"doc\">settings documentation</span></a> for details of these\nand other advanced settings.</p>\n<p>If using an SQLite in-memory database with Python 3.4+ and SQLite 3.7.13+,\n<a class=\"reference external\" href=\"https://www.sqlite.org/sharedcache.html\">shared cache</a> will be enabled, so\nyou can write tests with ability to share the database between threads.</p>\n<aside class=\"admonition-finding-data-from-your-production-database-when-running-tests admonition\">\n<p class=\"admonition-title\">Finding data from your production database when running tests?</p>\n<p>If your code attempts to access the database when its modules are compiled,\nthis will occur <em>before</em> the test database is set up, with potentially\nunexpected results. For example, if you have a database query in\nmodule-level code and a real database exists, production data could pollute\nyour tests. <em>It is a bad idea to have such import-time database queries in\nyour code</em> anyway - rewrite your code so that it doesn't do this.</p>\n<p>This also applies to customized implementations of\n<a class=\"reference internal\" href=\"/ja/1.11/ref/applications/#django.apps.AppConfig.ready\" title=\"django.apps.AppConfig.ready\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">ready()</span></code></a>.</p>\n</aside>\n<aside class=\"admonition admonition-seealso\">\n<p class=\"admonition-title\">参考</p>\n<p>The <a class=\"reference internal\" href=\"/ja/1.11/topics/testing/advanced/#topics-testing-advanced-multidb\"><span class=\"std std-ref\">advanced multi-db testing topics</span></a>.</p>\n</aside>\n</section>\n<section id=\"order-in-which-tests-are-executed\">\n<span id=\"order-of-tests\"></span><h3>テストの実行順序<a class=\"heading-anchor\" href=\"#order-in-which-tests-are-executed\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>すべての <code class=\"docutils literal notranslate\"><span class=\"pre\">TestCase</span></code> コードがクリーンなデータベースで実行されることを保証するために、Django のテストランナーは次の方法でテストの実行順序を決定します。</p>\n<ul class=\"simple\">\n<li><p>すべての <a class=\"reference internal\" href=\"/ja/1.11/topics/testing/tools/#django.test.TestCase\" title=\"django.test.TestCase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">TestCase</span></code></a> サブクラスが最初に実行されます。</p></li>\n<li><p>つぎに、すべての Django ベースのテスト (<a class=\"reference internal\" href=\"/ja/1.11/topics/testing/tools/#django.test.TransactionTestCase\" title=\"django.test.TransactionTestCase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">TransactionTestCase</span></code></a> を含む <a class=\"reference internal\" href=\"/ja/1.11/topics/testing/tools/#django.test.SimpleTestCase\" title=\"django.test.SimpleTestCase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">SimpleTestCase</span></code></a> から作ったテストケース) を、実行順序が保証されず、また強制もされないような適当な順番で実行します。</p></li>\n<li><p>最後に、その他の <a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.TestCase\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">unittest.TestCase</span></code></a> テスト (doctests を含む) が実行されます。このテストの中には、データベースを変更し、そのまま元の状態に戻さないようなテストがあることもあります。</p></li>\n</ul>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>The new ordering of tests may reveal unexpected dependencies on test case\nordering. This is the case with doctests that relied on state left in the\ndatabase by a given <a class=\"reference internal\" href=\"/ja/1.11/topics/testing/tools/#django.test.TransactionTestCase\" title=\"django.test.TransactionTestCase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">TransactionTestCase</span></code></a> test, they\nmust be updated to be able to run independently.</p>\n</aside>\n<p>You may reverse the execution order inside groups using the <a class=\"reference internal\" href=\"/ja/1.11/ref/django-admin/#cmdoption-test-reverse\"><code class=\"xref std std-option docutils literal notranslate\"><span class=\"pre\">test</span>\n<span class=\"pre\">--reverse</span></code></a> option. This can help with ensuring your tests are independent from\neach other.</p>\n</section>\n<section id=\"rollback-emulation\">\n<span id=\"test-case-serialized-rollback\"></span><h3>ロールバックのエミュレーション<a class=\"heading-anchor\" href=\"#rollback-emulation\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Any initial data loaded in migrations will only be available in <code class=\"docutils literal notranslate\"><span class=\"pre\">TestCase</span></code>\ntests and not in <code class=\"docutils literal notranslate\"><span class=\"pre\">TransactionTestCase</span></code> tests, and additionally only on\nbackends where transactions are supported (the most important exception being\nMyISAM). This is also true for tests which rely on <code class=\"docutils literal notranslate\"><span class=\"pre\">TransactionTestCase</span></code>\nsuch as <a class=\"reference internal\" href=\"/ja/1.11/topics/testing/tools/#django.test.LiveServerTestCase\" title=\"django.test.LiveServerTestCase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">LiveServerTestCase</span></code></a> and\n<a class=\"reference internal\" href=\"/ja/1.11/ref/contrib/staticfiles/#django.contrib.staticfiles.testing.StaticLiveServerTestCase\" title=\"django.contrib.staticfiles.testing.StaticLiveServerTestCase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">StaticLiveServerTestCase</span></code></a>.</p>\n<p>Django can reload that data for you on a per-testcase basis by\nsetting the <code class=\"docutils literal notranslate\"><span class=\"pre\">serialized_rollback</span></code> option to <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> in the body of the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">TestCase</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">TransactionTestCase</span></code>, but note that this will slow down\nthat test suite by approximately 3x.</p>\n<p>Third-party apps or those developing against MyISAM will need to set this;\nin general, however, you should be developing your own projects against a\ntransactional database and be using <code class=\"docutils literal notranslate\"><span class=\"pre\">TestCase</span></code> for most tests, and thus\nnot need this setting.</p>\n<p>The initial serialization is usually very quick, but if you wish to exclude\nsome apps from this process (and speed up test runs slightly), you may add\nthose apps to <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-TEST_NON_SERIALIZED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TEST_NON_SERIALIZED_APPS</span></code></a>.</p>\n<p>To prevent serialized data from being loaded twice, setting\n<code class=\"docutils literal notranslate\"><span class=\"pre\">serialized_rollback=True</span></code> disables the\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\"><span class=\"pre\">post_migrate</span></code></a> signal when flushing the test\ndatabase.</p>\n</section>\n<section id=\"other-test-conditions\">\n<h3>その他のテストに関する条件<a class=\"heading-anchor\" href=\"#other-test-conditions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>設定ファイルで指定した <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEBUG</span></code></a> の値にかかわらず、Django のテストは <a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEBUG</span></code></a>=False を指定したものとして実行されます。これは、表示されるコードの出力が、実際の環境設定で見られるものと同じになるようにするためです。</p>\n<p>各テスト後にキャッシュはクリアされません。そのため、&quot;manage.py test fooapp&quot; を実行すると、テストで挿入されたデータが実働環境のシステムのキャッシュに残り続ける可能性があります。データベースの場合と違い、別の「テスト用のキャッシュ」が使われないためです。ただし、将来、この動作は <a class=\"reference external\" href=\"https://code.djangoproject.com/ticket/11505\">変更される可能性があります</a> 。</p>\n</section>\n<section id=\"understanding-the-test-output\">\n<h3>テストの出力を理解する<a class=\"heading-anchor\" href=\"#understanding-the-test-output\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>テストを実行すると、テストランナーが用意したたくさんのメッセージが表示されます。表示するメッセージの詳細レベルは、コマンドラインで <code class=\"docutils literal notranslate\"><span class=\"pre\">verbosity</span></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><span class=\"n\">Creating</span> <span class=\"n\">test</span> <span class=\"n\">database</span><span class=\"o\">...</span>\n<span class=\"n\">Creating</span> <span class=\"n\">table</span> <span class=\"n\">myapp_animal</span>\n<span class=\"n\">Creating</span> <span class=\"n\">table</span> <span class=\"n\">myapp_mineral</span>\n</code></pre></div>\n<p>このメッセージは、テストランナーが前のセクションで説明したテスト用のデータベースを作成していることを表しています。</p>\n<p>テスト用データベースが作成されると、Django はテストを実行します。すべてのテストが成功すれば、次のようなメッセージが表示されるはずです。</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=\"o\">----------------------------------------------------------------------</span>\n<span class=\"n\">Ran</span> <span class=\"mi\">22</span> <span class=\"n\">tests</span> <span class=\"ow\">in</span> <span class=\"mf\">0.221</span><span class=\"n\">s</span>\n\n<span class=\"n\">OK</span>\n</code></pre></div>\n<p>しかし、もしテストが失敗した場合には、どのテストが失敗したのかとその詳細が表示されます。</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=\"o\">======================================================================</span>\n<span class=\"n\">FAIL</span><span class=\"p\">:</span> <span class=\"n\">test_was_published_recently_with_future_poll</span> <span class=\"p\">(</span><span class=\"n\">polls</span><span class=\"o\">.</span><span class=\"n\">tests</span><span class=\"o\">.</span><span class=\"n\">PollMethodTests</span><span class=\"p\">)</span>\n<span class=\"o\">----------------------------------------------------------------------</span>\n<span class=\"n\">Traceback</span> <span class=\"p\">(</span><span class=\"n\">most</span> <span class=\"n\">recent</span> <span class=\"n\">call</span> <span class=\"n\">last</span><span class=\"p\">):</span>\n  <span class=\"n\">File</span> <span class=\"s2\">&quot;/dev/mysite/polls/tests.py&quot;</span><span class=\"p\">,</span> <span class=\"n\">line</span> <span class=\"mi\">16</span><span class=\"p\">,</span> <span class=\"ow\">in</span> <span class=\"n\">test_was_published_recently_with_future_poll</span>\n    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertIs</span><span class=\"p\">(</span><span class=\"n\">future_poll</span><span class=\"o\">.</span><span class=\"n\">was_published_recently</span><span class=\"p\">(),</span> <span class=\"kc\">False</span><span class=\"p\">)</span>\n<span class=\"ne\">AssertionError</span><span class=\"p\">:</span> <span class=\"kc\">True</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"kc\">False</span>\n\n<span class=\"o\">----------------------------------------------------------------------</span>\n<span class=\"n\">Ran</span> <span class=\"mi\">1</span> <span class=\"n\">test</span> <span class=\"ow\">in</span> <span class=\"mf\">0.003</span><span class=\"n\">s</span>\n\n<span class=\"n\">FAILED</span> <span class=\"p\">(</span><span class=\"n\">failures</span><span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>このエラー出力の詳しい説明は、このドキュメントの範囲外ですが、極めて直感的に理解できるものです。詳しく知りたければ、Python の <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\"><span class=\"pre\">unittest</span></code></a> ライブラリのドキュメントを読んでみてください。</p>\n<p>失敗したテストやおかしなテストが複数あったとしても、テストランナーのスクリプトから返ってくる終了コードは 1 であることに注意してください。すべてのテストが成功すれば、0 が返ります。この特徴は、別のシェルスクリプトの中でテストランナースクリプトを実行するときに、成功したかどうかの情報が必要な時に役に立ちます。</p>\n</section>\n<section id=\"speeding-up-the-tests\">\n<span id=\"speeding-up-tests-auth-hashers\"></span><h3>テストのスピードアップ<a class=\"heading-anchor\" href=\"#speeding-up-the-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<section id=\"running-tests-in-parallel\">\n<h4>テストの並列実行<a class=\"heading-anchor\" href=\"#running-tests-in-parallel\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>各テストが適切に独立性を保ったものであれば、マルチコアのハードウェア上でテストを並列実行することでスピートアップさせることができます。詳しくは <a class=\"reference internal\" href=\"/ja/1.11/ref/django-admin/#cmdoption-test-parallel\"><code class=\"xref std std-option docutils literal notranslate\"><span class=\"pre\">test</span> <span class=\"pre\">--parallel</span></code></a> を読んでください。</p>\n</section>\n<section id=\"password-hashing\">\n<h4>パスワードのハッシュ生成<a class=\"heading-anchor\" href=\"#password-hashing\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>デフォルトのパスワードのハッシュ生成器は、設計上、時間のかかるものになっています。テストの中で多数のユーザーを認証する必要がある場合、カスタムの設定ファイルを用意して、<a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-PASSWORD_HASHERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">PASSWORD_HASHERS</span></code></a> 設定に、より高速なハッシュ生成アルゴリズムを設定すると良いでしょう。</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\">PASSWORD_HASHERS</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"s1\">&#39;django.contrib.auth.hashers.MD5PasswordHasher&#39;</span><span class=\"p\">,</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p><a class=\"reference internal\" href=\"/ja/1.11/ref/settings/#std-setting-PASSWORD_HASHERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">PASSWORD_HASHERS</span></code></a> には、必要なハッシュアルゴリズムが複数あっても、追加しておくことを忘れないようにしてください。</p>\n</section>\n<section id=\"preserving-the-test-database\">\n<h4>Preserving the test database<a class=\"heading-anchor\" href=\"#preserving-the-test-database\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>The <a class=\"reference internal\" href=\"/ja/1.11/ref/django-admin/#cmdoption-test-keepdb\"><code class=\"xref std std-option docutils literal notranslate\"><span class=\"pre\">test</span> <span class=\"pre\">--keepdb</span></code></a> option preserves the test database between test\nruns. It skips the create and destroy actions which can greatly decrease the\ntime to run tests.</p>\n</section>\n</section>\n</section>","rootId":"module-django.test","toc":[{"title":"Writing tests","anchor":"writing-tests","children":[]},{"title":"テストの実行","anchor":"running-tests","children":[{"title":"test データベース","anchor":"the-test-database","children":[]},{"title":"テストの実行順序","anchor":"order-in-which-tests-are-executed","children":[]},{"title":"ロールバックのエミュレーション","anchor":"rollback-emulation","children":[]},{"title":"その他のテストに関する条件","anchor":"other-test-conditions","children":[]},{"title":"テストの出力を理解する","anchor":"understanding-the-test-output","children":[]},{"title":"テストのスピードアップ","anchor":"speeding-up-the-tests","children":[{"title":"テストの並列実行","anchor":"running-tests-in-parallel","children":[]},{"title":"パスワードのハッシュ生成","anchor":"password-hashing","children":[]},{"title":"Preserving the test database","anchor":"preserving-the-test-database","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/index","title":"Django におけるテスト","url":"/ja/1.11/topics/testing/"},"next":{"docname":"topics/testing/tools","title":"テストツール","url":"/ja/1.11/topics/testing/tools/"},"formats":{"html":"/ja/1.11/topics/testing/overview/","markdown":"/ja/1.11/topics/testing/overview.md","json":"/ja/1.11/topics/testing/overview.json"},"source":"https://github.com/django/django/blob/stable/1.11.x/docs/topics/testing/overview.txt","official":"https://docs.djangoproject.com/ja/1.11/topics/testing/overview/","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"]}