{"title":"テストツール","version":"4.2","locale":"ja","docname":"topics/testing/tools","url":"/ja/4.2/topics/testing/tools/","canonical":"https://djangodocs.dev/ja/4.2/topics/testing/tools/","summary":"Django は、テストを書くのに便利なツールをいくつか提供しています。テストクライアントは、ダミーのウェブブラウザとして振る舞う Python のクラスです。","html":"<h1>テストツール<a class=\"heading-anchor\" href=\"#testing-tools\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Django は、テストを書くのに便利なツールをいくつか提供しています。</p>\n<section id=\"the-test-client\">\n<span id=\"test-client\"></span><h2>テストクライアント<a class=\"heading-anchor\" href=\"#the-test-client\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>テストクライアントは、ダミーのウェブブラウザとして振る舞う Python のクラスです。これを使えば、ビューと Django で作ったアプリケーションとの関係をプログラムから自動でテストできるようになります。</p>\n<p>テストクライアントでは、次のようなことができます。</p>\n<ul class=\"simple\">\n<li><p>ある URL に対する GET および POST リクエストのシミュレートと、その結果の観察。低レベルの HTTP (レスポンスのヘッダーやステータスコードなど) から、ページのコンテンツまで、あらゆるレスポンスの内容を調査することができます。</p></li>\n<li><p>一連の (好きな数の) リダイレクトを見て、その各ステップごとに URL とステータスコードをチェックする。</p></li>\n<li><p>指定したリクエストが特定の Django テンプレートによってレンダリングされたとき、テンプレートのコンテキストが正しく特定の値を含んでいるかどうかをテストする。</p></li>\n</ul>\n<p>テストクライアントは <a class=\"reference external\" href=\"https://www.selenium.dev/\">Selenium</a> や他の &quot;ブラウザ内 (in-browser)&quot; フレームワークの代替を目指すものではないことに注意してください。Django のテストクライアントの目的は別の点にあります。つまり、</p>\n<ul class=\"simple\">\n<li><p>Django のテストクライアントは、正しいテンプレートがレンダリングされ、そのテンプレートが正しいコンテキストデータをちゃんと渡しているのかをチェックするために使います。</p></li>\n<li><p>ビューの機能を直接テストするには、<a class=\"reference internal\" href=\"/ja/4.2/topics/testing/advanced/#django.test.RequestFactory\" title=\"django.test.RequestFactory\"><code class=\"xref py py-class docutils literal notranslate\">RequestFactory</code></a> を使用して、ルーティングとミドルウェアのレイヤーをバイパスしてください。</p></li>\n<li><p><a class=\"reference external\" href=\"https://www.selenium.dev/\">Selenium</a> などのブラウザー内フレームワークは、HTML の*レンダリング*や、ウェブページの*振る舞い*、つまり JavaScript の機能性をテストするために使います。また、Django はこれらのフレームワーク向けの特別なサポートも提供しています。詳細については、<a class=\"reference internal\" href=\"#django.test.LiveServerTestCase\" title=\"django.test.LiveServerTestCase\"><code class=\"xref py py-class docutils literal notranslate\">LiveServerTestCase</code></a> のセクションを読んでください。</p></li>\n</ul>\n<p>包括的なテストスイートを実現するには、これらすべてのタイプのテストを組み合わせて行うべきです。</p>\n<section id=\"overview-and-a-quick-example\">\n<h3>概要と簡単な例<a class=\"heading-anchor\" href=\"#overview-and-a-quick-example\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>テストクライアントを使うには、<code class=\"docutils literal notranslate\">django.test.Client</code> からインスタンスを作り、次のようにウェブページを取得します。</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">django.test</span> <span class=\"kn\">import</span> Client\n<span class=\"gp\">&gt;&gt;&gt; </span>c <span class=\"o\">=</span> Client<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>response <span class=\"o\">=</span> c<span class=\"o\">.</span>post<span class=\"p\">(</span><span class=\"s2\">&quot;/login/&quot;</span><span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s2\">&quot;username&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;john&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;password&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;smith&quot;</span><span class=\"p\">})</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>response<span class=\"o\">.</span>status_code\n<span class=\"go\">200</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>response <span class=\"o\">=</span> c<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s2\">&quot;/customer/details/&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>response<span class=\"o\">.</span>content\n<span class=\"go\">b&#39;&lt;!DOCTYPE html...&#39;</span>\n</code></pre></div>\n<p>この例が示唆しているように、 <code class=\"docutils literal notranslate\">Client</code> のインスタンスは、Python のインタラクティブなインタプリタ上のセッションからでも作ることができます。</p>\n<p>テストクライアントの動作の仕方に関して、いくつか大切な注意点があります。</p>\n<ul>\n<li><p>テストクライアントが動作するために、ウェブサーバーが動作している必要は*ありません*。実際、ウェブサーバーを一切起動せずに動作するのです！ HTTP のオーバーヘッドを避けて、Django フレームワークと直接やりとりすることでこれを実現しています。これにより、高速なユニットテストが可能になっています。</p></li>\n<li><p>ページの取得時には、ドメイン全体ではなく、URL の <em>path</em> だけを指定することを覚えておいてください。たとえば、次は正しいです。</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s2\">&quot;/login/&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>しかし、これは間違いです。</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s2\">&quot;https://www.example.com/login/&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>テストクライアントには、自分の Django プロジェクト以外のウェブページを取得する機能はありません。他のウェブページが必要な場合には、 <a class=\"reference external\" href=\"https://docs.python.org/3/library/urllib.html#module-urllib\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\">urllib</code></a> などの Python のスタンダードライブラリモジュールを利用してください。</p>\n</li>\n<li><p>URL を解決するとき、テストクライアントは <a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-ROOT_URLCONF\"><code class=\"xref std std-setting docutils literal notranslate\">ROOT_URLCONF</code></a> 設定で指定されたすべての URLconf を使用します。</p></li>\n<li><p>上の例では Python のインタラクティブなインタプリタ上でも動作するはずですが、テストクライアントの一部の機能、特にテンプレート関係の機能は、 <em>テストの実行中</em> にしか使えないことがあります。</p>\n<p>というのも、Django のテストランナーは、与えられたビューによって読み込まれるテンプレートを決定する時に、ちょっとした黒魔術を使っています。この黒魔術 (具体的には Django のテンプレートシステムに対してメモリ上でパッチを当てています) は、テストの実行中にだけ使われるのです。</p>\n</li>\n<li><p>デフォルトでは、テストクライアントはサイト上でのすべての CSRF チェックを無効にしています。</p>\n<p>何らかの理由でテストクライアントに CSRF チェックを実行して*ほしい*ときには、CSRF チェックの実行を強制するテストクライアントのインスタンスを作ることができます。これには、クライアントを作る時に次のように <code class=\"docutils literal notranslate\">enforce_csrf_checks</code> 引数を渡します。</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">django.test</span> <span class=\"kn\">import</span> Client\n<span class=\"gp\">&gt;&gt;&gt; </span>csrf_client <span class=\"o\">=</span> Client<span class=\"p\">(</span>enforce_csrf_checks<span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">)</span>\n</code></pre></div>\n</li>\n</ul>\n</section>\n<section id=\"making-requests\">\n<h3>リクエストの作成<a class=\"heading-anchor\" href=\"#making-requests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>リクエストの作成には、<code class=\"docutils literal notranslate\">django.test.Client</code> クラスを使います。</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.test.Client\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">Client</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">enforce_csrf_checks<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">raise_request_exception<span class=\"o\">=</span><span class=\"default_value\">True</span></em>, <em class=\"sig-param\">json_encoder<span class=\"o\">=</span><span class=\"default_value\">DjangoJSONEncoder</span></em>, <em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\">*</abbr></span></em>, <em class=\"sig-param\">headers<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>defaults</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.Client\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A testing HTTP client. Takes several arguments that can customize behavior.</p>\n<p><code class=\"docutils literal notranslate\">headers</code> allows you to specify default headers that will be sent with\nevery request. For example, to set a <code class=\"docutils literal notranslate\">User-Agent</code> header:</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>client <span class=\"o\">=</span> Client<span class=\"p\">(</span>headers<span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s2\">&quot;user-agent&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;curl/7.79.1&quot;</span><span class=\"p\">})</span>\n</code></pre></div>\n<p>Arbitrary keyword arguments in <code class=\"docutils literal notranslate\">**defaults</code> set WSGI\n<span class=\"target\" id=\"index-0\"></span><a class=\"pep reference external\" href=\"https://peps.python.org/pep-3333/#environ-variables\"><strong>environ variables</strong></a>. For example, to set the\nscript name:</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>client <span class=\"o\">=</span> Client<span class=\"p\">(</span>SCRIPT_NAME<span class=\"o\">=</span><span class=\"s2\">&quot;/app/&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>Keyword arguments starting with a <code class=\"docutils literal notranslate\">HTTP_</code> prefix are set as headers,\nbut the <code class=\"docutils literal notranslate\">headers</code> parameter should be preferred for readability.</p>\n</aside>\n<p><a class=\"reference internal\" href=\"#django.test.Client.get\" title=\"django.test.Client.get\"><code class=\"xref py py-meth docutils literal notranslate\">get()</code></a> や <a class=\"reference internal\" href=\"#django.test.Client.post\" title=\"django.test.Client.post\"><code class=\"xref py py-meth docutils literal notranslate\">post()</code></a> などに <code class=\"docutils literal notranslate\">headers</code> と <code class=\"docutils literal notranslate\">extra</code> キーワード引数で与えられた値は、クラスコンストラクタに渡されたデフォルト値に優先します。</p>\n<p><code class=\"docutils literal notranslate\">enforce_csrf_checks</code> 引数を使うと、CSRF プロテクションのテストが実行できます (上の説明を参照)。</p>\n<p>The <code class=\"docutils literal notranslate\">raise_request_exception</code> argument allows controlling whether or not\nexceptions raised during the request should also be raised in the test.\nDefaults to <code class=\"docutils literal notranslate\">True</code>.</p>\n<p>The <code class=\"docutils literal notranslate\">json_encoder</code> argument allows setting a custom JSON encoder for\nthe JSON serialization that's described in <a class=\"reference internal\" href=\"#django.test.Client.post\" title=\"django.test.Client.post\"><code class=\"xref py py-meth docutils literal notranslate\">post()</code></a>.</p>\n<p><code class=\"docutils literal notranslate\">Client</code> インスタンスを一度作れば、以下のメソッドを自由に使うことができます。</p>\n<aside class=\"version-note version-changed\" data-version=\"4.2\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 4.2</p><p>The <code class=\"docutils literal notranslate\">headers</code> parameter was added.</p>\n</aside>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.Client.get\">\n<span class=\"sig-name descname\">get</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">path</em>, <em class=\"sig-param\">data<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">follow<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">secure<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\">*</abbr></span></em>, <em class=\"sig-param\">headers<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>extra</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.Client.get\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>与えられた <code class=\"docutils literal notranslate\">path</code> に対して GET リクエストを作り、<code class=\"docutils literal notranslate\">Response</code> オブジェクトを返します。<code class=\"docutils literal notranslate\">Response</code> オブジェクトについては、下のセクションにドキュメントされています。</p>\n<p><code class=\"docutils literal notranslate\">data</code> ディクショナリ内の key-value ペアは、GET の URL のデータ部分を構築するのに使われます。例えば、次の例では、</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>c <span class=\"o\">=</span> Client<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s2\">&quot;/customers/details/&quot;</span><span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s2\">&quot;name&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;fred&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;age&quot;</span><span class=\"p\">:</span> <span class=\"mi\">7</span><span class=\"p\">})</span>\n</code></pre></div>\n<p>引数の評価の結果、次の GET リクエストの実行と等価になります。</p>\n<div class=\"code-block\" data-language=\"text\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Text</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=\"Text code\"><code>/customers/details/?name=fred&amp;age=7\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\">headers</code> 引数は、リクエスト時に送信されるヘッダーの指定に使われます。たとえば、次のコード</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>c <span class=\"o\">=</span> Client<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"o\">.</span>get<span class=\"p\">(</span>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;/customers/details/&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"p\">{</span><span class=\"s2\">&quot;name&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;fred&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;age&quot;</span><span class=\"p\">:</span> <span class=\"mi\">7</span><span class=\"p\">},</span>\n<span class=\"gp\">... </span>    headers<span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s2\">&quot;accept&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;application/json&quot;</span><span class=\"p\">},</span>\n<span class=\"gp\">... </span><span class=\"p\">)</span>\n</code></pre></div>\n<p>は、HTTP ヘッダー <code class=\"docutils literal notranslate\">HTTP_ACCEPT</code> を details ビューに送信します。これは、 <a class=\"reference internal\" href=\"/ja/4.2/ref/request-response/#django.http.HttpRequest.accepts\" title=\"django.http.HttpRequest.accepts\"><code class=\"xref py py-meth docutils literal notranslate\">django.http.HttpRequest.accepts()</code></a> メソッドを使ったコードパースのテストのための良い方法です。</p>\n<p>Arbitrary keyword arguments set WSGI\n<span class=\"target\" id=\"index-1\"></span><a class=\"pep reference external\" href=\"https://peps.python.org/pep-3333/#environ-variables\"><strong>environ variables</strong></a>. For example, headers\nto set the script name:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>c <span class=\"o\">=</span> Client<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s2\">&quot;/&quot;</span><span class=\"p\">,</span> SCRIPT_NAME<span class=\"o\">=</span><span class=\"s2\">&quot;/app/&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>GET の引数がすでに URL エンコードされた形式である場合は、data 引数の代わりにエンコード済みの文字列を使うことができます。たとえば、先ほどの GET リクエストは次のようにも書けます。</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>c <span class=\"o\">=</span> Client<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s2\">&quot;/customers/details/?name=fred&amp;age=7&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>エンコード済みの GET データと data 引数の両方が与えられた場合には、data 引数の方が優先されます。</p>\n<p><code class=\"docutils literal notranslate\">follow</code> を <code class=\"docutils literal notranslate\">True</code> を与えると、クライアントはすべてのリダイレクトを辿り、途中の URL とステータスコードのタプルが、レスポンスオブジェクトの <code class=\"docutils literal notranslate\">redirect_chain</code> 属性に追加されてゆきます。</p>\n<p>たとえば、URL <code class=\"docutils literal notranslate\">/redirect_me/</code> が <code class=\"docutils literal notranslate\">/next/</code> にリダイレクトし、それがさらに <code class=\"docutils literal notranslate\">/final/</code> にリダイレクトするような場合には、<code class=\"docutils literal notranslate\">redirect_chain</code> は次のような値になります。</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>response <span class=\"o\">=</span> c<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s2\">&quot;/redirect_me/&quot;</span><span class=\"p\">,</span> follow<span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>response<span class=\"o\">.</span>redirect_chain\n<span class=\"go\">[(&#39;http://testserver/next/&#39;, 302), (&#39;http://testserver/final/&#39;, 302)]</span>\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\">secure</code> を <code class=\"docutils literal notranslate\">True</code> に設定すると、クライアントは HTTPS リクエストをエミュレートします。</p>\n<aside class=\"version-note version-changed\" data-version=\"4.2\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 4.2</p><p>The <code class=\"docutils literal notranslate\">headers</code> parameter was added.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.Client.post\">\n<span class=\"sig-name descname\">post</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">path</em>, <em class=\"sig-param\">data<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">content_type<span class=\"o\">=</span><span class=\"default_value\">MULTIPART_CONTENT</span></em>, <em class=\"sig-param\">follow<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">secure<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\">*</abbr></span></em>, <em class=\"sig-param\">headers<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>extra</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.Client.post\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>与えられた <code class=\"docutils literal notranslate\">path</code> に対して POST リクエストを作り、<code class=\"docutils literal notranslate\">Response</code> オブジェクトを返します。<code class=\"docutils literal notranslate\">Response</code> オブジェクトについては、下のセクションにドキュメントされています。</p>\n<p><code class=\"docutils literal notranslate\">data</code> ディクショナリ内の key-value ペアは、POST データを送信するのに使われます。例えば、次の例では、</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>c <span class=\"o\">=</span> Client<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"o\">.</span>post<span class=\"p\">(</span><span class=\"s2\">&quot;/login/&quot;</span><span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s2\">&quot;name&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;fred&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;passwd&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;secret&quot;</span><span class=\"p\">})</span>\n</code></pre></div>\n<p>引数の評価の結果、次の URL へ POST リクエストが行われます。</p>\n<div class=\"code-block\" data-language=\"text\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Text</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=\"Text code\"><code>/login/\n</code></pre></div>\n<p>リクエストで送られる POST データは次のものになります。</p>\n<div class=\"code-block\" data-language=\"text\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Text</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=\"Text code\"><code>name=fred&amp;passwd=secret\n</code></pre></div>\n<p>If you provide <code class=\"docutils literal notranslate\">content_type</code> as <em class=\"mimetype\">application/json</em>, the\n<code class=\"docutils literal notranslate\">data</code> is serialized using <a class=\"reference external\" href=\"https://docs.python.org/3/library/json.html#json.dumps\" title=\"(in Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\">json.dumps()</code></a> if it's a dict, list,\nor tuple. Serialization is performed with\n<a class=\"reference internal\" href=\"/ja/4.2/topics/serialization/#django.core.serializers.json.DjangoJSONEncoder\" title=\"django.core.serializers.json.DjangoJSONEncoder\"><code class=\"xref py py-class docutils literal notranslate\">DjangoJSONEncoder</code></a> by default,\nand can be overridden by providing a <code class=\"docutils literal notranslate\">json_encoder</code> argument to\n<a class=\"reference internal\" href=\"#django.test.Client\" title=\"django.test.Client\"><code class=\"xref py py-class docutils literal notranslate\">Client</code></a>. This serialization also happens for <a class=\"reference internal\" href=\"#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=\"#django.test.Client.patch\" title=\"django.test.Client.patch\"><code class=\"xref py py-meth docutils literal notranslate\">patch()</code></a>, and <a class=\"reference internal\" href=\"#django.test.Client.delete\" title=\"django.test.Client.delete\"><code class=\"xref py py-meth docutils literal notranslate\">delete()</code></a> requests.</p>\n<p>もし他の <code class=\"docutils literal notranslate\">content_type</code> (たとえば、XML データの場合は <em class=\"mimetype\">text/xml</em>) を与えると、<code class=\"docutils literal notranslate\">data</code> のコンテンツは、HTTP の <code class=\"docutils literal notranslate\">Content-Type</code> ヘッダーの中で <code class=\"docutils literal notranslate\">content_type</code> を使ったものとして、POST リクエストで送られます。</p>\n<p><code class=\"docutils literal notranslate\">content_type</code> に値を渡さなかったときは、<code class=\"docutils literal notranslate\">data</code> 内の値を <em class=\"mimetype\">multipart/form-data</em> のコンテンツタイプとして送信します。この場合は、 <code class=\"docutils literal notranslate\">data</code> 内のkey-value ペアが multipart メッセージにえんこーどされ、POST データを生成するのに使われます。</p>\n<p>たとえば <code class=\"docutils literal notranslate\">&lt;select multiple&gt;</code> の複数の選択を指定する場合のように、特定のキーに対して複数の値を送信したいときは、必要なキーに対する値をリストまたはタプルとして与えます。たとえば、 <code class=\"docutils literal notranslate\">data</code> に次の値を与えれば、3つの選択した値を <code class=\"docutils literal notranslate\">choice</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=\"p\">{</span><span class=\"s2\">&quot;choices&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;a&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;b&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;d&quot;</span><span class=\"p\">]}</span>\n</code></pre></div>\n<p>Submitting files is a special case. To POST a file, you need only\nprovide the file field name as a key, and a file handle to the file you\nwish to upload as a value. For example, if your form has fields\n<code class=\"docutils literal notranslate\">name</code> and <code class=\"docutils literal notranslate\">attachment</code>, the latter a\n<a class=\"reference internal\" href=\"/ja/4.2/ref/forms/fields/#django.forms.FileField\" title=\"django.forms.FileField\"><code class=\"xref py py-class docutils literal notranslate\">FileField</code></a>:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>c <span class=\"o\">=</span> Client<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">with</span> <span class=\"nb\">open</span><span class=\"p\">(</span><span class=\"s2\">&quot;wishlist.doc&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;rb&quot;</span><span class=\"p\">)</span> <span class=\"k\">as</span> fp<span class=\"p\">:</span>\n<span class=\"gp\">... </span>    c<span class=\"o\">.</span>post<span class=\"p\">(</span><span class=\"s2\">&quot;/customers/wishes/&quot;</span><span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s2\">&quot;name&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;fred&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;attachment&quot;</span><span class=\"p\">:</span> fp<span class=\"p\">})</span>\n<span class=\"gp\">...</span>\n</code></pre></div>\n<p>You may also provide any file-like object (e.g., <a class=\"reference external\" href=\"https://docs.python.org/3/library/io.html#io.StringIO\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">StringIO</code></a> or\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/io.html#io.BytesIO\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">BytesIO</code></a>) as a file handle. If you're uploading to an\n<a class=\"reference internal\" href=\"/ja/4.2/ref/models/fields/#django.db.models.ImageField\" title=\"django.db.models.ImageField\"><code class=\"xref py py-class docutils literal notranslate\">ImageField</code></a>, the object needs a <code class=\"docutils literal notranslate\">name</code>\nattribute that passes the\n<a class=\"reference internal\" href=\"/ja/4.2/ref/validators/#django.core.validators.validate_image_file_extension\" title=\"django.core.validators.validate_image_file_extension\"><code class=\"xref py py-data docutils literal notranslate\">validate_image_file_extension</code></a> validator.\nFor example:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">io</span> <span class=\"kn\">import</span> BytesIO\n<span class=\"gp\">&gt;&gt;&gt; </span>img <span class=\"o\">=</span> BytesIO<span class=\"p\">(</span>\n<span class=\"gp\">... </span>    <span class=\"sa\">b</span><span class=\"s2\">&quot;GIF89a</span><span class=\"se\">\\x01\\x00\\x01\\x00\\x00\\x00\\x00</span><span class=\"s2\">!</span><span class=\"se\">\\xf9\\x04\\x01\\x00\\x00\\x00</span><span class=\"s2\">&quot;</span>\n<span class=\"gp\">... </span>    <span class=\"sa\">b</span><span class=\"s2\">&quot;</span><span class=\"se\">\\x00</span><span class=\"s2\">,</span><span class=\"se\">\\x00\\x00\\x00\\x00\\x01\\x00\\x01\\x00\\x00\\x02\\x01\\x00\\x00</span><span class=\"s2\">&quot;</span>\n<span class=\"gp\">... </span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>img<span class=\"o\">.</span>name <span class=\"o\">=</span> <span class=\"s2\">&quot;myimage.gif&quot;</span>\n</code></pre></div>\n<p>複数の <code class=\"docutils literal notranslate\">post()</code> の呼び出しに対して同じファイルハンドラを使う時には、post 間でファイルポインタを手動でリセットする必要があります。これを一番簡単に扱う方法は、上に示したように、ファイルが <code class=\"docutils literal notranslate\">post()</code> に与えられた後に手動でファイルを close することです。</p>\n<p>データが読み込めるように、正しい方法でファイルを開くようにする必要があります。これはつまり、画像ファイルなどのバイナリデータが含まれている場合には、<code class=\"docutils literal notranslate\">rb</code> (read binary、バイナリ読み込み) モードで開かなければならないということです。</p>\n<p><code class=\"docutils literal notranslate\">headers</code> と <code class=\"docutils literal notranslate\">extra</code> 引数は、<a class=\"reference internal\" href=\"#django.test.Client.get\" title=\"django.test.Client.get\"><code class=\"xref py py-meth docutils literal notranslate\">Client.get()</code></a> と同じように振る舞います。</p>\n<p>POST でリクエストした URL にエンコード済みパラメータが含まれている場合には、これらのデータは request.GET データから利用できます。たとえば、次のようなリクエストを行った場合、</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"o\">.</span>post<span class=\"p\">(</span><span class=\"s2\">&quot;/login/?visitor=true&quot;</span><span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s2\">&quot;name&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;fred&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;passwd&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;secret&quot;</span><span class=\"p\">})</span>\n</code></pre></div>\n<p>このリクエストをハンドリングするビューでは、request.POST からはユーザー名とパスワードを取得し、request.GET からはユーザーが visitor であるかどうかを特定することができます。</p>\n<p><code class=\"docutils literal notranslate\">follow</code> を <code class=\"docutils literal notranslate\">True</code> を与えると、クライアントはすべてのリダイレクトを辿り、途中の URL とステータスコードのタプルが、レスポンスオブジェクトの <code class=\"docutils literal notranslate\">redirect_chain</code> 属性に追加されてゆきます。</p>\n<p><code class=\"docutils literal notranslate\">secure</code> を <code class=\"docutils literal notranslate\">True</code> に設定すると、クライアントは HTTPS リクエストをエミュレートします。</p>\n<aside class=\"version-note version-changed\" data-version=\"4.2\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 4.2</p><p>The <code class=\"docutils literal notranslate\">headers</code> parameter was added.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.Client.head\">\n<span class=\"sig-name descname\">head</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">path</em>, <em class=\"sig-param\">data<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">follow<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">secure<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\">*</abbr></span></em>, <em class=\"sig-param\">headers<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>extra</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.Client.head\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>与えられた <code class=\"docutils literal notranslate\">path</code> に対して HEAD リクエストを作り、<code class=\"docutils literal notranslate\">Response</code> オブジェクトを返します。message body を返さない点を除いて、<a class=\"reference internal\" href=\"#django.test.Client.get\" title=\"django.test.Client.get\"><code class=\"xref py py-meth docutils literal notranslate\">Client.get()</code></a> と同じように動作します。<code class=\"docutils literal notranslate\">follow</code>、<code class=\"docutils literal notranslate\">secure</code>、<code class=\"docutils literal notranslate\">headers</code>、<code class=\"docutils literal notranslate\">extra</code> の引数の動作も同様です。</p>\n<aside class=\"version-note version-changed\" data-version=\"4.2\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 4.2</p><p>The <code class=\"docutils literal notranslate\">headers</code> parameter was added.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.Client.options\">\n<span class=\"sig-name descname\">options</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">path</em>, <em class=\"sig-param\">data<span class=\"o\">=</span><span class=\"default_value\">''</span></em>, <em class=\"sig-param\">content_type<span class=\"o\">=</span><span class=\"default_value\">'application/octet-stream'</span></em>, <em class=\"sig-param\">follow<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">secure<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\">*</abbr></span></em>, <em class=\"sig-param\">headers<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>extra</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.Client.options\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>与えられた <code class=\"docutils literal notranslate\">path</code> に対して OPTION リクエストを作り、<code class=\"docutils literal notranslate\">Response</code> オブジェクトを返します。RESTful インターフェイスのテスト時に有用です。</p>\n<p><code class=\"docutils literal notranslate\">data</code> が与えられると、request body として使われます。 <code class=\"docutils literal notranslate\">content_type</code> は <code class=\"docutils literal notranslate\">Content-Type</code> ヘッダーに設定されます。</p>\n<p><code class=\"docutils literal notranslate\">follow</code>、<code class=\"docutils literal notranslate\">secure</code>、<code class=\"docutils literal notranslate\">headers</code>、<code class=\"docutils literal notranslate\">extra</code> 引数は、<a class=\"reference internal\" href=\"#django.test.Client.get\" title=\"django.test.Client.get\"><code class=\"xref py py-meth docutils literal notranslate\">Client.get()</code></a> と同様に動作します。</p>\n<aside class=\"version-note version-changed\" data-version=\"4.2\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 4.2</p><p>The <code class=\"docutils literal notranslate\">headers</code> parameter was added.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.Client.put\">\n<span class=\"sig-name descname\">put</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">path</em>, <em class=\"sig-param\">data<span class=\"o\">=</span><span class=\"default_value\">''</span></em>, <em class=\"sig-param\">content_type<span class=\"o\">=</span><span class=\"default_value\">'application/octet-stream'</span></em>, <em class=\"sig-param\">follow<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">secure<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\">*</abbr></span></em>, <em class=\"sig-param\">headers<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>extra</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.Client.put\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>与えられた <code class=\"docutils literal notranslate\">path</code> に対して PUT リクエストを作り、<code class=\"docutils literal notranslate\">Response</code> オブジェクトを返します。RESTful インターフェイスのテスト時に有用です。</p>\n<p><code class=\"docutils literal notranslate\">data</code> が与えられると、request body として使われます。 <code class=\"docutils literal notranslate\">content_type</code> は <code class=\"docutils literal notranslate\">Content-Type</code> ヘッダーに設定されます。</p>\n<p><code class=\"docutils literal notranslate\">follow</code>、<code class=\"docutils literal notranslate\">secure</code>、<code class=\"docutils literal notranslate\">headers</code>、<code class=\"docutils literal notranslate\">extra</code> 引数は、<a class=\"reference internal\" href=\"#django.test.Client.get\" title=\"django.test.Client.get\"><code class=\"xref py py-meth docutils literal notranslate\">Client.get()</code></a> と同様に動作します。</p>\n<aside class=\"version-note version-changed\" data-version=\"4.2\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 4.2</p><p>The <code class=\"docutils literal notranslate\">headers</code> parameter was added.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.Client.patch\">\n<span class=\"sig-name descname\">patch</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">path</em>, <em class=\"sig-param\">data<span class=\"o\">=</span><span class=\"default_value\">''</span></em>, <em class=\"sig-param\">content_type<span class=\"o\">=</span><span class=\"default_value\">'application/octet-stream'</span></em>, <em class=\"sig-param\">follow<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">secure<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\">*</abbr></span></em>, <em class=\"sig-param\">headers<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>extra</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.Client.patch\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>与えられた <code class=\"docutils literal notranslate\">path</code> に対して PATCH リクエストを作り、<code class=\"docutils literal notranslate\">Response</code> オブジェクトを返します。RESTful インターフェイスのテスト時に有用です。</p>\n<p><code class=\"docutils literal notranslate\">follow</code>、<code class=\"docutils literal notranslate\">secure</code>、<code class=\"docutils literal notranslate\">headers</code>、<code class=\"docutils literal notranslate\">extra</code> 引数は、<a class=\"reference internal\" href=\"#django.test.Client.get\" title=\"django.test.Client.get\"><code class=\"xref py py-meth docutils literal notranslate\">Client.get()</code></a> と同様に動作します。</p>\n<aside class=\"version-note version-changed\" data-version=\"4.2\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 4.2</p><p>The <code class=\"docutils literal notranslate\">headers</code> parameter was added.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.Client.delete\">\n<span class=\"sig-name descname\">delete</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">path</em>, <em class=\"sig-param\">data<span class=\"o\">=</span><span class=\"default_value\">''</span></em>, <em class=\"sig-param\">content_type<span class=\"o\">=</span><span class=\"default_value\">'application/octet-stream'</span></em>, <em class=\"sig-param\">follow<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">secure<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\">*</abbr></span></em>, <em class=\"sig-param\">headers<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>extra</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.Client.delete\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>与えられた <code class=\"docutils literal notranslate\">path</code> に対して DELETE リクエストを作り、<code class=\"docutils literal notranslate\">Response</code> オブジェクトを返します。RESTful インターフェイスのテスト時に有用です。</p>\n<p><code class=\"docutils literal notranslate\">data</code> が与えられると、request body として使われます。 <code class=\"docutils literal notranslate\">content_type</code> は <code class=\"docutils literal notranslate\">Content-Type</code> ヘッダーに設定されます。</p>\n<p><code class=\"docutils literal notranslate\">follow</code>、<code class=\"docutils literal notranslate\">secure</code>、<code class=\"docutils literal notranslate\">headers</code>、<code class=\"docutils literal notranslate\">extra</code> 引数は、<a class=\"reference internal\" href=\"#django.test.Client.get\" title=\"django.test.Client.get\"><code class=\"xref py py-meth docutils literal notranslate\">Client.get()</code></a> と同様に動作します。</p>\n<aside class=\"version-note version-changed\" data-version=\"4.2\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 4.2</p><p>The <code class=\"docutils literal notranslate\">headers</code> parameter was added.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.Client.trace\">\n<span class=\"sig-name descname\">trace</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">path</em>, <em class=\"sig-param\">follow<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">secure<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\">*</abbr></span></em>, <em class=\"sig-param\">headers<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>extra</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.Client.trace\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>与えられた <code class=\"docutils literal notranslate\">path</code> に対して TRACE リクエストを作り、<code class=\"docutils literal notranslate\">Response</code> オブジェクトを返します。診断のための調査をシミュレートするときに役に立ちます。</p>\n<p>他のリクエストメソッドとは違い、 <code class=\"docutils literal notranslate\">data</code> がキーワード引数にありません。 <span class=\"target\" id=\"index-6\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc9110.html#section-9.3.8\"><strong>RFC 9110 Section 9.3.8</strong></a> に従うためです。そのため、TRACE リクエストには body を含むことが禁止されています。</p>\n<p><code class=\"docutils literal notranslate\">follow</code>、<code class=\"docutils literal notranslate\">secure</code>、<code class=\"docutils literal notranslate\">headers</code>、<code class=\"docutils literal notranslate\">extra</code> 引数は、<a class=\"reference internal\" href=\"#django.test.Client.get\" title=\"django.test.Client.get\"><code class=\"xref py py-meth docutils literal notranslate\">Client.get()</code></a> と同様に動作します。</p>\n<aside class=\"version-note version-changed\" data-version=\"4.2\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 4.2</p><p>The <code class=\"docutils literal notranslate\">headers</code> parameter was added.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.Client.login\">\n<span class=\"sig-name descname\">login</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">**</span>credentials</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.Client.login\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>あなたのサイトが Django の <a class=\"reference internal\" href=\"/ja/4.2/topics/auth/\"><span class=\"doc\">認証システム</span></a> を使っていて、ユーザーのログインをテストしたければ、テストクライアントの <code class=\"docutils literal notranslate\">login()</code> メソッドを使うことで、ユーザーがサイトにログインしたときの状況をシミュレートできます。</p>\n<p>このメソッドを呼ぶ事で、テストクライアントはログインに基づいてビューを形成するテストを行う上で必要なクッキーとセッション情報を全て持ちます。</p>\n<p>引数 <code class=\"docutils literal notranslate\">credentials</code> の形式は利用している (<a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-AUTHENTICATION_BACKENDS\"><code class=\"xref std std-setting docutils literal notranslate\">AUTHENTICATION_BACKENDS</code></a> の設定値に定義されています) <a class=\"reference internal\" href=\"/ja/4.2/topics/auth/customizing/#authentication-backends\"><span class=\"std std-ref\">認証バックエンド</span></a> に依存します。Django によって提供される標準の認証バックエンド (<code class=\"docutils literal notranslate\">ModelBackend</code>) を用いている場合は、 <code class=\"docutils literal notranslate\">credentials</code> は利用者のユーザー名とパスワードであり、キーワード引数として渡されます:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>c <span class=\"o\">=</span> Client<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"o\">.</span>login<span class=\"p\">(</span>username<span class=\"o\">=</span><span class=\"s2\">&quot;fred&quot;</span><span class=\"p\">,</span> password<span class=\"o\">=</span><span class=\"s2\">&quot;secret&quot;</span><span class=\"p\">)</span>\n\n<span class=\"go\"># Now you can access a view that&#39;s only available to logged-in users.</span>\n</code></pre></div>\n<p>もし標準以外の認証バックエンドを利用している場合、このメソッドは異なった認証情報を必要とします。この値では利用している認証バックエンドの <code class=\"docutils literal notranslate\">authenticate()</code> メソッドによって要求される認証情報が必要になります。</p>\n<p>認証情報が受け入れられてログインが成功した場合に <code class=\"docutils literal notranslate\">login()</code> は <code class=\"docutils literal notranslate\">True</code> を返します。</p>\n<p>Finally, you'll need to remember to create user accounts before you can\nuse this method. As we explained above, the test runner is executed\nusing a test database, which contains no users by default. As a result,\nuser accounts that are valid on your production site will not work\nunder test conditions. You'll need to create users as part of the test\nsuite -- either manually (using the Django model API) or with a test\nfixture. Remember that if you want your test user to have a password,\nyou can't set the user's password by setting the password attribute\ndirectly -- you must use the\n<a class=\"reference internal\" href=\"/ja/4.2/ref/contrib/auth/#django.contrib.auth.models.User.set_password\" title=\"django.contrib.auth.models.User.set_password\"><code class=\"xref py py-meth docutils literal notranslate\">set_password()</code></a> function to\nstore a correctly hashed password. Alternatively, you can use the\n<a class=\"reference internal\" href=\"/ja/4.2/ref/contrib/auth/#django.contrib.auth.models.UserManager.create_user\" title=\"django.contrib.auth.models.UserManager.create_user\"><code class=\"xref py py-meth docutils literal notranslate\">create_user()</code></a> helper\nmethod to create a new user with a correctly hashed password.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.Client.force_login\">\n<span class=\"sig-name descname\">force_login</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">user</em>, <em class=\"sig-param\">backend<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.Client.force_login\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>If your site uses Django's <a class=\"reference internal\" href=\"/ja/4.2/topics/auth/\"><span class=\"doc\">authentication\nsystem</span></a>, you can use the <code class=\"docutils literal notranslate\">force_login()</code> method\nto simulate the effect of a user logging into the site. Use this method\ninstead of <a class=\"reference internal\" href=\"#django.test.Client.login\" title=\"django.test.Client.login\"><code class=\"xref py py-meth docutils literal notranslate\">login()</code></a> when a test requires a user be logged in and\nthe details of how a user logged in aren't important.</p>\n<p>Unlike <code class=\"docutils literal notranslate\">login()</code>, this method skips the authentication and\nverification steps: inactive users (<a class=\"reference internal\" href=\"/ja/4.2/ref/contrib/auth/#django.contrib.auth.models.User.is_active\" title=\"django.contrib.auth.models.User.is_active\"><code class=\"xref py py-attr docutils literal notranslate\">is_active=False</code></a>) are permitted to login\nand the user's credentials don't need to be provided.</p>\n<p>The user will have its <code class=\"docutils literal notranslate\">backend</code> attribute set to the value of the\n<code class=\"docutils literal notranslate\">backend</code> argument (which should be a dotted Python path string), or\nto <code class=\"docutils literal notranslate\">settings.AUTHENTICATION_BACKENDS[0]</code> if a value isn't provided.\nThe <a class=\"reference internal\" href=\"/ja/4.2/topics/auth/default/#django.contrib.auth.authenticate\" title=\"django.contrib.auth.authenticate\"><code class=\"xref py py-func docutils literal notranslate\">authenticate()</code></a> function called by\n<a class=\"reference internal\" href=\"#django.test.Client.login\" title=\"django.test.Client.login\"><code class=\"xref py py-meth docutils literal notranslate\">login()</code></a> normally annotates the user like this.</p>\n<p>This method is faster than <code class=\"docutils literal notranslate\">login()</code> since the expensive\npassword hashing algorithms are bypassed. Also, you can speed up\n<code class=\"docutils literal notranslate\">login()</code> by <a class=\"reference internal\" href=\"/ja/4.2/topics/testing/overview/#speeding-up-tests-auth-hashers\"><span class=\"std std-ref\">using a weaker hasher while testing</span></a>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.Client.logout\">\n<span class=\"sig-name descname\">logout</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.Client.logout\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>If your site uses Django's <a class=\"reference internal\" href=\"/ja/4.2/topics/auth/\"><span class=\"doc\">authentication system</span></a>,\nthe <code class=\"docutils literal notranslate\">logout()</code> method can be used to simulate the effect of a user\nlogging out of your site.</p>\n<p>After you call this method, the test client will have all the cookies\nand session data cleared to defaults. Subsequent requests will appear\nto come from an <a class=\"reference internal\" href=\"/ja/4.2/ref/contrib/auth/#django.contrib.auth.models.AnonymousUser\" title=\"django.contrib.auth.models.AnonymousUser\"><code class=\"xref py py-class docutils literal notranslate\">AnonymousUser</code></a>.</p>\n</dd></dl>\n\n</dd></dl>\n\n</section>\n<section id=\"testing-responses\">\n<h3>レスポンスのテスト<a class=\"heading-anchor\" href=\"#testing-responses\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><code class=\"docutils literal notranslate\">get()</code> および <code class=\"docutils literal notranslate\">post()</code> メソッドは、両方とも <code class=\"docutils literal notranslate\">Response</code> オブジェクトを返します。この``Response`` オブジェクトは、Django のビューによって返される <code class=\"docutils literal notranslate\">HttpResponse</code> オブジェクトとは <em>異なるものです</em>; テストのレスポンスは、テストコードの検証に役立ついくつかの追加データを持ちます。</p>\n<p>特に、<code class=\"docutils literal notranslate\">Response</code> オブジェクトは以下の属性を持ちます:</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.test.Response\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">Response</span><a class=\"heading-anchor\" href=\"#django.test.Response\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.test.Response.client\">\n<span class=\"sig-name descname\">client</span><a class=\"heading-anchor\" href=\"#django.test.Response.client\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>レスポンスの結果の元となるリクエストを作るために使われた、テストクライアントです。</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.test.Response.content\">\n<span class=\"sig-name descname\">content</span><a class=\"heading-anchor\" href=\"#django.test.Response.content\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Bytestring としてのレスポンスの本文です。ビューないしエラーメッセージによってレンダリングされる際の最終的なページコンテンツです。</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.test.Response.context\">\n<span class=\"sig-name descname\">context</span><a class=\"heading-anchor\" href=\"#django.test.Response.context\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>テンプレートの <code class=\"docutils literal notranslate\">Context</code> インスタンスです。レスポンスの content を生成するテンプレートをレンダリングする際に使われます。</p>\n<p>描画されたページが複数のテンプレートを使っていた場合、<code class=\"docutils literal notranslate\">context</code> は``Context`` オブジェクトのリストとなり、その順序はレンダリングされた順となります。</p>\n<p>レンダリングに使われるテンプレートの数にかかわらず、<code class=\"docutils literal notranslate\">[]</code> オペレータを使ってコンテキストの値を取り出すことができます。たとえば、コンテキストの変数 <code class=\"docutils literal notranslate\">name</code> は以下のように取り出せます。</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>response <span class=\"o\">=</span> client<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s2\">&quot;/foo/&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>response<span class=\"o\">.</span>context<span class=\"p\">[</span><span class=\"s2\">&quot;name&quot;</span><span class=\"p\">]</span>\n<span class=\"go\">&#39;Arthur&#39;</span>\n</code></pre></div>\n<aside class=\"admonition-not-using-django-templates admonition\" role=\"note\">\n<p class=\"admonition-title\">Django のテンプレートを使っていない?</p>\n<p>この属性は、<a class=\"reference internal\" href=\"/ja/4.2/topics/templates/#django.template.backends.django.DjangoTemplates\" title=\"django.template.backends.django.DjangoTemplates\"><code class=\"xref py py-class docutils literal notranslate\">DjangoTemplates</code></a> バックエンドを使用しているときのみ格納されます。他のテンプレートエンジンを使っている場合、<a class=\"reference internal\" href=\"/ja/4.2/ref/template-response/#django.template.response.SimpleTemplateResponse.context_data\" title=\"django.template.response.SimpleTemplateResponse.context_data\"><code class=\"xref py py-attr docutils literal notranslate\">context_data</code></a> がこの属性を扱うレスポンスの適切な選択肢となるでしょう。</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.test.Response.exc_info\">\n<span class=\"sig-name descname\">exc_info</span><a class=\"heading-anchor\" href=\"#django.test.Response.exc_info\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A tuple of three values that provides information about the unhandled\nexception, if any, that occurred during the view.</p>\n<p>The values are (type, value, traceback), the same as returned by\nPython's <a class=\"reference external\" href=\"https://docs.python.org/3/library/sys.html#sys.exc_info\" title=\"(in Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\">sys.exc_info()</code></a>. Their meanings are:</p>\n<ul class=\"simple\">\n<li><p><em>type</em>: The type of the exception.</p></li>\n<li><p><em>value</em>: The exception instance.</p></li>\n<li><p><em>traceback</em>: A traceback object which encapsulates the call stack at\nthe point where the exception originally occurred.</p></li>\n</ul>\n<p>If no exception occurred, then <code class=\"docutils literal notranslate\">exc_info</code> will be <code class=\"docutils literal notranslate\">None</code>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.Response.json\">\n<span class=\"sig-name descname\">json</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.Response.json\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>JSON としてパースされた、レスポンスの本文です。追加のキーワード引数が <a class=\"reference external\" href=\"https://docs.python.org/3/library/json.html#json.loads\" title=\"(in Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\">json.loads()</code></a> に渡されます。たとえば、次のようになります。</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>response <span class=\"o\">=</span> client<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s2\">&quot;/foo/&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>response<span class=\"o\">.</span>json<span class=\"p\">()[</span><span class=\"s2\">&quot;name&quot;</span><span class=\"p\">]</span>\n<span class=\"go\">&#39;Arthur&#39;</span>\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\">Content-Type</code> ヘッダが <code class=\"docutils literal notranslate\">&quot;application/json&quot;</code> ではない場合、レスポンスをパースする際に <a class=\"reference external\" href=\"https://docs.python.org/3/library/exceptions.html#ValueError\" title=\"(in Python v3.14)\"><code class=\"xref py py-exc docutils literal notranslate\">ValueError</code></a> が投げられます。</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.test.Response.request\">\n<span class=\"sig-name descname\">request</span><a class=\"heading-anchor\" href=\"#django.test.Response.request\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>レスポンスを機能させるリクエストデータです。</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.test.Response.wsgi_request\">\n<span class=\"sig-name descname\">wsgi_request</span><a class=\"heading-anchor\" href=\"#django.test.Response.wsgi_request\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>レスポンスにより生成されたテストヘッダーが生成する <code class=\"docutils literal notranslate\">WSGIRequest</code> インスタンスです。</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.test.Response.status_code\">\n<span class=\"sig-name descname\">status_code</span><a class=\"heading-anchor\" href=\"#django.test.Response.status_code\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>レスポンスの HTTP ステータスで、数値です。 定義済みのコードの全リストは、<a class=\"reference external\" href=\"https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml\">IANA status code registry</a> を参照してください。</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.test.Response.templates\">\n<span class=\"sig-name descname\">templates</span><a class=\"heading-anchor\" href=\"#django.test.Response.templates\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>最終コンテンツをレンダリングするときに使われる <code class=\"docutils literal notranslate\">Template</code> インスタンスのリストで、 レンダリングされる順となります。テンプレートがファイルから読み込まれる場合、テンプレートのファイル名を取得するためには、リスト内の各テンプレートに対して <code class=\"docutils literal notranslate\">template.name</code> を使ってください (名前は <code class=\"docutils literal notranslate\">'admin/index.html'</code> のような文字列となります)。</p>\n<aside class=\"admonition-not-using-django-templates admonition\" role=\"note\">\n<p class=\"admonition-title\">Django のテンプレートを使っていない?</p>\n<p>この属性は、<a class=\"reference internal\" href=\"/ja/4.2/topics/templates/#django.template.backends.django.DjangoTemplates\" title=\"django.template.backends.django.DjangoTemplates\"><code class=\"xref py py-class docutils literal notranslate\">DjangoTemplates</code></a> バックエンドを使用しているときのみ格納されます。他のテンプレートを知使っている場合、<a class=\"reference internal\" href=\"/ja/4.2/ref/template-response/#django.template.response.SimpleTemplateResponse.template_name\" title=\"django.template.response.SimpleTemplateResponse.template_name\"><code class=\"xref py py-attr docutils literal notranslate\">template_name</code></a> が、レンダリングに使われるテンプレートの名前のみが必要な場合の適切な代替策となるでしょう。</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.test.Response.resolver_match\">\n<span class=\"sig-name descname\">resolver_match</span><a class=\"heading-anchor\" href=\"#django.test.Response.resolver_match\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>レスポンスに対する <a class=\"reference internal\" href=\"/ja/4.2/ref/urlresolvers/#django.urls.ResolverMatch\" title=\"django.urls.ResolverMatch\"><code class=\"xref py py-class docutils literal notranslate\">ResolverMatch</code></a> のインスタンスです。<a class=\"reference internal\" href=\"/ja/4.2/ref/urlresolvers/#django.urls.ResolverMatch.func\" title=\"django.urls.ResolverMatch.func\"><code class=\"xref py py-attr docutils literal notranslate\">func</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=\"c1\"># my_view here is a function based view.</span>\n<span class=\"bp\">self</span><span class=\"o\">.</span>assertEqual<span class=\"p\">(</span>response<span class=\"o\">.</span>resolver_match<span class=\"o\">.</span>func<span class=\"p\">,</span> my_view<span class=\"p\">)</span>\n\n<span class=\"c1\"># Class-based views need to compare the view_class, as the</span>\n<span class=\"c1\"># functions generated by as_view() won&#39;t be equal.</span>\n<span class=\"bp\">self</span><span class=\"o\">.</span>assertIs<span class=\"p\">(</span>response<span class=\"o\">.</span>resolver_match<span class=\"o\">.</span>func<span class=\"o\">.</span>view_class<span class=\"p\">,</span> MyView<span class=\"p\">)</span>\n</code></pre></div>\n<p>指定された URL が見つからない場合、この属性にアクセスすると <a class=\"reference internal\" href=\"/ja/4.2/ref/exceptions/#django.urls.Resolver404\" title=\"django.urls.Resolver404\"><code class=\"xref py py-exc docutils literal notranslate\">Resolver404</code></a> 例外が投げられます。</p>\n</dd></dl>\n\n</dd></dl>\n\n<p>As with a normal response, you can also access the headers through\n<a class=\"reference internal\" href=\"/ja/4.2/ref/request-response/#django.http.HttpResponse.headers\" title=\"django.http.HttpResponse.headers\"><code class=\"xref py py-attr docutils literal notranslate\">HttpResponse.headers</code></a>. For example, you could determine the content\ntype of a response using <code class=\"docutils literal notranslate\">response.headers['Content-Type']</code>.</p>\n</section>\n<section id=\"exceptions\">\n<h3>例外<a class=\"heading-anchor\" href=\"#exceptions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you point the test client at a view that raises an exception and\n<code class=\"docutils literal notranslate\">Client.raise_request_exception</code> is <code class=\"docutils literal notranslate\">True</code>, that exception will be visible\nin the test case. You can then use a standard <code class=\"docutils literal notranslate\">try ... except</code> block or\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaises\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\">assertRaises()</code></a> to test for exceptions.</p>\n<p>The only exceptions that are not visible to the test client are\n<a class=\"reference internal\" href=\"/ja/4.2/topics/http/views/#django.http.Http404\" title=\"django.http.Http404\"><code class=\"xref py py-class docutils literal notranslate\">Http404</code></a>,\n<a class=\"reference internal\" href=\"/ja/4.2/ref/exceptions/#django.core.exceptions.PermissionDenied\" title=\"django.core.exceptions.PermissionDenied\"><code class=\"xref py py-class docutils literal notranslate\">PermissionDenied</code></a>, <a class=\"reference external\" href=\"https://docs.python.org/3/library/exceptions.html#SystemExit\" title=\"(in Python v3.14)\"><code class=\"xref py py-exc docutils literal notranslate\">SystemExit</code></a>, and\n<a class=\"reference internal\" href=\"/ja/4.2/ref/exceptions/#django.core.exceptions.SuspiciousOperation\" title=\"django.core.exceptions.SuspiciousOperation\"><code class=\"xref py py-class docutils literal notranslate\">SuspiciousOperation</code></a>. Django catches these\nexceptions internally and converts them into the appropriate HTTP response\ncodes. In these cases, you can check <code class=\"docutils literal notranslate\">response.status_code</code> in your test.</p>\n<p>If <code class=\"docutils literal notranslate\">Client.raise_request_exception</code> is <code class=\"docutils literal notranslate\">False</code>, the test client will return a\n500 response as would be returned to a browser. The response has the attribute\n<a class=\"reference internal\" href=\"#django.test.Response.exc_info\" title=\"django.test.Response.exc_info\"><code class=\"xref py py-attr docutils literal notranslate\">exc_info</code></a> to provide information about the unhandled\nexception.</p>\n</section>\n<section id=\"persistent-state\">\n<h3>永続的なステート<a class=\"heading-anchor\" href=\"#persistent-state\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>テストクライアントはステートフルです。レスポンスがクッキーを返す場合、そのクッキーはテストクライアント内に保持され、以降すべての <code class=\"docutils literal notranslate\">get()</code> および <code class=\"docutils literal notranslate\">post()</code> リクエストともに送信されます。</p>\n<p>クッキーに対する有効期限ポリシーは使えません。クッキーを期限切れにしたい場合、手動で削除するか、新たに (すべてのクッキーを削除するために) <code class=\"docutils literal notranslate\">Client</code> を作成してください。</p>\n<p>テストクライアントは、永続的なステート情報を保持する属性を持ちます。テスト条件の一環として、これらのプロパティにアクセスすることができます。</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.test.Client.cookies\">\n<span class=\"sig-prename descclassname\">Client.</span><span class=\"sig-name descname\">cookies</span><a class=\"heading-anchor\" href=\"#django.test.Client.cookies\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Python の <a class=\"reference external\" href=\"https://docs.python.org/3/library/http.cookies.html#http.cookies.SimpleCookie\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">SimpleCookie</code></a> オブジェクトで、すべてのクライアントのクッキーの現在の値を含みます。より詳しくは、<a class=\"reference external\" href=\"https://docs.python.org/3/library/http.cookies.html#module-http.cookies\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\">http.cookies</code></a> のドキュメントを参照してください。</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.test.Client.session\">\n<span class=\"sig-prename descclassname\">Client.</span><span class=\"sig-name descname\">session</span><a class=\"heading-anchor\" href=\"#django.test.Client.session\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>辞書形式のようなオブジェクトで、セッション情報を含みます。<a class=\"reference internal\" href=\"/ja/4.2/topics/http/sessions/\"><span class=\"doc\">セッションのドキュメント</span></a> に網羅的情報があります。</p>\n<p>セッションを修正し保存するためには、まず変数に格納される必要があります (新しい <code class=\"docutils literal notranslate\">SessionStore</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=\"k\">def</span> <span class=\"nf\">test_something</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n    session <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>client<span class=\"o\">.</span>session\n    session<span class=\"p\">[</span><span class=\"s2\">&quot;somekey&quot;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;test&quot;</span>\n    session<span class=\"o\">.</span>save<span class=\"p\">()</span>\n</code></pre></div>\n</dd></dl>\n\n</section>\n<section id=\"setting-the-language\">\n<h3>言語の設定<a class=\"heading-anchor\" href=\"#setting-the-language\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>国際化とローカルかをサポートしているアプリケーションをテストする際、テストクライアントのリクエストに対して言語を設定するのがよいでしょう。その方法は、<a class=\"reference internal\" href=\"/ja/4.2/ref/middleware/#django.middleware.locale.LocaleMiddleware\" title=\"django.middleware.locale.LocaleMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">LocaleMiddleware</code></a> が有効化されているか否かによって異なります。</p>\n<p>ミドルウェアが有効化されている場合、<a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-LANGUAGE_COOKIE_NAME\"><code class=\"xref std std-setting docutils literal notranslate\">LANGUAGE_COOKIE_NAME</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=\"nn\">django.conf</span> <span class=\"kn\">import</span> settings\n\n\n<span class=\"k\">def</span> <span class=\"nf\">test_language_using_cookie</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n    <span class=\"bp\">self</span><span class=\"o\">.</span>client<span class=\"o\">.</span>cookies<span class=\"o\">.</span>load<span class=\"p\">({</span>settings<span class=\"o\">.</span>LANGUAGE_COOKIE_NAME<span class=\"p\">:</span> <span class=\"s2\">&quot;fr&quot;</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=\"s2\">&quot;/&quot;</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>content<span class=\"p\">,</span> <span class=\"sa\">b</span><span class=\"s2\">&quot;Bienvenue sur mon site.&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>もしくは、<code class=\"docutils literal notranslate\">Accept-Language</code> HTTP ヘッダをリクエスト内に含めます:</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\">def</span> <span class=\"nf\">test_language_using_header</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=\"s2\">&quot;/&quot;</span><span class=\"p\">,</span> headers<span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s2\">&quot;accept-language&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;fr&quot;</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>content<span class=\"p\">,</span> <span class=\"sa\">b</span><span class=\"s2\">&quot;Bienvenue sur mon site.&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>When using these methods, ensure to reset the active language at the end of\neach test:</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\">def</span> <span class=\"nf\">tearDown</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n    translation<span class=\"o\">.</span>activate<span class=\"p\">(</span>settings<span class=\"o\">.</span>LANGUAGE_CODE<span class=\"p\">)</span>\n</code></pre></div>\n</aside>\n<p>さらなる詳細は <a class=\"reference internal\" href=\"/ja/4.2/topics/i18n/translation/#how-django-discovers-language-preference\"><span class=\"std std-ref\">How Django discovers language preference</span></a> にあります。</p>\n<p>ミドルウェアが有効化されていない場合、アクティブな言語は <a class=\"reference internal\" href=\"/ja/4.2/ref/utils/#django.utils.translation.override\" title=\"django.utils.translation.override\"><code class=\"xref py py-func docutils literal notranslate\">translation.override()</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=\"nn\">django.utils</span> <span class=\"kn\">import</span> translation\n\n\n<span class=\"k\">def</span> <span class=\"nf\">test_language_using_override</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n    <span class=\"k\">with</span> translation<span class=\"o\">.</span>override<span class=\"p\">(</span><span class=\"s2\">&quot;fr&quot;</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=\"s2\">&quot;/&quot;</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>content<span class=\"p\">,</span> <span class=\"sa\">b</span><span class=\"s2\">&quot;Bienvenue sur mon site.&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>さらなる詳細は <a class=\"reference internal\" href=\"/ja/4.2/topics/i18n/translation/#explicitly-setting-the-active-language\"><span class=\"std std-ref\">明示的にアクティブな言語をセットする</span></a> にあります。</p>\n</section>\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>以下は、テストクライアントを使ったユニットテストです。</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\">import</span> <span class=\"nn\">unittest</span>\n<span class=\"kn\">from</span> <span class=\"nn\">django.test</span> <span class=\"kn\">import</span> Client\n\n\n<span class=\"k\">class</span> <span class=\"nc\">SimpleTest</span><span class=\"p\">(</span>unittest<span class=\"o\">.</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 a client.</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>client <span class=\"o\">=</span> Client<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\"># Issue a GET request.</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=\"s2\">&quot;/customer/details/&quot;</span><span class=\"p\">)</span>\n\n        <span class=\"c1\"># Check that the response is 200 OK.</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\n        <span class=\"c1\"># Check that the rendered context contains 5 customers.</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>assertEqual<span class=\"p\">(</span><span class=\"nb\">len</span><span class=\"p\">(</span>response<span class=\"o\">.</span>context<span class=\"p\">[</span><span class=\"s2\">&quot;customers&quot;</span><span class=\"p\">]),</span> <span class=\"mi\">5</span><span class=\"p\">)</span>\n</code></pre></div>\n<aside class=\"admonition admonition-seealso\" role=\"note\">\n<p class=\"admonition-title\">参考</p>\n<p><a class=\"reference internal\" href=\"/ja/4.2/topics/testing/advanced/#django.test.RequestFactory\" title=\"django.test.RequestFactory\"><code class=\"xref py py-class docutils literal notranslate\">django.test.RequestFactory</code></a></p>\n</aside>\n</section>\n</section>\n<section id=\"provided-test-case-classes\">\n<span id=\"django-testcase-subclasses\"></span><h2>提供されるテストケースのクラス<a class=\"heading-anchor\" href=\"#provided-test-case-classes\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>標準の Python ユニットテストのクラスは、<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\">unittest.TestCase</code></a> のベースクラスを拡張しています。Django は、このベースクラスのいくつかの拡張を提供します。</p>\n<figure class=\"align-default\" id=\"id4\">\n<span id=\"testcase-hierarchy-diagram\"></span><a class=\"reference internal image-reference\" href=\"../../../../../../_images/django_unittest_classes_hierarchy.svg\"><img alt=\"Django ユニットテストクラスの階層 (TestCase サブクラス)\" src=\"/ja/4.2/_images/django_unittest_classes_hierarchy.svg\" style=\"width: 508px; height: 328px;\" />\n</a>\n<figcaption>\n<p><span class=\"caption-text\">Django ユニットテストクラスの階層</span><a class=\"heading-anchor\" href=\"#id4\"><span class=\"visually-hidden\">Link to this image</span><span aria-hidden=\"true\">#</span></a></p>\n</figcaption>\n</figure>\n<p>You can convert a normal <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\">unittest.TestCase</code></a> to any of the subclasses:\nchange the base class of your test from <code class=\"docutils literal notranslate\">unittest.TestCase</code> to the subclass.\nAll of the standard Python unit test functionality will be available, and it\nwill be augmented with some useful additions as described in each section\nbelow.</p>\n<section id=\"simpletestcase\">\n<h3><code class=\"docutils literal notranslate\">SimpleTestCase</code><a class=\"heading-anchor\" href=\"#simpletestcase\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">SimpleTestCase</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\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\">unittest.TestCase</code></a> のサブクラスです:</p>\n<ul class=\"simple\">\n<li><p>以下のような便利なアサーション:</p>\n<ul>\n<li><p>Checking that a callable <a class=\"reference internal\" href=\"#django.test.SimpleTestCase.assertRaisesMessage\" title=\"django.test.SimpleTestCase.assertRaisesMessage\"><code class=\"xref py py-meth docutils literal notranslate\">raises a certain exception</code></a>.</p></li>\n<li><p>Checking that a callable <a class=\"reference internal\" href=\"#django.test.SimpleTestCase.assertWarnsMessage\" title=\"django.test.SimpleTestCase.assertWarnsMessage\"><code class=\"xref py py-meth docutils literal notranslate\">triggers a certain warning</code></a>.</p></li>\n<li><p>Testing form field <a class=\"reference internal\" href=\"#django.test.SimpleTestCase.assertFieldOutput\" title=\"django.test.SimpleTestCase.assertFieldOutput\"><code class=\"xref py py-meth docutils literal notranslate\">rendering and error treatment</code></a>.</p></li>\n<li><p>Testing <a class=\"reference internal\" href=\"#django.test.SimpleTestCase.assertContains\" title=\"django.test.SimpleTestCase.assertContains\"><code class=\"xref py py-meth docutils literal notranslate\">HTML responses for the presence/lack of a given fragment</code></a>.</p></li>\n<li><p>Verifying that a template <a class=\"reference internal\" href=\"#django.test.SimpleTestCase.assertTemplateUsed\" title=\"django.test.SimpleTestCase.assertTemplateUsed\"><code class=\"xref py py-meth docutils literal notranslate\">has/hasn't been used to generate a given\nresponse content</code></a>.</p></li>\n<li><p>Verifying that two <a class=\"reference internal\" href=\"#django.test.SimpleTestCase.assertURLEqual\" title=\"django.test.SimpleTestCase.assertURLEqual\"><code class=\"xref py py-meth docutils literal notranslate\">URLs</code></a> are equal.</p></li>\n<li><p>Verifying an HTTP <a class=\"reference internal\" href=\"#django.test.SimpleTestCase.assertRedirects\" title=\"django.test.SimpleTestCase.assertRedirects\"><code class=\"xref py py-meth docutils literal notranslate\">redirect</code></a> is\nperformed by the app.</p></li>\n<li><p>Robustly testing two <a class=\"reference internal\" href=\"#django.test.SimpleTestCase.assertHTMLEqual\" title=\"django.test.SimpleTestCase.assertHTMLEqual\"><code class=\"xref py py-meth docutils literal notranslate\">HTML fragments</code></a>\nfor equality/inequality or <a class=\"reference internal\" href=\"#django.test.SimpleTestCase.assertInHTML\" title=\"django.test.SimpleTestCase.assertInHTML\"><code class=\"xref py py-meth docutils literal notranslate\">containment</code></a>.</p></li>\n<li><p>Robustly testing two <a class=\"reference internal\" href=\"#django.test.SimpleTestCase.assertXMLEqual\" title=\"django.test.SimpleTestCase.assertXMLEqual\"><code class=\"xref py py-meth docutils literal notranslate\">XML fragments</code></a>\nfor equality/inequality.</p></li>\n<li><p>Robustly testing two <a class=\"reference internal\" href=\"#django.test.SimpleTestCase.assertJSONEqual\" title=\"django.test.SimpleTestCase.assertJSONEqual\"><code class=\"xref py py-meth docutils literal notranslate\">JSON fragments</code></a>\nfor equality.</p></li>\n</ul>\n</li>\n<li><p><a class=\"reference internal\" href=\"#overriding-settings\"><span class=\"std std-ref\">編集した設定</span></a> でテストを実行する機能。</p></li>\n<li><p><a class=\"reference internal\" href=\"#django.test.SimpleTestCase.client\" title=\"django.test.SimpleTestCase.client\"><code class=\"xref py py-attr docutils literal notranslate\">client</code></a> <a class=\"reference internal\" href=\"#django.test.Client\" title=\"django.test.Client\"><code class=\"xref py py-class docutils literal notranslate\">Client</code></a> を使う。</p></li>\n</ul>\n<p>データベースクエリを作る手薄との場合、サブクラス <a class=\"reference internal\" href=\"#django.test.TransactionTestCase\" title=\"django.test.TransactionTestCase\"><code class=\"xref py py-class docutils literal notranslate\">TransactionTestCase</code></a> や <a class=\"reference internal\" href=\"#django.test.TestCase\" title=\"django.test.TestCase\"><code class=\"xref py py-class docutils literal notranslate\">TestCase</code></a> を使用してください。</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.databases\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">databases</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.databases\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p><a class=\"reference internal\" href=\"#django.test.SimpleTestCase\" title=\"django.test.SimpleTestCase\"><code class=\"xref py py-class docutils literal notranslate\">SimpleTestCase</code></a> disallows database queries by default. This\nhelps to avoid executing write queries which will affect other tests\nsince each <code class=\"docutils literal notranslate\">SimpleTestCase</code> test isn't run in a transaction. If you\naren't concerned about this problem, you can disable this behavior by\nsetting the <code class=\"docutils literal notranslate\">databases</code> class attribute to <code class=\"docutils literal notranslate\">'__all__'</code> on your test\nclass.</p>\n</dd></dl>\n\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p><code class=\"docutils literal notranslate\">SimpleTestCase</code> とそのサブクラス (例: <code class=\"docutils literal notranslate\">TestCase</code>, ...) は、<code class=\"docutils literal notranslate\">setUpClass()</code> と <code class=\"docutils literal notranslate\">tearDownClass()</code> に依存して、クラス横断的な初期化を実現しています (例: 設定のオーバーライド) これらをオーバーライドする必要があるときは、<code class=\"docutils literal notranslate\">super</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=\"k\">class</span> <span class=\"nc\">MyTestCase</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    <span class=\"nd\">@classmethod</span>\n    <span class=\"k\">def</span> <span class=\"nf\">setUpClass</span><span class=\"p\">(</span><span class=\"bp\">cls</span><span class=\"p\">):</span>\n        <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span>setUpClass<span class=\"p\">()</span>\n        <span class=\"o\">...</span>\n\n    <span class=\"nd\">@classmethod</span>\n    <span class=\"k\">def</span> <span class=\"nf\">tearDownClass</span><span class=\"p\">(</span><span class=\"bp\">cls</span><span class=\"p\">):</span>\n        <span class=\"o\">...</span>\n        <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span>tearDownClass<span class=\"p\">()</span>\n</code></pre></div>\n<p>Be sure to account for Python's behavior if an exception is raised during\n<code class=\"docutils literal notranslate\">setUpClass()</code>. If that happens, neither the tests in the class nor\n<code class=\"docutils literal notranslate\">tearDownClass()</code> are run. In the case of <a class=\"reference internal\" href=\"#django.test.TestCase\" title=\"django.test.TestCase\"><code class=\"xref py py-class docutils literal notranslate\">django.test.TestCase</code></a>,\nthis will leak the transaction created in <code class=\"docutils literal notranslate\">super()</code>  which results in\nvarious symptoms including a segmentation fault on some platforms (reported\non macOS). If you want to intentionally raise an exception such as\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.SkipTest\" title=\"(in Python v3.14)\"><code class=\"xref py py-exc docutils literal notranslate\">unittest.SkipTest</code></a> in <code class=\"docutils literal notranslate\">setUpClass()</code>, be sure to do it before\ncalling <code class=\"docutils literal notranslate\">super()</code> to avoid this.</p>\n</aside>\n</section>\n<section id=\"transactiontestcase\">\n<h3><code class=\"docutils literal notranslate\">TransactionTestCase</code><a class=\"heading-anchor\" href=\"#transactiontestcase\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.test.TransactionTestCase\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">TransactionTestCase</span><a class=\"heading-anchor\" href=\"#django.test.TransactionTestCase\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p><code class=\"docutils literal notranslate\">TransactionTestCase</code> は <a class=\"reference internal\" href=\"#django.test.SimpleTestCase\" title=\"django.test.SimpleTestCase\"><code class=\"xref py py-class docutils literal notranslate\">SimpleTestCase</code></a> を継承し、データベースに特有の機能を追加しています:</p>\n<ul class=\"simple\">\n<li><p>Resetting the database to a known state at the beginning of each test to\nease testing and using the ORM.</p></li>\n<li><p>Database <a class=\"reference internal\" href=\"#django.test.TransactionTestCase.fixtures\" title=\"django.test.TransactionTestCase.fixtures\"><code class=\"xref py py-attr docutils literal notranslate\">fixtures</code></a>.</p></li>\n<li><p>Test <a class=\"reference internal\" href=\"#skipping-tests\"><span class=\"std std-ref\">skipping based on database backend features</span></a>.</p></li>\n<li><p>The remaining specialized <a class=\"reference internal\" href=\"#django.test.TransactionTestCase.assertQuerySetEqual\" title=\"django.test.TransactionTestCase.assertQuerySetEqual\"><code class=\"xref py py-meth docutils literal notranslate\">assert*</code></a> methods.</p></li>\n</ul>\n<p>Django's <a class=\"reference internal\" href=\"#django.test.TestCase\" title=\"django.test.TestCase\"><code class=\"xref py py-class docutils literal notranslate\">TestCase</code></a> class is a more commonly used subclass of\n<code class=\"docutils literal notranslate\">TransactionTestCase</code> that makes use of database transaction facilities\nto speed up the process of resetting the database to a known state at the\nbeginning of each test. A consequence of this, however, is that some database\nbehaviors cannot be tested within a Django <code class=\"docutils literal notranslate\">TestCase</code> class. For instance,\nyou cannot test that a block of code is executing within a transaction, as is\nrequired when using\n<a class=\"reference internal\" href=\"/ja/4.2/ref/models/querysets/#django.db.models.query.QuerySet.select_for_update\" title=\"django.db.models.query.QuerySet.select_for_update\"><code class=\"xref py py-meth docutils literal notranslate\">select_for_update()</code></a>. In those cases,\nyou should use <code class=\"docutils literal notranslate\">TransactionTestCase</code>.</p>\n<p><code class=\"docutils literal notranslate\">TransactionTestCase</code> and <code class=\"docutils literal notranslate\">TestCase</code> are identical except for the manner\nin which the database is reset to a known state and the ability for test code\nto test the effects of commit and rollback:</p>\n<ul class=\"simple\">\n<li><p>A <code class=\"docutils literal notranslate\">TransactionTestCase</code> resets the database after the test runs by\ntruncating all tables. A <code class=\"docutils literal notranslate\">TransactionTestCase</code> may call commit and rollback\nand observe the effects of these calls on the database.</p></li>\n<li><p>A <code class=\"docutils literal notranslate\">TestCase</code>, on the other hand, does not truncate tables after a test.\nInstead, it encloses the test code in a database transaction that is rolled\nback at the end of the test. This guarantees that the rollback at the end of\nthe test restores the database to its initial state.</p></li>\n</ul>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p><code class=\"docutils literal notranslate\">TestCase</code> running on a database that does not support rollback (e.g. MySQL\nwith the MyISAM storage engine), and all instances of <code class=\"docutils literal notranslate\">TransactionTestCase</code>,\nwill roll back at the end of the test by deleting all data from the test\ndatabase.</p>\n<p>Apps <a class=\"reference internal\" href=\"/ja/4.2/topics/testing/overview/#test-case-serialized-rollback\"><span class=\"std std-ref\">will not see their data reloaded</span></a>;\nif you need this functionality (for example, third-party apps should enable\nthis) you can set <code class=\"docutils literal notranslate\">serialized_rollback = True</code> inside the\n<code class=\"docutils literal notranslate\">TestCase</code> body.</p>\n</aside>\n</section>\n<section id=\"testcase\">\n<h3><code class=\"docutils literal notranslate\">TestCase</code><a class=\"heading-anchor\" href=\"#testcase\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.test.TestCase\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">TestCase</span><a class=\"heading-anchor\" href=\"#django.test.TestCase\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>これは、Django でテストを書く際に使われる、最も一般的なクラスです。<a class=\"reference internal\" href=\"#django.test.TransactionTestCase\" title=\"django.test.TransactionTestCase\"><code class=\"xref py py-class docutils literal notranslate\">TransactionTestCase</code></a> (と拡張による <a class=\"reference internal\" href=\"#django.test.SimpleTestCase\" title=\"django.test.SimpleTestCase\"><code class=\"xref py py-class docutils literal notranslate\">SimpleTestCase</code></a>) を継承します。あなたの Django アプリケーションがデータベースを使用しない場合は、<a class=\"reference internal\" href=\"#django.test.SimpleTestCase\" title=\"django.test.SimpleTestCase\"><code class=\"xref py py-class docutils literal notranslate\">SimpleTestCase</code></a> を使ってください。</p>\n<p>クラス:</p>\n<ul class=\"simple\">\n<li><p>2 つのネストされた <a class=\"reference internal\" href=\"/ja/4.2/topics/db/transactions/#django.db.transaction.atomic\" title=\"django.db.transaction.atomic\"><code class=\"xref py py-func docutils literal notranslate\">atomic()</code></a> ブロックでテストをラップします: 1 つはテスト全体、もう 1 つは各テストのためです。したがって、特定のデータベーストランザクションの振る舞いをテストしたい場合は、<a class=\"reference internal\" href=\"#django.test.TransactionTestCase\" title=\"django.test.TransactionTestCase\"><code class=\"xref py py-class docutils literal notranslate\">TransactionTestCase</code></a> を使ってください。</p></li>\n<li><p>各テストの最後に、deferrable なデータベース制約をチェックします。</p></li>\n</ul>\n<p>さらに追加メソッドを提供します:</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.TestCase.setUpTestData\">\n<em class=\"property\"><span class=\"k\">classmethod</span> </em><span class=\"sig-prename descclassname\">TestCase.</span><span class=\"sig-name descname\">setUpTestData</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.TestCase.setUpTestData\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>上述のクラスレベルの <code class=\"docutils literal notranslate\">atomic</code> ブロックは、全体の <code class=\"docutils literal notranslate\">TestCase</code> に対して一度、クラスレベルでの初期データの作成を可能にします。 この技法により、<code class=\"docutils literal notranslate\">setUp()</code> を使うのに比べて高速なテストとなります。</p>\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=\"kn\">from</span> <span class=\"nn\">django.test</span> <span class=\"kn\">import</span> TestCase\n\n\n<span class=\"k\">class</span> <span class=\"nc\">MyTests</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    <span class=\"nd\">@classmethod</span>\n    <span class=\"k\">def</span> <span class=\"nf\">setUpTestData</span><span class=\"p\">(</span><span class=\"bp\">cls</span><span class=\"p\">):</span>\n        <span class=\"c1\"># Set up data for the whole TestCase</span>\n        <span class=\"bp\">cls</span><span class=\"o\">.</span>foo <span class=\"o\">=</span> Foo<span class=\"o\">.</span>objects<span class=\"o\">.</span>create<span class=\"p\">(</span>bar<span class=\"o\">=</span><span class=\"s2\">&quot;Test&quot;</span><span class=\"p\">)</span>\n        <span class=\"o\">...</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">test1</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"c1\"># Some test using self.foo</span>\n        <span class=\"o\">...</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">test2</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"c1\"># Some other test using self.foo</span>\n        <span class=\"o\">...</span>\n</code></pre></div>\n<p>テストがトランザクションサポートのないデータベース (たとえば MyISAM エンジンの MySQL) で実行される場合、各テストの前に <code class=\"docutils literal notranslate\">setUpTestData()</code> が呼ばれ、高速化のメリットはなくなります。</p>\n<p>Objects assigned to class attributes in <code class=\"docutils literal notranslate\">setUpTestData()</code> must support\ncreating deep copies with <a class=\"reference external\" href=\"https://docs.python.org/3/library/copy.html#copy.deepcopy\" title=\"(in Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\">copy.deepcopy()</code></a> in order to isolate them\nfrom alterations performed by each test methods.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.TestCase.captureOnCommitCallbacks\">\n<em class=\"property\"><span class=\"k\">classmethod</span> </em><span class=\"sig-prename descclassname\">TestCase.</span><span class=\"sig-name descname\">captureOnCommitCallbacks</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">using<span class=\"o\">=</span><span class=\"default_value\">DEFAULT_DB_ALIAS</span></em>, <em class=\"sig-param\">execute<span class=\"o\">=</span><span class=\"default_value\">False</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.TestCase.captureOnCommitCallbacks\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns a context manager that captures <a class=\"reference internal\" href=\"/ja/4.2/topics/db/transactions/#django.db.transaction.on_commit\" title=\"django.db.transaction.on_commit\"><code class=\"xref py py-func docutils literal notranslate\">transaction.on_commit()</code></a> callbacks for the given database\nconnection. It returns a list that contains, on exit of the context, the\ncaptured callback functions. From this list you can make assertions on the\ncallbacks or call them to invoke their side effects, emulating a commit.</p>\n<p><code class=\"docutils literal notranslate\">using</code> is the alias of the database connection to capture callbacks for.</p>\n<p>If <code class=\"docutils literal notranslate\">execute</code> is <code class=\"docutils literal notranslate\">True</code>, all the callbacks will be called as the context\nmanager exits, if no exception occurred. This emulates a commit after the\nwrapped block of code.</p>\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=\"kn\">from</span> <span class=\"nn\">django.core</span> <span class=\"kn\">import</span> mail\n<span class=\"kn\">from</span> <span class=\"nn\">django.test</span> <span class=\"kn\">import</span> TestCase\n\n\n<span class=\"k\">class</span> <span class=\"nc\">ContactTests</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_post</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">with</span> <span class=\"bp\">self</span><span class=\"o\">.</span>captureOnCommitCallbacks<span class=\"p\">(</span>execute<span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">)</span> <span class=\"k\">as</span> callbacks<span class=\"p\">:</span>\n            response <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>client<span class=\"o\">.</span>post<span class=\"p\">(</span>\n                <span class=\"s2\">&quot;/contact/&quot;</span><span class=\"p\">,</span>\n                <span class=\"p\">{</span><span class=\"s2\">&quot;message&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;I like your site&quot;</span><span class=\"p\">},</span>\n            <span class=\"p\">)</span>\n\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        <span class=\"bp\">self</span><span class=\"o\">.</span>assertEqual<span class=\"p\">(</span><span class=\"nb\">len</span><span class=\"p\">(</span>callbacks<span class=\"p\">),</span> <span class=\"mi\">1</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>assertEqual<span class=\"p\">(</span><span class=\"nb\">len</span><span class=\"p\">(</span>mail<span class=\"o\">.</span>outbox<span class=\"p\">),</span> <span class=\"mi\">1</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>assertEqual<span class=\"p\">(</span>mail<span class=\"o\">.</span>outbox<span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span><span class=\"o\">.</span>subject<span class=\"p\">,</span> <span class=\"s2\">&quot;Contact Form&quot;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>assertEqual<span class=\"p\">(</span>mail<span class=\"o\">.</span>outbox<span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span><span class=\"o\">.</span>body<span class=\"p\">,</span> <span class=\"s2\">&quot;I like your site&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n</dd></dl>\n\n</section>\n<section id=\"liveservertestcase\">\n<span id=\"live-test-server\"></span><h3><code class=\"docutils literal notranslate\">LiveServerTestCase</code><a class=\"heading-anchor\" href=\"#liveservertestcase\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.test.LiveServerTestCase\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">LiveServerTestCase</span><a class=\"heading-anchor\" href=\"#django.test.LiveServerTestCase\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p><code class=\"docutils literal notranslate\">LiveServerTestCase</code> は <a class=\"reference internal\" href=\"#django.test.TransactionTestCase\" title=\"django.test.TransactionTestCase\"><code class=\"xref py py-class docutils literal notranslate\">TransactionTestCase</code></a> とほぼ同じですが、追加機能を 1 つ持ちます: セットアップ時に実際の Django サーバーをバックグラウンドでローンチし、 終了時に破棄します。これにより、たとえば <a class=\"reference external\" href=\"https://www.selenium.dev/\">Selenium</a> client のような <a class=\"reference internal\" href=\"#test-client\"><span class=\"std std-ref\">Django ダミークライアント</span></a> 以外の自動化されたテストクライアントを使えるようになり、ブラウザ内での一連の機能テストを実施して実際のユーザーの行動をシミュレートできます。</p>\n<p>The live server listens on <code class=\"docutils literal notranslate\">localhost</code> and binds to port 0 which uses a free\nport assigned by the operating system. The server's URL can be accessed with\n<code class=\"docutils literal notranslate\">self.live_server_url</code> during the tests.</p>\n<p>To demonstrate how to use <code class=\"docutils literal notranslate\">LiveServerTestCase</code>, let's write a Selenium test.\nFirst of all, you need to install the <a class=\"extlink-pypi reference external\" href=\"https://pypi.org/project/selenium/\">selenium</a> package:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-0-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>python -m pip install <span class=\"s2\">&quot;selenium &gt;= 3.8.0&quot;</span>\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-0-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> py -m pip install <span class=\"s2\">&quot;selenium &gt;= 3.8.0&quot;</span>\n</code></pre></div></div></div>\n<p>Then, add a <code class=\"docutils literal notranslate\">LiveServerTestCase</code>-based test to your app's tests module\n(for example: <code class=\"docutils literal notranslate\">myapp/tests.py</code>). For this example, we'll assume you're using\nthe <a class=\"reference internal\" href=\"/ja/4.2/ref/contrib/staticfiles/#module-django.contrib.staticfiles\" title=\"django.contrib.staticfiles: An app for handling static files.\"><code class=\"xref py py-mod docutils literal notranslate\">staticfiles</code></a> app and want to have static files served\nduring the execution of your tests similar to what we get at development time\nwith <code class=\"docutils literal notranslate\">DEBUG=True</code>, i.e. without having to collect them using\n<a class=\"reference internal\" href=\"/ja/4.2/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\">collectstatic</code></a>. We'll use\nthe  <a class=\"reference internal\" href=\"/ja/4.2/ref/contrib/staticfiles/#django.contrib.staticfiles.testing.StaticLiveServerTestCase\" title=\"django.contrib.staticfiles.testing.StaticLiveServerTestCase\"><code class=\"xref py py-class docutils literal notranslate\">StaticLiveServerTestCase</code></a>\nsubclass which provides that functionality. Replace it with\n<code class=\"docutils literal notranslate\">django.test.LiveServerTestCase</code> if you don't need that.</p>\n<p>The code for this test may look 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.contrib.staticfiles.testing</span> <span class=\"kn\">import</span> StaticLiveServerTestCase\n<span class=\"kn\">from</span> <span class=\"nn\">selenium.webdriver.common.by</span> <span class=\"kn\">import</span> By\n<span class=\"kn\">from</span> <span class=\"nn\">selenium.webdriver.firefox.webdriver</span> <span class=\"kn\">import</span> WebDriver\n\n\n<span class=\"k\">class</span> <span class=\"nc\">MySeleniumTests</span><span class=\"p\">(</span>StaticLiveServerTestCase<span class=\"p\">):</span>\n    fixtures <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s2\">&quot;user-data.json&quot;</span><span class=\"p\">]</span>\n\n    <span class=\"nd\">@classmethod</span>\n    <span class=\"k\">def</span> <span class=\"nf\">setUpClass</span><span class=\"p\">(</span><span class=\"bp\">cls</span><span class=\"p\">):</span>\n        <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span>setUpClass<span class=\"p\">()</span>\n        <span class=\"bp\">cls</span><span class=\"o\">.</span>selenium <span class=\"o\">=</span> WebDriver<span class=\"p\">()</span>\n        <span class=\"bp\">cls</span><span class=\"o\">.</span>selenium<span class=\"o\">.</span>implicitly_wait<span class=\"p\">(</span><span class=\"mi\">10</span><span class=\"p\">)</span>\n\n    <span class=\"nd\">@classmethod</span>\n    <span class=\"k\">def</span> <span class=\"nf\">tearDownClass</span><span class=\"p\">(</span><span class=\"bp\">cls</span><span class=\"p\">):</span>\n        <span class=\"bp\">cls</span><span class=\"o\">.</span>selenium<span class=\"o\">.</span>quit<span class=\"p\">()</span>\n        <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span>tearDownClass<span class=\"p\">()</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">test_login</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>selenium<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"sa\">f</span><span class=\"s2\">&quot;</span><span class=\"si\">{</span><span class=\"bp\">self</span><span class=\"o\">.</span>live_server_url<span class=\"si\">}</span><span class=\"s2\">/login/&quot;</span><span class=\"p\">)</span>\n        username_input <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>selenium<span class=\"o\">.</span>find_element<span class=\"p\">(</span>By<span class=\"o\">.</span>NAME<span class=\"p\">,</span> <span class=\"s2\">&quot;username&quot;</span><span class=\"p\">)</span>\n        username_input<span class=\"o\">.</span>send_keys<span class=\"p\">(</span><span class=\"s2\">&quot;myuser&quot;</span><span class=\"p\">)</span>\n        password_input <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>selenium<span class=\"o\">.</span>find_element<span class=\"p\">(</span>By<span class=\"o\">.</span>NAME<span class=\"p\">,</span> <span class=\"s2\">&quot;password&quot;</span><span class=\"p\">)</span>\n        password_input<span class=\"o\">.</span>send_keys<span class=\"p\">(</span><span class=\"s2\">&quot;secret&quot;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>selenium<span class=\"o\">.</span>find_element<span class=\"p\">(</span>By<span class=\"o\">.</span>XPATH<span class=\"p\">,</span> <span class=\"s1\">&#39;//input[@value=&quot;Log in&quot;]&#39;</span><span class=\"p\">)</span><span class=\"o\">.</span>click<span class=\"p\">()</span>\n</code></pre></div>\n<p>Finally, you may run the test as follows:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-1-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./manage.py <span class=\"nb\">test</span> myapp.tests.MySeleniumTests.test_login\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-1-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> manage.py test myapp.tests.MySeleniumTests.test_login\n</code></pre></div></div></div>\n<p>This example will automatically open Firefox then go to the login page, enter\nthe credentials and press the &quot;Log in&quot; button. Selenium offers other drivers in\ncase you do not have Firefox installed or wish to use another browser. The\nexample above is just a tiny fraction of what the Selenium client can do; check\nout the <a class=\"reference external\" href=\"https://selenium-python.readthedocs.io/api.html\">full reference</a> for more details.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>When using an in-memory SQLite database to run the tests, the same database\nconnection will be shared by two threads in parallel: the thread in which\nthe live server is run and the thread in which the test case is run. It's\nimportant to prevent simultaneous database queries via this shared\nconnection by the two threads, as that may sometimes randomly cause the\ntests to fail. So you need to ensure that the two threads don't access the\ndatabase at the same time. In particular, this means that in some cases\n(for example, just after clicking a link or submitting a form), you might\nneed to check that a response is received by Selenium and that the next\npage is loaded before proceeding with further test execution.\nDo this, for example, by making Selenium wait until the <code class=\"docutils literal notranslate\">&lt;body&gt;</code> HTML tag\nis found in the response (requires Selenium &gt; 2.13):</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\">def</span> <span class=\"nf\">test_login</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n    <span class=\"kn\">from</span> <span class=\"nn\">selenium.webdriver.support.wait</span> <span class=\"kn\">import</span> WebDriverWait\n\n    timeout <span class=\"o\">=</span> <span class=\"mi\">2</span>\n    <span class=\"o\">...</span>\n    <span class=\"bp\">self</span><span class=\"o\">.</span>selenium<span class=\"o\">.</span>find_element<span class=\"p\">(</span>By<span class=\"o\">.</span>XPATH<span class=\"p\">,</span> <span class=\"s1\">&#39;//input[@value=&quot;Log in&quot;]&#39;</span><span class=\"p\">)</span><span class=\"o\">.</span>click<span class=\"p\">()</span>\n    <span class=\"c1\"># Wait until the response is received</span>\n    WebDriverWait<span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span>selenium<span class=\"p\">,</span> timeout<span class=\"p\">)</span><span class=\"o\">.</span>until<span class=\"p\">(</span>\n        <span class=\"k\">lambda</span> driver<span class=\"p\">:</span> driver<span class=\"o\">.</span>find_element<span class=\"p\">(</span>By<span class=\"o\">.</span>TAG_NAME<span class=\"p\">,</span> <span class=\"s2\">&quot;body&quot;</span><span class=\"p\">)</span>\n    <span class=\"p\">)</span>\n</code></pre></div>\n<p>The tricky thing here is that there's really no such thing as a &quot;page load,&quot;\nespecially in modern web apps that generate HTML dynamically after the\nserver generates the initial document. So, checking for the presence of\n<code class=\"docutils literal notranslate\">&lt;body&gt;</code> in the response might not necessarily be appropriate for all use\ncases. Please refer to the <a class=\"reference external\" href=\"https://web.archive.org/web/20160129132110/http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions#Q:_WebDriver_fails_to_find_elements_/_Does_not_block_on_page_loa\">Selenium FAQ</a> and <a class=\"reference external\" href=\"https://www.selenium.dev/documentation/webdriver/waits/#explicit-wait\">Selenium documentation</a>\nfor more information.</p>\n</aside>\n</section>\n</section>\n<section id=\"test-cases-features\">\n<h2>テストケースの機能<a class=\"heading-anchor\" href=\"#test-cases-features\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"default-test-client\">\n<h3>デフォルトのテストクライアント<a class=\"heading-anchor\" href=\"#default-test-client\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.client\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">client</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.client\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p><code class=\"docutils literal notranslate\">django.test.*TestCase</code> インスタンス内のすべてのテストケースは、Django のテストクライアントにアクセスすることができます。このクライアントは、<code class=\"docutils literal notranslate\">self.client</code> としてアクセスできます。このクライアントはテストごとに再作成されるため、(クッキーのような) ステートがテスト間で持ち越されることを心配する必要はありません。</p>\n<p>つまり、各テスト内で <code class=\"docutils literal notranslate\">Client</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=\"kn\">import</span> <span class=\"nn\">unittest</span>\n<span class=\"kn\">from</span> <span class=\"nn\">django.test</span> <span class=\"kn\">import</span> Client\n\n\n<span class=\"k\">class</span> <span class=\"nc\">SimpleTest</span><span class=\"p\">(</span>unittest<span class=\"o\">.</span>TestCase<span class=\"p\">):</span>\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        client <span class=\"o\">=</span> Client<span class=\"p\">()</span>\n        response <span class=\"o\">=</span> client<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s2\">&quot;/customer/details/&quot;</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\n    <span class=\"k\">def</span> <span class=\"nf\">test_index</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        client <span class=\"o\">=</span> Client<span class=\"p\">()</span>\n        response <span class=\"o\">=</span> client<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s2\">&quot;/customer/index/&quot;</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>次のように <code class=\"docutils literal notranslate\">self.client</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=\"kn\">from</span> <span class=\"nn\">django.test</span> <span class=\"kn\">import</span> TestCase\n\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\">test_details</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=\"s2\">&quot;/customer/details/&quot;</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\n    <span class=\"k\">def</span> <span class=\"nf\">test_index</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=\"s2\">&quot;/customer/index/&quot;</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</section>\n<section id=\"customizing-the-test-client\">\n<h3>テストクライアントをカスタムする<a class=\"heading-anchor\" href=\"#customizing-the-test-client\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.client_class\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">client_class</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.client_class\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>異なる <code class=\"docutils literal notranslate\">Client</code> クラスを使用したい場合 (たとえば独自の動作を持つサブクラス)、<a class=\"reference internal\" href=\"#django.test.SimpleTestCase.client_class\" title=\"django.test.SimpleTestCase.client_class\"><code class=\"xref py py-attr docutils literal notranslate\">client_class</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=\"nn\">django.test</span> <span class=\"kn\">import</span> Client<span class=\"p\">,</span> TestCase\n\n\n<span class=\"k\">class</span> <span class=\"nc\">MyTestClient</span><span class=\"p\">(</span>Client<span class=\"p\">):</span>\n    <span class=\"c1\"># Specialized methods for your environment</span>\n    <span class=\"o\">...</span>\n\n\n<span class=\"k\">class</span> <span class=\"nc\">MyTest</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    client_class <span class=\"o\">=</span> MyTestClient\n\n    <span class=\"k\">def</span> <span class=\"nf\">test_my_stuff</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"c1\"># Here self.client is an instance of MyTestClient...</span>\n        call_some_test_code<span class=\"p\">()</span>\n</code></pre></div>\n</section>\n<section id=\"fixture-loading\">\n<span id=\"topics-testing-fixtures\"></span><h3>フィクスチャーのロード<a class=\"heading-anchor\" href=\"#fixture-loading\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.test.TransactionTestCase.fixtures\">\n<span class=\"sig-prename descclassname\">TransactionTestCase.</span><span class=\"sig-name descname\">fixtures</span><a class=\"heading-anchor\" href=\"#django.test.TransactionTestCase.fixtures\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>データベースに基づくウェブサイトに対するテストケースは、データベースにデータがないとほとんど意味がありません。テストは、たとえば <a class=\"reference internal\" href=\"#django.test.TestCase.setUpTestData\" title=\"django.test.TestCase.setUpTestData\"><code class=\"xref py py-meth docutils literal notranslate\">TestCase.setUpTestData()</code></a> 内の ORM を使うことでより可読性が高く維持しやすくなりますが、<a class=\"reference internal\" href=\"/ja/4.2/topics/db/fixtures/#fixtures-explanation\"><span class=\"std std-ref\">フィクスチャー</span></a> を使うこともできます。</p>\n<p>フィクスチャーは、Django がどうやってデータベースにインポートするか分かっているデータのコレクションです。たとえば、サイトがユーザーアカウントを持つ場合、テスト内でデータベースに格納させるために、フェイクのユーザーアカウントのフィクスチャーをセットアップできます。</p>\n<p>フィクスチャーを作成するもっとも分かりやすい方法は、<a class=\"reference internal\" href=\"/ja/4.2/ref/django-admin/#django-admin-dumpdata\"><code class=\"xref std std-djadmin docutils literal notranslate\">manage.py dumpdata</code></a> コマンドを使うことです。これは、データベース内にすでに何らかのデータがあるという仮定に基づいています。詳細は、<a class=\"reference internal\" href=\"/ja/4.2/ref/django-admin/#django-admin-dumpdata\"><code class=\"xref std std-djadmin docutils literal notranslate\">dumpdata のドキュメント</code></a> 参照してください。</p>\n<p>いったんフィクスチャーを作成して <a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a> 内ののどれかで <code class=\"docutils literal notranslate\">fixtures</code> ディクショナリに記述したら、<a class=\"reference internal\" href=\"#django.test.TestCase\" title=\"django.test.TestCase\"><code class=\"xref py py-class docutils literal notranslate\">django.test.TestCase</code></a> サブクラスで <code class=\"docutils literal notranslate\">fixtures</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=\"kn\">from</span> <span class=\"nn\">django.test</span> <span class=\"kn\">import</span> TestCase\n<span class=\"kn\">from</span> <span class=\"nn\">myapp.models</span> <span class=\"kn\">import</span> Animal\n\n\n<span class=\"k\">class</span> <span class=\"nc\">AnimalTestCase</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    fixtures <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s2\">&quot;mammals.json&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;birds&quot;</span><span class=\"p\">]</span>\n\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\"># Test definitions as before.</span>\n        call_setup_methods<span class=\"p\">()</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">test_fluffy_animals</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"c1\"># A test that uses the fixtures.</span>\n        call_some_test_code<span class=\"p\">()</span>\n</code></pre></div>\n<p>Here's specifically what will happen:</p>\n<ul class=\"simple\">\n<li><p>At the start of each test, before <code class=\"docutils literal notranslate\">setUp()</code> is run, Django will flush the\ndatabase, returning the database to the state it was in directly after\n<a class=\"reference internal\" href=\"/ja/4.2/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\">migrate</code></a> was called.</p></li>\n<li><p>Then, all the named fixtures are installed. In this example, Django will\ninstall any JSON fixture named <code class=\"docutils literal notranslate\">mammals</code>, followed by any fixture named\n<code class=\"docutils literal notranslate\">birds</code>. See the <a class=\"reference internal\" href=\"/ja/4.2/topics/db/fixtures/#fixtures-explanation\"><span class=\"std std-ref\">フィクスチャー</span></a> topic for more details on\ndefining and installing fixtures.</p></li>\n</ul>\n<p>For performance reasons, <a class=\"reference internal\" href=\"#django.test.TestCase\" title=\"django.test.TestCase\"><code class=\"xref py py-class docutils literal notranslate\">TestCase</code></a> loads fixtures once for the entire\ntest class, before <a class=\"reference internal\" href=\"#django.test.TestCase.setUpTestData\" title=\"django.test.TestCase.setUpTestData\"><code class=\"xref py py-meth docutils literal notranslate\">setUpTestData()</code></a>, instead of before each\ntest, and it uses transactions to clean the database before each test. In any case,\nyou can be certain that the outcome of a test will not be affected by another\ntest or by the order of test execution.</p>\n<p>By default, fixtures are only loaded into the <code class=\"docutils literal notranslate\">default</code> database. If you are\nusing multiple databases and set <a class=\"reference internal\" href=\"#django.test.TransactionTestCase.databases\" title=\"django.test.TransactionTestCase.databases\"><code class=\"xref py py-attr docutils literal notranslate\">TransactionTestCase.databases</code></a>,\nfixtures will be loaded into all specified databases.</p>\n</section>\n<section id=\"urlconf-configuration\">\n<h3>URLconf の設定<a class=\"heading-anchor\" href=\"#urlconf-configuration\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If your application provides views, you may want to include tests that use the\ntest client to exercise those views. However, an end user is free to deploy the\nviews in your application at any URL of their choosing. This means that your\ntests can't rely upon the fact that your views will be available at a\nparticular URL. Decorate your test class or test method with\n<code class=\"docutils literal notranslate\">&#64;override_settings(ROOT_URLCONF=...)</code> for URLconf configuration.</p>\n</section>\n<section id=\"multi-database-support\">\n<span id=\"testing-multi-db\"></span><h3>マルチデータベースのサポート<a class=\"heading-anchor\" href=\"#multi-database-support\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.test.TransactionTestCase.databases\">\n<span class=\"sig-prename descclassname\">TransactionTestCase.</span><span class=\"sig-name descname\">databases</span><a class=\"heading-anchor\" href=\"#django.test.TransactionTestCase.databases\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Django sets up a test database corresponding to every database that is\ndefined in the <a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\">DATABASES</code></a> definition in your settings and referred to\nby at least one test through <code class=\"docutils literal notranslate\">databases</code>.</p>\n<p>However, a big part of the time taken to run a Django <code class=\"docutils literal notranslate\">TestCase</code> is consumed\nby the call to <code class=\"docutils literal notranslate\">flush</code> that ensures that you have a clean database at the\nstart of each test run. If you have multiple databases, multiple flushes are\nrequired (one for each database), which can be a time consuming activity --\nespecially if your tests don't need to test multi-database activity.</p>\n<p>As an optimization, Django only flushes the <code class=\"docutils literal notranslate\">default</code> database at\nthe start of each test run. If your setup contains multiple databases,\nand you have a test that requires every database to be clean, you can\nuse the <code class=\"docutils literal notranslate\">databases</code> attribute on the test suite to request extra databases\nto be flushed.</p>\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=\"k\">class</span> <span class=\"nc\">TestMyViews</span><span class=\"p\">(</span>TransactionTestCase<span class=\"p\">):</span>\n    databases <span class=\"o\">=</span> <span class=\"p\">{</span><span class=\"s2\">&quot;default&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;other&quot;</span><span class=\"p\">}</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">test_index_page_view</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        call_some_test_code<span class=\"p\">()</span>\n</code></pre></div>\n<p>This test case will flush the <code class=\"docutils literal notranslate\">default</code> and <code class=\"docutils literal notranslate\">other</code> test databases before\nrunning <code class=\"docutils literal notranslate\">test_index_page_view</code>. You can also use <code class=\"docutils literal notranslate\">'__all__'</code> to specify\nthat all of the test databases must be flushed.</p>\n<p>The <code class=\"docutils literal notranslate\">databases</code> flag also controls which databases the\n<a class=\"reference internal\" href=\"#django.test.TransactionTestCase.fixtures\" title=\"django.test.TransactionTestCase.fixtures\"><code class=\"xref py py-attr docutils literal notranslate\">TransactionTestCase.fixtures</code></a> are loaded into. By default, fixtures are\nonly loaded into the <code class=\"docutils literal notranslate\">default</code> database.</p>\n<p>Queries against databases not in <code class=\"docutils literal notranslate\">databases</code> will give assertion errors to\nprevent state leaking between tests.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.test.TestCase.databases\">\n<span class=\"sig-prename descclassname\">TestCase.</span><span class=\"sig-name descname\">databases</span><a class=\"heading-anchor\" href=\"#django.test.TestCase.databases\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>By default, only the <code class=\"docutils literal notranslate\">default</code> database will be wrapped in a transaction\nduring a <code class=\"docutils literal notranslate\">TestCase</code>'s execution and attempts to query other databases will\nresult in assertion errors to prevent state leaking between tests.</p>\n<p>Use the <code class=\"docutils literal notranslate\">databases</code> class attribute on the test class to request transaction\nwrapping against non-<code class=\"docutils literal notranslate\">default</code> databases.</p>\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=\"k\">class</span> <span class=\"nc\">OtherDBTests</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    databases <span class=\"o\">=</span> <span class=\"p\">{</span><span class=\"s2\">&quot;other&quot;</span><span class=\"p\">}</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">test_other_db_query</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"o\">...</span>\n</code></pre></div>\n<p>This test will only allow queries against the <code class=\"docutils literal notranslate\">other</code> database. Just like for\n<a class=\"reference internal\" href=\"#django.test.SimpleTestCase.databases\" title=\"django.test.SimpleTestCase.databases\"><code class=\"xref py py-attr docutils literal notranslate\">SimpleTestCase.databases</code></a> and <a class=\"reference internal\" href=\"#django.test.TransactionTestCase.databases\" title=\"django.test.TransactionTestCase.databases\"><code class=\"xref py py-attr docutils literal notranslate\">TransactionTestCase.databases</code></a>, the\n<code class=\"docutils literal notranslate\">'__all__'</code> constant can be used to specify that the test should allow\nqueries to all databases.</p>\n</section>\n<section id=\"overriding-settings\">\n<span id=\"id1\"></span><h3>設定のオーバーライド<a class=\"heading-anchor\" href=\"#overriding-settings\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p>Use the functions below to temporarily alter the value of settings in tests.\nDon't manipulate <code class=\"docutils literal notranslate\">django.conf.settings</code> directly as Django won't restore\nthe original values after such manipulations.</p>\n</aside>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.settings\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">settings</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.settings\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>For testing purposes it's often useful to change a setting temporarily and\nrevert to the original value after running the testing code. For this use case\nDjango provides a standard Python context manager (see <span class=\"target\" id=\"index-3\"></span><a class=\"pep reference external\" href=\"https://peps.python.org/pep-0343/\"><strong>PEP 343</strong></a>) called\n<a class=\"reference internal\" href=\"#django.test.SimpleTestCase.settings\" title=\"django.test.SimpleTestCase.settings\"><code class=\"xref py py-meth docutils literal notranslate\">settings()</code></a>, which can be used like this:</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\n<span class=\"k\">class</span> <span class=\"nc\">LoginTestCase</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_login</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"c1\"># First check for the default behavior</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=\"s2\">&quot;/sekrit/&quot;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>assertRedirects<span class=\"p\">(</span>response<span class=\"p\">,</span> <span class=\"s2\">&quot;/accounts/login/?next=/sekrit/&quot;</span><span class=\"p\">)</span>\n\n        <span class=\"c1\"># Then override the LOGIN_URL setting</span>\n        <span class=\"k\">with</span> <span class=\"bp\">self</span><span class=\"o\">.</span>settings<span class=\"p\">(</span>LOGIN_URL<span class=\"o\">=</span><span class=\"s2\">&quot;/other/login/&quot;</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=\"s2\">&quot;/sekrit/&quot;</span><span class=\"p\">)</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span>assertRedirects<span class=\"p\">(</span>response<span class=\"p\">,</span> <span class=\"s2\">&quot;/other/login/?next=/sekrit/&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>This example will override the <a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-LOGIN_URL\"><code class=\"xref std std-setting docutils literal notranslate\">LOGIN_URL</code></a> setting for the code\nin the <code class=\"docutils literal notranslate\">with</code> block and reset its value to the previous state afterward.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.modify_settings\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">modify_settings</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.modify_settings\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>It can prove unwieldy to redefine settings that contain a list of values. In\npractice, adding or removing values is often sufficient. Django provides the\n<a class=\"reference internal\" href=\"#django.test.SimpleTestCase.modify_settings\" title=\"django.test.SimpleTestCase.modify_settings\"><code class=\"xref py py-meth docutils literal notranslate\">modify_settings()</code></a> context manager for easier\nsettings changes:</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\n<span class=\"k\">class</span> <span class=\"nc\">MiddlewareTestCase</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_cache_middleware</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">with</span> <span class=\"bp\">self</span><span class=\"o\">.</span>modify_settings<span class=\"p\">(</span>\n            MIDDLEWARE<span class=\"o\">=</span><span class=\"p\">{</span>\n                <span class=\"s2\">&quot;append&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.middleware.cache.FetchFromCacheMiddleware&quot;</span><span class=\"p\">,</span>\n                <span class=\"s2\">&quot;prepend&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.middleware.cache.UpdateCacheMiddleware&quot;</span><span class=\"p\">,</span>\n                <span class=\"s2\">&quot;remove&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span>\n                    <span class=\"s2\">&quot;django.contrib.sessions.middleware.SessionMiddleware&quot;</span><span class=\"p\">,</span>\n                    <span class=\"s2\">&quot;django.contrib.auth.middleware.AuthenticationMiddleware&quot;</span><span class=\"p\">,</span>\n                    <span class=\"s2\">&quot;django.contrib.messages.middleware.MessageMiddleware&quot;</span><span class=\"p\">,</span>\n                <span class=\"p\">],</span>\n            <span class=\"p\">}</span>\n        <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=\"s2\">&quot;/&quot;</span><span class=\"p\">)</span>\n            <span class=\"c1\"># ...</span>\n</code></pre></div>\n<p>For each action, you can supply either a list of values or a string. When the\nvalue already exists in the list, <code class=\"docutils literal notranslate\">append</code> and <code class=\"docutils literal notranslate\">prepend</code> have no effect;\nneither does <code class=\"docutils literal notranslate\">remove</code> when the value doesn't exist.</p>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.test.override_settings\">\n<span class=\"sig-name descname\">override_settings</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.override_settings\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>In case you want to override a setting for a test method, Django provides the\n<a class=\"reference internal\" href=\"#django.test.override_settings\" title=\"django.test.override_settings\"><code class=\"xref py py-func docutils literal notranslate\">override_settings()</code></a> decorator (see <span class=\"target\" id=\"index-4\"></span><a class=\"pep reference external\" href=\"https://peps.python.org/pep-0318/\"><strong>PEP 318</strong></a>). It's used\nlike this:</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\n<span class=\"k\">class</span> <span class=\"nc\">LoginTestCase</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    <span class=\"nd\">@override_settings</span><span class=\"p\">(</span>LOGIN_URL<span class=\"o\">=</span><span class=\"s2\">&quot;/other/login/&quot;</span><span class=\"p\">)</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_login</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=\"s2\">&quot;/sekrit/&quot;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>assertRedirects<span class=\"p\">(</span>response<span class=\"p\">,</span> <span class=\"s2\">&quot;/other/login/?next=/sekrit/&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>The decorator can also be applied to <a class=\"reference internal\" href=\"#django.test.TestCase\" title=\"django.test.TestCase\"><code class=\"xref py py-class docutils literal notranslate\">TestCase</code></a> classes:</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\n<span class=\"nd\">@override_settings</span><span class=\"p\">(</span>LOGIN_URL<span class=\"o\">=</span><span class=\"s2\">&quot;/other/login/&quot;</span><span class=\"p\">)</span>\n<span class=\"k\">class</span> <span class=\"nc\">LoginTestCase</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_login</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=\"s2\">&quot;/sekrit/&quot;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>assertRedirects<span class=\"p\">(</span>response<span class=\"p\">,</span> <span class=\"s2\">&quot;/other/login/?next=/sekrit/&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.test.modify_settings\">\n<span class=\"sig-name descname\">modify_settings</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">*</span>args</em>, <em class=\"sig-param\"><span class=\"o\">**</span>kwargs</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.modify_settings\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Likewise, Django provides the <a class=\"reference internal\" href=\"#django.test.modify_settings\" title=\"django.test.modify_settings\"><code class=\"xref py py-func docutils literal notranslate\">modify_settings()</code></a>\ndecorator:</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> modify_settings\n\n\n<span class=\"k\">class</span> <span class=\"nc\">MiddlewareTestCase</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    <span class=\"nd\">@modify_settings</span><span class=\"p\">(</span>\n        MIDDLEWARE<span class=\"o\">=</span><span class=\"p\">{</span>\n            <span class=\"s2\">&quot;append&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.middleware.cache.FetchFromCacheMiddleware&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;prepend&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.middleware.cache.UpdateCacheMiddleware&quot;</span><span class=\"p\">,</span>\n        <span class=\"p\">}</span>\n    <span class=\"p\">)</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_cache_middleware</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=\"s2\">&quot;/&quot;</span><span class=\"p\">)</span>\n        <span class=\"c1\"># ...</span>\n</code></pre></div>\n<p>The decorator can also be applied to test case classes:</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> modify_settings\n\n\n<span class=\"nd\">@modify_settings</span><span class=\"p\">(</span>\n    MIDDLEWARE<span class=\"o\">=</span><span class=\"p\">{</span>\n        <span class=\"s2\">&quot;append&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.middleware.cache.FetchFromCacheMiddleware&quot;</span><span class=\"p\">,</span>\n        <span class=\"s2\">&quot;prepend&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.middleware.cache.UpdateCacheMiddleware&quot;</span><span class=\"p\">,</span>\n    <span class=\"p\">}</span>\n<span class=\"p\">)</span>\n<span class=\"k\">class</span> <span class=\"nc\">MiddlewareTestCase</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_cache_middleware</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=\"s2\">&quot;/&quot;</span><span class=\"p\">)</span>\n        <span class=\"c1\"># ...</span>\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>When given a class, these decorators modify the class directly and return\nit; they don't create and return a modified copy of it. So if you try to\ntweak the above examples to assign the return value to a different name\nthan <code class=\"docutils literal notranslate\">LoginTestCase</code> or <code class=\"docutils literal notranslate\">MiddlewareTestCase</code>, you may be surprised to\nfind that the original test case classes are still equally affected by the\ndecorator. For a given class, <a class=\"reference internal\" href=\"#django.test.modify_settings\" title=\"django.test.modify_settings\"><code class=\"xref py py-func docutils literal notranslate\">modify_settings()</code></a> is\nalways applied after <a class=\"reference internal\" href=\"#django.test.override_settings\" title=\"django.test.override_settings\"><code class=\"xref py py-func docutils literal notranslate\">override_settings()</code></a>.</p>\n</aside>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p>The settings file contains some settings that are only consulted during\ninitialization of Django internals. If you change them with\n<code class=\"docutils literal notranslate\">override_settings</code>, the setting is changed if you access it via the\n<code class=\"docutils literal notranslate\">django.conf.settings</code> module, however, Django's internals access it\ndifferently. Effectively, using <a class=\"reference internal\" href=\"#django.test.override_settings\" title=\"django.test.override_settings\"><code class=\"xref py py-func docutils literal notranslate\">override_settings()</code></a> or\n<a class=\"reference internal\" href=\"#django.test.modify_settings\" title=\"django.test.modify_settings\"><code class=\"xref py py-func docutils literal notranslate\">modify_settings()</code></a> with these settings is probably not\ngoing to do what you expect it to do.</p>\n<p>We do not recommend altering the <a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\">DATABASES</code></a> setting. Altering\nthe <a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-CACHES\"><code class=\"xref std std-setting docutils literal notranslate\">CACHES</code></a> setting is possible, but a bit tricky if you are\nusing internals that make using of caching, like\n<a class=\"reference internal\" href=\"/ja/4.2/topics/http/sessions/#module-django.contrib.sessions\" title=\"django.contrib.sessions: Provides session management for Django projects.\"><code class=\"xref py py-mod docutils literal notranslate\">django.contrib.sessions</code></a>. For example, you will have to reinitialize\nthe session backend in a test that uses cached sessions and overrides\n<a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-CACHES\"><code class=\"xref std std-setting docutils literal notranslate\">CACHES</code></a>.</p>\n<p>Finally, avoid aliasing your settings as module-level constants as\n<code class=\"docutils literal notranslate\">override_settings()</code> won't work on such values since they are\nonly evaluated the first time the module is imported.</p>\n</aside>\n<p>You can also simulate the absence of a setting by deleting it after settings\nhave been overridden, like this:</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=\"nd\">@override_settings</span><span class=\"p\">()</span>\n<span class=\"k\">def</span> <span class=\"nf\">test_something</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n    <span class=\"k\">del</span> settings<span class=\"o\">.</span>LOGIN_URL\n    <span class=\"o\">...</span>\n</code></pre></div>\n<p>When overriding settings, make sure to handle the cases in which your app's\ncode uses a cache or similar feature that retains state even if the setting is\nchanged. Django provides the <a class=\"reference internal\" href=\"/ja/4.2/ref/signals/#django.test.signals.setting_changed\" title=\"django.test.signals.setting_changed\"><code class=\"xref py py-data docutils literal notranslate\">django.test.signals.setting_changed</code></a>\nsignal that lets you register callbacks to clean up and otherwise reset state\nwhen settings are changed.</p>\n<p>Django itself uses this signal to reset various data:</p>\n<div class=\"table-scroll\" role=\"region\" tabindex=\"0\" aria-label=\"Table\"><table class=\"docutils align-default\">\n<thead>\n<tr class=\"row-odd\"><th class=\"head\"><p>オーバーライドされた設定</p></th>\n<th class=\"head\"><p>データのリセット</p></th>\n</tr>\n</thead>\n<tbody>\n<tr class=\"row-even\"><td><p>USE_TZ, TIME_ZONE</p></td>\n<td><p>データベースのタイムゾーン</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>TEMPLATES</p></td>\n<td><p>テンプレートエンジン</p></td>\n</tr>\n<tr class=\"row-even\"><td><p>SERIALIZATION_MODULES</p></td>\n<td><p>シリアライザーのキャッシュ</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>LOCALE_PATHS、LANGUAGE_CODE</p></td>\n<td><p>デフォルトの翻訳と読み込まれた翻訳</p></td>\n</tr>\n<tr class=\"row-even\"><td><p>DEFAULT_FILE_STORAGE、STATICFILES_STORAGE、STATIC_ROOT、STATIC_URL、STORAGES</p></td>\n<td><p>ストレージの設定</p></td>\n</tr>\n</tbody>\n</table>\n</div>\n</section>\n<section id=\"isolating-apps\">\n<h3>アプリの隔離<a class=\"heading-anchor\" href=\"#isolating-apps\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.test.utils.isolate_apps\">\n<span class=\"sig-prename descclassname\">utils.</span><span class=\"sig-name descname\">isolate_apps</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">*</span>app_labels</em>, <em class=\"sig-param\">attr_name<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">kwarg_name<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.utils.isolate_apps\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Registers the models defined within a wrapped context into their own\nisolated <a class=\"reference internal\" href=\"/ja/4.2/ref/applications/#django.apps.apps\" title=\"django.apps.apps\"><code class=\"xref py py-attr docutils literal notranslate\">apps</code></a> registry. This functionality is useful\nwhen creating model classes for tests, as the classes will be cleanly\ndeleted afterward, and there is no risk of name collisions.</p>\n<p>The app labels which the isolated registry should contain must be passed as\nindividual arguments. You can use <code class=\"docutils literal notranslate\">isolate_apps()</code> as a decorator or a\ncontext manager. For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n<span class=\"kn\">from</span> <span class=\"nn\">django.test</span> <span class=\"kn\">import</span> SimpleTestCase\n<span class=\"kn\">from</span> <span class=\"nn\">django.test.utils</span> <span class=\"kn\">import</span> isolate_apps\n\n\n<span class=\"k\">class</span> <span class=\"nc\">MyModelTests</span><span class=\"p\">(</span>SimpleTestCase<span class=\"p\">):</span>\n    <span class=\"nd\">@isolate_apps</span><span class=\"p\">(</span><span class=\"s2\">&quot;app_label&quot;</span><span class=\"p\">)</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_model_definition</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">class</span> <span class=\"nc\">TestModel</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n            <span class=\"k\">pass</span>\n\n        <span class=\"o\">...</span>\n</code></pre></div>\n<p>… or:</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\">with</span> isolate_apps<span class=\"p\">(</span><span class=\"s2\">&quot;app_label&quot;</span><span class=\"p\">):</span>\n\n    <span class=\"k\">class</span> <span class=\"nc\">TestModel</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n        <span class=\"k\">pass</span>\n\n    <span class=\"o\">...</span>\n</code></pre></div>\n<p>The decorator form can also be applied to classes.</p>\n<p>Two optional keyword arguments can be specified:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">attr_name</code>: attribute assigned the isolated registry if used as a\nclass decorator.</p></li>\n<li><p><code class=\"docutils literal notranslate\">kwarg_name</code>: keyword argument passing the isolated registry if used as\na function decorator.</p></li>\n</ul>\n<p>The temporary <code class=\"docutils literal notranslate\">Apps</code> instance used to isolate model registration can be\nretrieved as an attribute when used as a class decorator by using the\n<code class=\"docutils literal notranslate\">attr_name</code> parameter:</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=\"nd\">@isolate_apps</span><span class=\"p\">(</span><span class=\"s2\">&quot;app_label&quot;</span><span class=\"p\">,</span> attr_name<span class=\"o\">=</span><span class=\"s2\">&quot;apps&quot;</span><span class=\"p\">)</span>\n<span class=\"k\">class</span> <span class=\"nc\">TestModelDefinition</span><span class=\"p\">(</span>SimpleTestCase<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_model_definition</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">class</span> <span class=\"nc\">TestModel</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n            <span class=\"k\">pass</span>\n\n        <span class=\"bp\">self</span><span class=\"o\">.</span>assertIs<span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span>apps<span class=\"o\">.</span>get_model<span class=\"p\">(</span><span class=\"s2\">&quot;app_label&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;TestModel&quot;</span><span class=\"p\">),</span> TestModel<span class=\"p\">)</span>\n</code></pre></div>\n<p>… or alternatively as an argument on the test method when used as a method\ndecorator by using the <code class=\"docutils literal notranslate\">kwarg_name</code> parameter:</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\">TestModelDefinition</span><span class=\"p\">(</span>SimpleTestCase<span class=\"p\">):</span>\n    <span class=\"nd\">@isolate_apps</span><span class=\"p\">(</span><span class=\"s2\">&quot;app_label&quot;</span><span class=\"p\">,</span> kwarg_name<span class=\"o\">=</span><span class=\"s2\">&quot;apps&quot;</span><span class=\"p\">)</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_model_definition</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> apps<span class=\"p\">):</span>\n        <span class=\"k\">class</span> <span class=\"nc\">TestModel</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n            <span class=\"k\">pass</span>\n\n        <span class=\"bp\">self</span><span class=\"o\">.</span>assertIs<span class=\"p\">(</span>apps<span class=\"o\">.</span>get_model<span class=\"p\">(</span><span class=\"s2\">&quot;app_label&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;TestModel&quot;</span><span class=\"p\">),</span> TestModel<span class=\"p\">)</span>\n</code></pre></div>\n</dd></dl>\n\n</section>\n<section id=\"emptying-the-test-outbox\">\n<span id=\"emptying-test-outbox\"></span><h3>Emptying the test outbox<a class=\"heading-anchor\" href=\"#emptying-the-test-outbox\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you use any of Django's custom <code class=\"docutils literal notranslate\">TestCase</code> classes, the test runner will\nclear the contents of the test email outbox at the start of each test case.</p>\n<p>For more detail on email services during tests, see <a class=\"reference internal\" href=\"#email-services\">Email services</a> below.</p>\n</section>\n<section id=\"assertions\">\n<span id=\"id2\"></span><h3>アサーション<a class=\"heading-anchor\" href=\"#assertions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>As Python's normal <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\">unittest.TestCase</code></a> class implements assertion methods\nsuch as <a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertTrue\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\">assertTrue()</code></a> and\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertEqual\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\">assertEqual()</code></a>, Django's custom <a class=\"reference internal\" href=\"#django.test.TestCase\" title=\"django.test.TestCase\"><code class=\"xref py py-class docutils literal notranslate\">TestCase</code></a> class\nprovides a number of custom assertion methods that are useful for testing web\napplications:</p>\n<p>The failure messages given by most of these assertion methods can be customized\nwith the <code class=\"docutils literal notranslate\">msg_prefix</code> argument. This string will be prefixed to any failure\nmessage generated by the assertion. This allows you to provide additional\ndetails that may help you to identify the location and cause of a failure in\nyour test suite.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.assertRaisesMessage\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">assertRaisesMessage</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">expected_exception</em>, <em class=\"sig-param\">expected_message</em>, <em class=\"sig-param\">callable</em>, <em class=\"sig-param\"><span class=\"o\">*</span>args</em>, <em class=\"sig-param\"><span class=\"o\">**</span>kwargs</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.assertRaisesMessage\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dt class=\"sig sig-object py\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">assertRaisesMessage</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">expected_exception</em>, <em class=\"sig-param\">expected_message</em><span class=\"sig-paren\">)</span></dt>\n<dd><p>Asserts that execution of <code class=\"docutils literal notranslate\">callable</code> raises <code class=\"docutils literal notranslate\">expected_exception</code> and\nthat <code class=\"docutils literal notranslate\">expected_message</code> is found in the exception's message. Any other\noutcome is reported as a failure. It's a simpler version of\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaisesRegex\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\">unittest.TestCase.assertRaisesRegex()</code></a> with the difference that\n<code class=\"docutils literal notranslate\">expected_message</code> isn't treated as a regular expression.</p>\n<p>If only the <code class=\"docutils literal notranslate\">expected_exception</code> and <code class=\"docutils literal notranslate\">expected_message</code> parameters are\ngiven, returns a context manager so that the code being tested can be\nwritten inline rather than as a function:</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\">with</span> <span class=\"bp\">self</span><span class=\"o\">.</span>assertRaisesMessage<span class=\"p\">(</span><span class=\"ne\">ValueError</span><span class=\"p\">,</span> <span class=\"s2\">&quot;invalid literal for int()&quot;</span><span class=\"p\">):</span>\n    <span class=\"nb\">int</span><span class=\"p\">(</span><span class=\"s2\">&quot;a&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.assertWarnsMessage\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">assertWarnsMessage</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">expected_warning</em>, <em class=\"sig-param\">expected_message</em>, <em class=\"sig-param\">callable</em>, <em class=\"sig-param\"><span class=\"o\">*</span>args</em>, <em class=\"sig-param\"><span class=\"o\">**</span>kwargs</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.assertWarnsMessage\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dt class=\"sig sig-object py\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">assertWarnsMessage</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">expected_warning</em>, <em class=\"sig-param\">expected_message</em><span class=\"sig-paren\">)</span></dt>\n<dd><p>Analogous to <a class=\"reference internal\" href=\"#django.test.SimpleTestCase.assertRaisesMessage\" title=\"django.test.SimpleTestCase.assertRaisesMessage\"><code class=\"xref py py-meth docutils literal notranslate\">SimpleTestCase.assertRaisesMessage()</code></a> but for\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertWarnsRegex\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\">assertWarnsRegex()</code></a> instead of\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaisesRegex\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\">assertRaisesRegex()</code></a>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.assertFieldOutput\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">assertFieldOutput</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">fieldclass</em>, <em class=\"sig-param\">valid</em>, <em class=\"sig-param\">invalid</em>, <em class=\"sig-param\">field_args<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">field_kwargs<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">empty_value<span class=\"o\">=</span><span class=\"default_value\">''</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.assertFieldOutput\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Asserts that a form field behaves correctly with various inputs.</p>\n<dl class=\"field-list simple\">\n<dt class=\"field-odd\">パラメータ<span class=\"colon\">:</span></dt>\n<dd class=\"field-odd\"><ul class=\"simple\">\n<li><p><strong>fieldclass</strong> -- the class of the field to be tested.</p></li>\n<li><p><strong>valid</strong> -- a dictionary mapping valid inputs to their expected cleaned\nvalues.</p></li>\n<li><p><strong>invalid</strong> -- a dictionary mapping invalid inputs to one or more raised\nerror messages.</p></li>\n<li><p><strong>field_args</strong> -- the args passed to instantiate the field.</p></li>\n<li><p><strong>field_kwargs</strong> -- the kwargs passed to instantiate the field.</p></li>\n<li><p><strong>empty_value</strong> -- the expected clean output for inputs in <code class=\"docutils literal notranslate\">empty_values</code>.</p></li>\n</ul>\n</dd>\n</dl>\n<p>For example, the following code tests that an <code class=\"docutils literal notranslate\">EmailField</code> accepts\n<code class=\"docutils literal notranslate\">a&#64;a.com</code> as a valid email address, but rejects <code class=\"docutils literal notranslate\">aaa</code> with a reasonable\nerror message:</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=\"bp\">self</span><span class=\"o\">.</span>assertFieldOutput<span class=\"p\">(</span>\n    EmailField<span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s2\">&quot;a@a.com&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;a@a.com&quot;</span><span class=\"p\">},</span> <span class=\"p\">{</span><span class=\"s2\">&quot;aaa&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;Enter a valid email address.&quot;</span><span class=\"p\">]}</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.assertFormError\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">assertFormError</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">form</em>, <em class=\"sig-param\">field</em>, <em class=\"sig-param\">errors</em>, <em class=\"sig-param\">msg_prefix<span class=\"o\">=</span><span class=\"default_value\">''</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.assertFormError\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Asserts that a field on a form raises the provided list of errors.</p>\n<p><code class=\"docutils literal notranslate\">form</code> is a <code class=\"docutils literal notranslate\">Form</code> instance. The form must be\n<a class=\"reference internal\" href=\"/ja/4.2/ref/forms/api/#ref-forms-api-bound-unbound\"><span class=\"std std-ref\">bound</span></a> but not necessarily\nvalidated (<code class=\"docutils literal notranslate\">assertFormError()</code> will automatically call <code class=\"docutils literal notranslate\">full_clean()</code>\non the form).</p>\n<p><code class=\"docutils literal notranslate\">field</code> is the name of the field on the form to check. To check the form's\n<a class=\"reference internal\" href=\"/ja/4.2/ref/forms/api/#django.forms.Form.non_field_errors\" title=\"django.forms.Form.non_field_errors\"><code class=\"xref py py-meth docutils literal notranslate\">non-field errors</code></a>, use\n<code class=\"docutils literal notranslate\">field=None</code>.</p>\n<p><code class=\"docutils literal notranslate\">errors</code> is a list of all the error strings that the field is expected to\nhave. You can also pass a single error string if you only expect one error\nwhich means that <code class=\"docutils literal notranslate\">errors='error message'</code> is the same as\n<code class=\"docutils literal notranslate\">errors=['error message']</code>.</p>\n<aside class=\"version-note version-changed\" data-version=\"4.1\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 4.1</p><p>In older versions, using an empty error list with <code class=\"docutils literal notranslate\">assertFormError()</code>\nwould always pass, regardless of whether the field had any errors or\nnot. Starting from Django 4.1, using <code class=\"docutils literal notranslate\">errors=[]</code> will only pass if\nthe field actually has no errors.</p>\n<p>Django 4.1 also changed the behavior of <code class=\"docutils literal notranslate\">assertFormError()</code> when a\nfield has multiple errors. In older versions, if a field had multiple\nerrors and you checked for only some of them, the test would pass.\nStarting from Django 4.1, the error list must be an exact match to the\nfield's actual errors.</p>\n</aside>\n<aside class=\"version-note version-deprecated\" data-version=\"4.1\" role=\"note\">\n<p class=\"version-note-title\">Deprecated since Django 4.1</p><p><span class=\"versionmodified deprecated\">バージョン 4.1 で非推奨: </span>Support for passing a response object and a form name to\n<code class=\"docutils literal notranslate\">assertFormError()</code> is deprecated and will be removed in Django 5.0.\nUse the form instance directly instead.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.assertFormSetError\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">assertFormSetError</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">formset</em>, <em class=\"sig-param\">form_index</em>, <em class=\"sig-param\">field</em>, <em class=\"sig-param\">errors</em>, <em class=\"sig-param\">msg_prefix<span class=\"o\">=</span><span class=\"default_value\">''</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.assertFormSetError\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Asserts that the <code class=\"docutils literal notranslate\">formset</code> raises the provided list of errors when\nrendered.</p>\n<p><code class=\"docutils literal notranslate\">formset</code> is a <code class=\"docutils literal notranslate\">FormSet</code> instance. The formset must be bound but not\nnecessarily validated (<code class=\"docutils literal notranslate\">assertFormSetError()</code> will automatically call the\n<code class=\"docutils literal notranslate\">full_clean()</code> on the formset).</p>\n<p><code class=\"docutils literal notranslate\">form_index</code> is the number of the form within the <code class=\"docutils literal notranslate\">FormSet</code> (starting\nfrom 0). Use <code class=\"docutils literal notranslate\">form_index=None</code> to check the formset's non-form errors,\ni.e. the errors you get when calling <code class=\"docutils literal notranslate\">formset.non_form_errors()</code>. In that\ncase you must also use <code class=\"docutils literal notranslate\">field=None</code>.</p>\n<p><code class=\"docutils literal notranslate\">field</code> and <code class=\"docutils literal notranslate\">errors</code> have the same meaning as the parameters to\n<code class=\"docutils literal notranslate\">assertFormError()</code>.</p>\n<aside class=\"version-note version-deprecated\" data-version=\"4.1\" role=\"note\">\n<p class=\"version-note-title\">Deprecated since Django 4.1</p><p><span class=\"versionmodified deprecated\">バージョン 4.1 で非推奨: </span>Support for passing a response object and a formset name to\n<code class=\"docutils literal notranslate\">assertFormSetError()</code> is deprecated and will be removed in Django\n5.0. Use the formset instance directly instead.</p>\n</aside>\n<aside class=\"version-note version-deprecated\" data-version=\"4.2\" role=\"note\">\n<p class=\"version-note-title\">Deprecated since Django 4.2</p><p><span class=\"versionmodified deprecated\">バージョン 4.2 で非推奨: </span>The <code class=\"docutils literal notranslate\">assertFormsetError()</code> assertion method is deprecated. Use\n<code class=\"docutils literal notranslate\">assertFormSetError()</code> instead.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.assertContains\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">assertContains</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">response</em>, <em class=\"sig-param\">text</em>, <em class=\"sig-param\">count<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">status_code<span class=\"o\">=</span><span class=\"default_value\">200</span></em>, <em class=\"sig-param\">msg_prefix<span class=\"o\">=</span><span class=\"default_value\">''</span></em>, <em class=\"sig-param\">html<span class=\"o\">=</span><span class=\"default_value\">False</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.assertContains\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Asserts that a <a class=\"reference internal\" href=\"/ja/4.2/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\">response</code></a> produced the\ngiven <a class=\"reference internal\" href=\"/ja/4.2/ref/request-response/#django.http.HttpResponse.status_code\" title=\"django.http.HttpResponse.status_code\"><code class=\"xref py py-attr docutils literal notranslate\">status_code</code></a> and that <code class=\"docutils literal notranslate\">text</code>\nappears in its <a class=\"reference internal\" href=\"/ja/4.2/ref/request-response/#django.http.HttpResponse.content\" title=\"django.http.HttpResponse.content\"><code class=\"xref py py-attr docutils literal notranslate\">content</code></a>. If <code class=\"docutils literal notranslate\">count</code>\nis provided, <code class=\"docutils literal notranslate\">text</code> must occur exactly <code class=\"docutils literal notranslate\">count</code> times in the response.</p>\n<p>Set <code class=\"docutils literal notranslate\">html</code> to <code class=\"docutils literal notranslate\">True</code> to handle <code class=\"docutils literal notranslate\">text</code> as HTML. The comparison with\nthe response content will be based on HTML semantics instead of\ncharacter-by-character equality. Whitespace is ignored in most cases,\nattribute ordering is not significant. See\n<a class=\"reference internal\" href=\"#django.test.SimpleTestCase.assertHTMLEqual\" title=\"django.test.SimpleTestCase.assertHTMLEqual\"><code class=\"xref py py-meth docutils literal notranslate\">assertHTMLEqual()</code></a> for more details.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.assertNotContains\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">assertNotContains</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">response</em>, <em class=\"sig-param\">text</em>, <em class=\"sig-param\">status_code<span class=\"o\">=</span><span class=\"default_value\">200</span></em>, <em class=\"sig-param\">msg_prefix<span class=\"o\">=</span><span class=\"default_value\">''</span></em>, <em class=\"sig-param\">html<span class=\"o\">=</span><span class=\"default_value\">False</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.assertNotContains\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Asserts that a <a class=\"reference internal\" href=\"/ja/4.2/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\">response</code></a> produced the\ngiven <a class=\"reference internal\" href=\"/ja/4.2/ref/request-response/#django.http.HttpResponse.status_code\" title=\"django.http.HttpResponse.status_code\"><code class=\"xref py py-attr docutils literal notranslate\">status_code</code></a> and that <code class=\"docutils literal notranslate\">text</code> does\n<em>not</em> appear in its <a class=\"reference internal\" href=\"/ja/4.2/ref/request-response/#django.http.HttpResponse.content\" title=\"django.http.HttpResponse.content\"><code class=\"xref py py-attr docutils literal notranslate\">content</code></a>.</p>\n<p>Set <code class=\"docutils literal notranslate\">html</code> to <code class=\"docutils literal notranslate\">True</code> to handle <code class=\"docutils literal notranslate\">text</code> as HTML. The comparison with\nthe response content will be based on HTML semantics instead of\ncharacter-by-character equality. Whitespace is ignored in most cases,\nattribute ordering is not significant. See\n<a class=\"reference internal\" href=\"#django.test.SimpleTestCase.assertHTMLEqual\" title=\"django.test.SimpleTestCase.assertHTMLEqual\"><code class=\"xref py py-meth docutils literal notranslate\">assertHTMLEqual()</code></a> for more details.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.assertTemplateUsed\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">assertTemplateUsed</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">response</em>, <em class=\"sig-param\">template_name</em>, <em class=\"sig-param\">msg_prefix<span class=\"o\">=</span><span class=\"default_value\">''</span></em>, <em class=\"sig-param\">count<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.assertTemplateUsed\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Asserts that the template with the given name was used in rendering the\nresponse.</p>\n<p><code class=\"docutils literal notranslate\">response</code> must be a response instance returned by the\n<a class=\"reference internal\" href=\"#django.test.Response\" title=\"django.test.Response\"><code class=\"xref py py-class docutils literal notranslate\">test client</code></a>.</p>\n<p><code class=\"docutils literal notranslate\">template_name</code> should be a string such as <code class=\"docutils literal notranslate\">'admin/index.html'</code>.</p>\n<p>The <code class=\"docutils literal notranslate\">count</code> argument is an integer indicating the number of times the\ntemplate should be rendered. Default is <code class=\"docutils literal notranslate\">None</code>, meaning that the template\nshould be rendered one or more times.</p>\n<p>You can use this as a context manager, like this:</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\">with</span> <span class=\"bp\">self</span><span class=\"o\">.</span>assertTemplateUsed<span class=\"p\">(</span><span class=\"s2\">&quot;index.html&quot;</span><span class=\"p\">):</span>\n    render_to_string<span class=\"p\">(</span><span class=\"s2\">&quot;index.html&quot;</span><span class=\"p\">)</span>\n<span class=\"k\">with</span> <span class=\"bp\">self</span><span class=\"o\">.</span>assertTemplateUsed<span class=\"p\">(</span>template_name<span class=\"o\">=</span><span class=\"s2\">&quot;index.html&quot;</span><span class=\"p\">):</span>\n    render_to_string<span class=\"p\">(</span><span class=\"s2\">&quot;index.html&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.assertTemplateNotUsed\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">assertTemplateNotUsed</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">response</em>, <em class=\"sig-param\">template_name</em>, <em class=\"sig-param\">msg_prefix<span class=\"o\">=</span><span class=\"default_value\">''</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.assertTemplateNotUsed\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Asserts that the template with the given name was <em>not</em> used in rendering\nthe response.</p>\n<p>You can use this as a context manager in the same way as\n<a class=\"reference internal\" href=\"#django.test.SimpleTestCase.assertTemplateUsed\" title=\"django.test.SimpleTestCase.assertTemplateUsed\"><code class=\"xref py py-meth docutils literal notranslate\">assertTemplateUsed()</code></a>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.assertURLEqual\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">assertURLEqual</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">url1</em>, <em class=\"sig-param\">url2</em>, <em class=\"sig-param\">msg_prefix<span class=\"o\">=</span><span class=\"default_value\">''</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.assertURLEqual\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Asserts that two URLs are the same, ignoring the order of query string\nparameters except for parameters with the same name. For example,\n<code class=\"docutils literal notranslate\">/path/?x=1&amp;y=2</code> is equal to <code class=\"docutils literal notranslate\">/path/?y=2&amp;x=1</code>, but\n<code class=\"docutils literal notranslate\">/path/?a=1&amp;a=2</code> isn't equal to <code class=\"docutils literal notranslate\">/path/?a=2&amp;a=1</code>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.assertRedirects\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">assertRedirects</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">response</em>, <em class=\"sig-param\">expected_url</em>, <em class=\"sig-param\">status_code<span class=\"o\">=</span><span class=\"default_value\">302</span></em>, <em class=\"sig-param\">target_status_code<span class=\"o\">=</span><span class=\"default_value\">200</span></em>, <em class=\"sig-param\">msg_prefix<span class=\"o\">=</span><span class=\"default_value\">''</span></em>, <em class=\"sig-param\">fetch_redirect_response<span class=\"o\">=</span><span class=\"default_value\">True</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.assertRedirects\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Asserts that the <a class=\"reference internal\" href=\"/ja/4.2/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\">response</code></a> returned a\n<a class=\"reference internal\" href=\"/ja/4.2/ref/request-response/#django.http.HttpResponse.status_code\" title=\"django.http.HttpResponse.status_code\"><code class=\"xref py py-attr docutils literal notranslate\">status_code</code></a> redirect status, redirected\nto <code class=\"docutils literal notranslate\">expected_url</code> (including any <code class=\"docutils literal notranslate\">GET</code> data), and that the final page\nwas received with <code class=\"docutils literal notranslate\">target_status_code</code>.</p>\n<p>If your request used the <code class=\"docutils literal notranslate\">follow</code> argument, the <code class=\"docutils literal notranslate\">expected_url</code> and\n<code class=\"docutils literal notranslate\">target_status_code</code> will be the url and status code for the final\npoint of the redirect chain.</p>\n<p>If <code class=\"docutils literal notranslate\">fetch_redirect_response</code> is <code class=\"docutils literal notranslate\">False</code>, the final page won't be\nloaded. Since the test client can't fetch external URLs, this is\nparticularly useful if <code class=\"docutils literal notranslate\">expected_url</code> isn't part of your Django app.</p>\n<p>Scheme is handled correctly when making comparisons between two URLs. If\nthere isn't any scheme specified in the location where we are redirected to,\nthe original request's scheme is used. If present, the scheme in\n<code class=\"docutils literal notranslate\">expected_url</code> is the one used to make the comparisons to.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.assertHTMLEqual\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">assertHTMLEqual</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">html1</em>, <em class=\"sig-param\">html2</em>, <em class=\"sig-param\">msg<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.assertHTMLEqual\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Asserts that the strings <code class=\"docutils literal notranslate\">html1</code> and <code class=\"docutils literal notranslate\">html2</code> are equal. The comparison\nis based on HTML semantics. The comparison takes following things into\naccount:</p>\n<ul class=\"simple\">\n<li><p>Whitespace before and after HTML tags is ignored.</p></li>\n<li><p>All types of whitespace are considered equivalent.</p></li>\n<li><p>All open tags are closed implicitly, e.g. when a surrounding tag is\nclosed or the HTML document ends.</p></li>\n<li><p>Empty tags are equivalent to their self-closing version.</p></li>\n<li><p>The ordering of attributes of an HTML element is not significant.</p></li>\n<li><p>Boolean attributes (like <code class=\"docutils literal notranslate\">checked</code>) without an argument are equal to\nattributes that equal in name and value (see the examples).</p></li>\n<li><p>Text, character references, and entity references that refer to the same\ncharacter are equivalent.</p></li>\n</ul>\n<p>The following examples are valid tests and don't raise any\n<code class=\"docutils literal notranslate\">AssertionError</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=\"bp\">self</span><span class=\"o\">.</span>assertHTMLEqual<span class=\"p\">(</span>\n    <span class=\"s2\">&quot;&lt;p&gt;Hello &lt;b&gt;&amp;#x27;world&amp;#x27;!&lt;/p&gt;&quot;</span><span class=\"p\">,</span>\n    <span class=\"sd\">&quot;&quot;&quot;&lt;p&gt;</span>\n<span class=\"sd\">        Hello   &lt;b&gt;&amp;#39;world&amp;#39;! &lt;/b&gt;</span>\n<span class=\"sd\">    &lt;/p&gt;&quot;&quot;&quot;</span><span class=\"p\">,</span>\n<span class=\"p\">)</span>\n<span class=\"bp\">self</span><span class=\"o\">.</span>assertHTMLEqual<span class=\"p\">(</span>\n    <span class=\"s1\">&#39;&lt;input type=&quot;checkbox&quot; checked=&quot;checked&quot; id=&quot;id_accept_terms&quot; /&gt;&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;&lt;input id=&quot;id_accept_terms&quot; type=&quot;checkbox&quot; checked&gt;&#39;</span><span class=\"p\">,</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\">html1</code> and <code class=\"docutils literal notranslate\">html2</code> must contain HTML. An <code class=\"docutils literal notranslate\">AssertionError</code> will be\nraised if one of them cannot be parsed.</p>\n<p>Output in case of error can be customized with the <code class=\"docutils literal notranslate\">msg</code> argument.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.assertHTMLNotEqual\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">assertHTMLNotEqual</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">html1</em>, <em class=\"sig-param\">html2</em>, <em class=\"sig-param\">msg<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.assertHTMLNotEqual\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Asserts that the strings <code class=\"docutils literal notranslate\">html1</code> and <code class=\"docutils literal notranslate\">html2</code> are <em>not</em> equal. The\ncomparison is based on HTML semantics. See\n<a class=\"reference internal\" href=\"#django.test.SimpleTestCase.assertHTMLEqual\" title=\"django.test.SimpleTestCase.assertHTMLEqual\"><code class=\"xref py py-meth docutils literal notranslate\">assertHTMLEqual()</code></a> for details.</p>\n<p><code class=\"docutils literal notranslate\">html1</code> and <code class=\"docutils literal notranslate\">html2</code> must contain HTML. An <code class=\"docutils literal notranslate\">AssertionError</code> will be\nraised if one of them cannot be parsed.</p>\n<p>Output in case of error can be customized with the <code class=\"docutils literal notranslate\">msg</code> argument.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.assertXMLEqual\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">assertXMLEqual</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">xml1</em>, <em class=\"sig-param\">xml2</em>, <em class=\"sig-param\">msg<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.assertXMLEqual\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Asserts that the strings <code class=\"docutils literal notranslate\">xml1</code> and <code class=\"docutils literal notranslate\">xml2</code> are equal. The\ncomparison is based on XML semantics. Similarly to\n<a class=\"reference internal\" href=\"#django.test.SimpleTestCase.assertHTMLEqual\" title=\"django.test.SimpleTestCase.assertHTMLEqual\"><code class=\"xref py py-meth docutils literal notranslate\">assertHTMLEqual()</code></a>, the comparison is\nmade on parsed content, hence only semantic differences are considered, not\nsyntax differences. When invalid XML is passed in any parameter, an\n<code class=\"docutils literal notranslate\">AssertionError</code> is always raised, even if both strings are identical.</p>\n<p>XML declaration, document type, processing instructions, and comments are\nignored. Only the root element and its children are compared.</p>\n<p>Output in case of error can be customized with the <code class=\"docutils literal notranslate\">msg</code> argument.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.assertXMLNotEqual\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">assertXMLNotEqual</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">xml1</em>, <em class=\"sig-param\">xml2</em>, <em class=\"sig-param\">msg<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.assertXMLNotEqual\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Asserts that the strings <code class=\"docutils literal notranslate\">xml1</code> and <code class=\"docutils literal notranslate\">xml2</code> are <em>not</em> equal. The\ncomparison is based on XML semantics. See\n<a class=\"reference internal\" href=\"#django.test.SimpleTestCase.assertXMLEqual\" title=\"django.test.SimpleTestCase.assertXMLEqual\"><code class=\"xref py py-meth docutils literal notranslate\">assertXMLEqual()</code></a> for details.</p>\n<p>Output in case of error can be customized with the <code class=\"docutils literal notranslate\">msg</code> argument.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.assertInHTML\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">assertInHTML</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">needle</em>, <em class=\"sig-param\">haystack</em>, <em class=\"sig-param\">count<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">msg_prefix<span class=\"o\">=</span><span class=\"default_value\">''</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.assertInHTML\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Asserts that the HTML fragment <code class=\"docutils literal notranslate\">needle</code> is contained in the <code class=\"docutils literal notranslate\">haystack</code>\nonce.</p>\n<p>If the <code class=\"docutils literal notranslate\">count</code> integer argument is specified, then additionally the number\nof <code class=\"docutils literal notranslate\">needle</code> occurrences will be strictly verified.</p>\n<p>Whitespace in most cases is ignored, and attribute ordering is not\nsignificant. See <a class=\"reference internal\" href=\"#django.test.SimpleTestCase.assertHTMLEqual\" title=\"django.test.SimpleTestCase.assertHTMLEqual\"><code class=\"xref py py-meth docutils literal notranslate\">assertHTMLEqual()</code></a> for more details.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.assertJSONEqual\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">assertJSONEqual</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">raw</em>, <em class=\"sig-param\">expected_data</em>, <em class=\"sig-param\">msg<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.assertJSONEqual\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Asserts that the JSON fragments <code class=\"docutils literal notranslate\">raw</code> and <code class=\"docutils literal notranslate\">expected_data</code> are equal.\nUsual JSON non-significant whitespace rules apply as the heavyweight is\ndelegated to the <a class=\"reference external\" href=\"https://docs.python.org/3/library/json.html#module-json\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\">json</code></a> library.</p>\n<p>Output in case of error can be customized with the <code class=\"docutils literal notranslate\">msg</code> argument.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.SimpleTestCase.assertJSONNotEqual\">\n<span class=\"sig-prename descclassname\">SimpleTestCase.</span><span class=\"sig-name descname\">assertJSONNotEqual</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">raw</em>, <em class=\"sig-param\">expected_data</em>, <em class=\"sig-param\">msg<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.SimpleTestCase.assertJSONNotEqual\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Asserts that the JSON fragments <code class=\"docutils literal notranslate\">raw</code> and <code class=\"docutils literal notranslate\">expected_data</code> are <em>not</em> equal.\nSee <a class=\"reference internal\" href=\"#django.test.SimpleTestCase.assertJSONEqual\" title=\"django.test.SimpleTestCase.assertJSONEqual\"><code class=\"xref py py-meth docutils literal notranslate\">assertJSONEqual()</code></a> for further details.</p>\n<p>Output in case of error can be customized with the <code class=\"docutils literal notranslate\">msg</code> argument.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.TransactionTestCase.assertQuerySetEqual\">\n<span class=\"sig-prename descclassname\">TransactionTestCase.</span><span class=\"sig-name descname\">assertQuerySetEqual</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">qs</em>, <em class=\"sig-param\">values</em>, <em class=\"sig-param\">transform<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">ordered<span class=\"o\">=</span><span class=\"default_value\">True</span></em>, <em class=\"sig-param\">msg<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.TransactionTestCase.assertQuerySetEqual\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Asserts that a queryset <code class=\"docutils literal notranslate\">qs</code> matches a particular iterable of values\n<code class=\"docutils literal notranslate\">values</code>.</p>\n<p>If <code class=\"docutils literal notranslate\">transform</code> is provided, <code class=\"docutils literal notranslate\">values</code> is compared to a list produced by\napplying <code class=\"docutils literal notranslate\">transform</code> to each member of <code class=\"docutils literal notranslate\">qs</code>.</p>\n<p>By default, the comparison is also ordering dependent. If <code class=\"docutils literal notranslate\">qs</code> doesn't\nprovide an implicit ordering, you can set the <code class=\"docutils literal notranslate\">ordered</code> parameter to\n<code class=\"docutils literal notranslate\">False</code>, which turns the comparison into a <code class=\"docutils literal notranslate\">collections.Counter</code> comparison.\nIf the order is undefined (if the given <code class=\"docutils literal notranslate\">qs</code> isn't ordered and the\ncomparison is against more than one ordered value), a <code class=\"docutils literal notranslate\">ValueError</code> is\nraised.</p>\n<p>Output in case of error can be customized with the <code class=\"docutils literal notranslate\">msg</code> argument.</p>\n<aside class=\"version-note version-deprecated\" data-version=\"4.2\" role=\"note\">\n<p class=\"version-note-title\">Deprecated since Django 4.2</p><p><span class=\"versionmodified deprecated\">バージョン 4.2 で非推奨: </span>The <code class=\"docutils literal notranslate\">assertQuerysetEqual()</code> assertion method is deprecated. Use\n<code class=\"docutils literal notranslate\">assertQuerySetEqual()</code> instead.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.test.TransactionTestCase.assertNumQueries\">\n<span class=\"sig-prename descclassname\">TransactionTestCase.</span><span class=\"sig-name descname\">assertNumQueries</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">num</em>, <em class=\"sig-param\">func</em>, <em class=\"sig-param\"><span class=\"o\">*</span>args</em>, <em class=\"sig-param\"><span class=\"o\">**</span>kwargs</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.TransactionTestCase.assertNumQueries\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Asserts that when <code class=\"docutils literal notranslate\">func</code> is called with <code class=\"docutils literal notranslate\">*args</code> and <code class=\"docutils literal notranslate\">**kwargs</code> that\n<code class=\"docutils literal notranslate\">num</code> database queries are executed.</p>\n<p>If a <code class=\"docutils literal notranslate\">&quot;using&quot;</code> key is present in <code class=\"docutils literal notranslate\">kwargs</code> it is used as the database\nalias for which to check the number of queries:</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=\"bp\">self</span><span class=\"o\">.</span>assertNumQueries<span class=\"p\">(</span><span class=\"mi\">7</span><span class=\"p\">,</span> using<span class=\"o\">=</span><span class=\"s2\">&quot;non_default_db&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>If you wish to call a function with a <code class=\"docutils literal notranslate\">using</code> parameter you can do it by\nwrapping the call with a <code class=\"docutils literal notranslate\">lambda</code> to add an extra parameter:</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=\"bp\">self</span><span class=\"o\">.</span>assertNumQueries<span class=\"p\">(</span><span class=\"mi\">7</span><span class=\"p\">,</span> <span class=\"k\">lambda</span><span class=\"p\">:</span> my_function<span class=\"p\">(</span>using<span class=\"o\">=</span><span class=\"mi\">7</span><span class=\"p\">))</span>\n</code></pre></div>\n<p>You can also use this as a context manager:</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\">with</span> <span class=\"bp\">self</span><span class=\"o\">.</span>assertNumQueries<span class=\"p\">(</span><span class=\"mi\">2</span><span class=\"p\">):</span>\n    Person<span class=\"o\">.</span>objects<span class=\"o\">.</span>create<span class=\"p\">(</span>name<span class=\"o\">=</span><span class=\"s2\">&quot;Aaron&quot;</span><span class=\"p\">)</span>\n    Person<span class=\"o\">.</span>objects<span class=\"o\">.</span>create<span class=\"p\">(</span>name<span class=\"o\">=</span><span class=\"s2\">&quot;Daniel&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n</dd></dl>\n\n</section>\n<section id=\"tagging-tests\">\n<span id=\"topics-tagging-tests\"></span><h3>Tagging tests<a class=\"heading-anchor\" href=\"#tagging-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>You can tag your tests so you can easily run a particular subset. For example,\nyou might label fast or slow tests:</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> tag\n\n\n<span class=\"k\">class</span> <span class=\"nc\">SampleTestCase</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    <span class=\"nd\">@tag</span><span class=\"p\">(</span><span class=\"s2\">&quot;fast&quot;</span><span class=\"p\">)</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_fast</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"o\">...</span>\n\n    <span class=\"nd\">@tag</span><span class=\"p\">(</span><span class=\"s2\">&quot;slow&quot;</span><span class=\"p\">)</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_slow</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"o\">...</span>\n\n    <span class=\"nd\">@tag</span><span class=\"p\">(</span><span class=\"s2\">&quot;slow&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;core&quot;</span><span class=\"p\">)</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_slow_but_core</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"o\">...</span>\n</code></pre></div>\n<p>You can also tag a test case:</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=\"nd\">@tag</span><span class=\"p\">(</span><span class=\"s2\">&quot;slow&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;core&quot;</span><span class=\"p\">)</span>\n<span class=\"k\">class</span> <span class=\"nc\">SampleTestCase</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    <span class=\"o\">...</span>\n</code></pre></div>\n<p>Subclasses inherit tags from superclasses, and methods inherit tags from their\nclass. Given:</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=\"nd\">@tag</span><span class=\"p\">(</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">)</span>\n<span class=\"k\">class</span> <span class=\"nc\">SampleTestCaseChild</span><span class=\"p\">(</span>SampleTestCase<span class=\"p\">):</span>\n    <span class=\"nd\">@tag</span><span class=\"p\">(</span><span class=\"s2\">&quot;bar&quot;</span><span class=\"p\">)</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"o\">...</span>\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\">SampleTestCaseChild.test</code> will be labeled with <code class=\"docutils literal notranslate\">'slow'</code>, <code class=\"docutils literal notranslate\">'core'</code>,\n<code class=\"docutils literal notranslate\">'bar'</code>, and <code class=\"docutils literal notranslate\">'foo'</code>.</p>\n<p>Then you can choose which tests to run. For example, to run only fast tests:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-2-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./manage.py <span class=\"nb\">test</span> --tag<span class=\"o\">=</span>fast\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-2-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> manage.py test --tag=fast\n</code></pre></div></div></div>\n<p>Or to run fast tests and the core one (even though it's slow):</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-3-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./manage.py <span class=\"nb\">test</span> --tag<span class=\"o\">=</span>fast --tag<span class=\"o\">=</span>core\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-3-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> manage.py test --tag=fast --tag=core\n</code></pre></div></div></div>\n<p>You can also exclude tests by tag. To run core tests if they are not slow:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-4-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./manage.py <span class=\"nb\">test</span> --tag<span class=\"o\">=</span>core --exclude-tag<span class=\"o\">=</span>slow\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-4-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> manage.py test --tag=core --exclude-tag=slow\n</code></pre></div></div></div>\n<p><a class=\"reference internal\" href=\"/ja/4.2/ref/django-admin/#cmdoption-test-exclude-tag\"><code class=\"xref std std-option docutils literal notranslate\">test --exclude-tag</code></a> has precedence over <a class=\"reference internal\" href=\"/ja/4.2/ref/django-admin/#cmdoption-test-tag\"><code class=\"xref std std-option docutils literal notranslate\">test --tag</code></a>, so if a\ntest has two tags and you select one of them and exclude the other, the test\nwon't be run.</p>\n</section>\n</section>\n<section id=\"testing-asynchronous-code\">\n<span id=\"async-tests\"></span><h2>Testing asynchronous code<a class=\"heading-anchor\" href=\"#testing-asynchronous-code\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>If you merely want to test the output of your asynchronous views, the standard\ntest client will run them inside their own asynchronous loop without any extra\nwork needed on your part.</p>\n<p>However, if you want to write fully-asynchronous tests for a Django project,\nyou will need to take several things into account.</p>\n<p>Firstly, your tests must be <code class=\"docutils literal notranslate\">async def</code> methods on the test class (in order\nto give them an asynchronous context). Django will automatically detect\nany <code class=\"docutils literal notranslate\">async def</code> tests and wrap them so they run in their own event loop.</p>\n<p>If you are testing from an asynchronous function, you must also use the\nasynchronous test client. This is available as <code class=\"docutils literal notranslate\">django.test.AsyncClient</code>,\nor as <code class=\"docutils literal notranslate\">self.async_client</code> on any test.</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.test.AsyncClient\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">AsyncClient</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">enforce_csrf_checks<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">raise_request_exception<span class=\"o\">=</span><span class=\"default_value\">True</span></em>, <em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\">*</abbr></span></em>, <em class=\"sig-param\">headers<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>defaults</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.AsyncClient\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p><code class=\"docutils literal notranslate\">AsyncClient</code> has the same methods and signatures as the synchronous (normal)\ntest client, with the following exceptions:</p>\n<ul>\n<li><p>In the initialization, arbitrary keyword arguments in <code class=\"docutils literal notranslate\">defaults</code> are added\ndirectly into the ASGI scope.</p></li>\n<li><p>The <code class=\"docutils literal notranslate\">follow</code> parameter is not supported.</p></li>\n<li><p>Headers passed as <code class=\"docutils literal notranslate\">extra</code> keyword arguments should not have the <code class=\"docutils literal notranslate\">HTTP_</code>\nprefix required by the synchronous client (see <a class=\"reference internal\" href=\"#django.test.Client.get\" title=\"django.test.Client.get\"><code class=\"xref py py-meth docutils literal notranslate\">Client.get()</code></a>). For\nexample, here is how to set an HTTP <code class=\"docutils literal notranslate\">Accept</code> header:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>c <span class=\"o\">=</span> AsyncClient<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s2\">&quot;/customers/details/&quot;</span><span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s2\">&quot;name&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;fred&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;age&quot;</span><span class=\"p\">:</span> <span class=\"mi\">7</span><span class=\"p\">},</span> ACCEPT<span class=\"o\">=</span><span class=\"s2\">&quot;application/json&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n</li>\n</ul>\n<aside class=\"version-note version-changed\" data-version=\"4.2\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 4.2</p><p>The <code class=\"docutils literal notranslate\">headers</code> parameter was added.</p>\n</aside>\n<p>Using <code class=\"docutils literal notranslate\">AsyncClient</code> any method that makes a request must be awaited:</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\">async</span> <span class=\"k\">def</span> <span class=\"nf\">test_my_thing</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n    response <span class=\"o\">=</span> <span class=\"k\">await</span> <span class=\"bp\">self</span><span class=\"o\">.</span>async_client<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s2\">&quot;/some-url/&quot;</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>The asynchronous client can also call synchronous views; it runs through\nDjango's <a class=\"reference internal\" href=\"/ja/4.2/topics/async/\"><span class=\"doc\">asynchronous request path</span></a>, which supports both.\nAny view called through the <code class=\"docutils literal notranslate\">AsyncClient</code> will get an <code class=\"docutils literal notranslate\">ASGIRequest</code> object\nfor its <code class=\"docutils literal notranslate\">request</code> rather than the <code class=\"docutils literal notranslate\">WSGIRequest</code> that the normal client\ncreates.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p>If you are using test decorators, they must be async-compatible to ensure\nthey work correctly. Django's built-in decorators will behave correctly, but\nthird-party ones may appear to not execute (they will &quot;wrap&quot; the wrong part\nof the execution flow and not your test).</p>\n<p>If you need to use these decorators, then you should decorate your test\nmethods with <a class=\"reference internal\" href=\"/ja/4.2/topics/async/#asgiref.sync.async_to_sync\" title=\"asgiref.sync.async_to_sync\"><code class=\"xref py py-func docutils literal notranslate\">async_to_sync()</code></a> <em>inside</em> of them instead:</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\">asgiref.sync</span> <span class=\"kn\">import</span> async_to_sync\n<span class=\"kn\">from</span> <span class=\"nn\">django.test</span> <span class=\"kn\">import</span> TestCase\n\n\n<span class=\"k\">class</span> <span class=\"nc\">MyTests</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    <span class=\"nd\">@mock</span><span class=\"o\">.</span>patch<span class=\"p\">(</span><span class=\"o\">...</span><span class=\"p\">)</span>\n    <span class=\"nd\">@async_to_sync</span>\n    <span class=\"k\">async</span> <span class=\"k\">def</span> <span class=\"nf\">test_my_thing</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"o\">...</span>\n</code></pre></div>\n</aside>\n</section>\n<section id=\"email-services\">\n<span id=\"topics-testing-email\"></span><h2>Email services<a class=\"heading-anchor\" href=\"#email-services\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>If any of your Django views send email using <a class=\"reference internal\" href=\"/ja/4.2/topics/email/\"><span class=\"doc\">Django's email\nfunctionality</span></a>, you probably don't want to send email each time\nyou run a test using that view. For this reason, Django's test runner\nautomatically redirects all Django-sent email to a dummy outbox. This lets you\ntest every aspect of sending email -- from the number of messages sent to the\ncontents of each message -- without actually sending the messages.</p>\n<p>The test runner accomplishes this by transparently replacing the normal\nemail backend with a testing backend.\n(Don't worry -- this has no effect on any other email senders outside of\nDjango, such as your machine's mail server, if you're running one.)</p>\n<dl class=\"py data\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.django.core.mail.outbox\">\n<span class=\"sig-prename descclassname\">django.core.mail.</span><span class=\"sig-name descname\">outbox</span><a class=\"heading-anchor\" href=\"#django.core.mail.django.core.mail.outbox\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>During test running, each outgoing email is saved in\n<code class=\"docutils literal notranslate\">django.core.mail.outbox</code>. This is a list of all\n<a class=\"reference internal\" href=\"/ja/4.2/topics/email/#django.core.mail.EmailMessage\" title=\"django.core.mail.EmailMessage\"><code class=\"xref py py-class docutils literal notranslate\">EmailMessage</code></a> instances that have been sent.  The\n<code class=\"docutils literal notranslate\">outbox</code> attribute is a special attribute that is created <em>only</em> when the\n<code class=\"docutils literal notranslate\">locmem</code> email backend is used. It doesn't normally exist as part of the\n<a class=\"reference internal\" href=\"/ja/4.2/topics/email/#module-django.core.mail\" title=\"django.core.mail: Helpers to easily send email.\"><code class=\"xref py py-mod docutils literal notranslate\">django.core.mail</code></a> module and you can't import it directly. The code below\nshows how to access this attribute correctly.</p>\n<p>Here's an example test that examines <code class=\"docutils literal notranslate\">django.core.mail.outbox</code> for length\nand contents:</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.core</span> <span class=\"kn\">import</span> mail\n<span class=\"kn\">from</span> <span class=\"nn\">django.test</span> <span class=\"kn\">import</span> TestCase\n\n\n<span class=\"k\">class</span> <span class=\"nc\">EmailTest</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_send_email</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"c1\"># Send message.</span>\n        mail<span class=\"o\">.</span>send_mail<span class=\"p\">(</span>\n            <span class=\"s2\">&quot;Subject here&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;Here is the message.&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;from@example.com&quot;</span><span class=\"p\">,</span>\n            <span class=\"p\">[</span><span class=\"s2\">&quot;to@example.com&quot;</span><span class=\"p\">],</span>\n            fail_silently<span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">,</span>\n        <span class=\"p\">)</span>\n\n        <span class=\"c1\"># Test that one message has been sent.</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>assertEqual<span class=\"p\">(</span><span class=\"nb\">len</span><span class=\"p\">(</span>mail<span class=\"o\">.</span>outbox<span class=\"p\">),</span> <span class=\"mi\">1</span><span class=\"p\">)</span>\n\n        <span class=\"c1\"># Verify that the subject of the first message is correct.</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>assertEqual<span class=\"p\">(</span>mail<span class=\"o\">.</span>outbox<span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span><span class=\"o\">.</span>subject<span class=\"p\">,</span> <span class=\"s2\">&quot;Subject here&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>As noted <a class=\"reference internal\" href=\"#emptying-test-outbox\"><span class=\"std std-ref\">previously</span></a>, the test outbox is emptied\nat the start of every test in a Django <code class=\"docutils literal notranslate\">*TestCase</code>. To empty the outbox\nmanually, assign the empty list to <code class=\"docutils literal notranslate\">mail.outbox</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=\"kn\">from</span> <span class=\"nn\">django.core</span> <span class=\"kn\">import</span> mail\n\n<span class=\"c1\"># Empty the test outbox</span>\nmail<span class=\"o\">.</span>outbox <span class=\"o\">=</span> <span class=\"p\">[]</span>\n</code></pre></div>\n</section>\n<section id=\"management-commands\">\n<span id=\"topics-testing-management-commands\"></span><h2>管理コマンド<a class=\"heading-anchor\" href=\"#management-commands\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Management commands can be tested with the\n<a class=\"reference internal\" href=\"/ja/4.2/ref/django-admin/#django.core.management.call_command\" title=\"django.core.management.call_command\"><code class=\"xref py py-func docutils literal notranslate\">call_command()</code></a> function. The output can be\nredirected into a <code class=\"docutils literal notranslate\">StringIO</code> instance:</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\">io</span> <span class=\"kn\">import</span> StringIO\n<span class=\"kn\">from</span> <span class=\"nn\">django.core.management</span> <span class=\"kn\">import</span> call_command\n<span class=\"kn\">from</span> <span class=\"nn\">django.test</span> <span class=\"kn\">import</span> TestCase\n\n\n<span class=\"k\">class</span> <span class=\"nc\">ClosepollTest</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_command_output</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        out <span class=\"o\">=</span> StringIO<span class=\"p\">()</span>\n        call_command<span class=\"p\">(</span><span class=\"s2\">&quot;closepoll&quot;</span><span class=\"p\">,</span> stdout<span class=\"o\">=</span>out<span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>assertIn<span class=\"p\">(</span><span class=\"s2\">&quot;Expected output&quot;</span><span class=\"p\">,</span> out<span class=\"o\">.</span>getvalue<span class=\"p\">())</span>\n</code></pre></div>\n</section>\n<section id=\"skipping-tests\">\n<span id=\"id3\"></span><h2>テストのスキップ<a class=\"heading-anchor\" href=\"#skipping-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The unittest library provides the <a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.skipIf\" title=\"(in Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\">&#64;skipIf</code></a> and\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.skipUnless\" title=\"(in Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\">&#64;skipUnless</code></a> decorators to allow you to skip tests\nif you know ahead of time that those tests are going to fail under certain\nconditions.</p>\n<p>For example, if your test requires a particular optional library in order to\nsucceed, you could decorate the test case with <a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.skipIf\" title=\"(in Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\">&#64;skipIf</code></a>. Then, the test runner will report that the test wasn't\nexecuted and why, instead of failing the test or omitting the test altogether.</p>\n<p>To supplement these test skipping behaviors, Django provides two\nadditional skip decorators. Instead of testing a generic boolean,\nthese decorators check the capabilities of the database, and skip the\ntest if the database doesn't support a specific named feature.</p>\n<p>The decorators use a string identifier to describe database features.\nThis string corresponds to attributes of the database connection\nfeatures class. See\n<a class=\"extlink-source reference external\" href=\"https://github.com/django/django/blob/stable/4.2.x/django/db/backends/base/features.py\">django.db.backends.base.features.BaseDatabaseFeatures class</a> for a full list of database features\nthat can be used as a basis for skipping tests.</p>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.test.skipIfDBFeature\">\n<span class=\"sig-name descname\">skipIfDBFeature</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">*</span>feature_name_strings</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.skipIfDBFeature\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Skip the decorated test or <code class=\"docutils literal notranslate\">TestCase</code> if all of the named database features\nare supported.</p>\n<p>For example, the following test will not be executed if the database\nsupports transactions (e.g., it would <em>not</em> run under PostgreSQL, but\nit would under MySQL with MyISAM tables):</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\">MyTests</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    <span class=\"nd\">@skipIfDBFeature</span><span class=\"p\">(</span><span class=\"s2\">&quot;supports_transactions&quot;</span><span class=\"p\">)</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_transaction_behavior</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"c1\"># ... conditional test code</span>\n        <span class=\"k\">pass</span>\n</code></pre></div>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.test.skipUnlessDBFeature\">\n<span class=\"sig-name descname\">skipUnlessDBFeature</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">*</span>feature_name_strings</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.skipUnlessDBFeature\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Skip the decorated test or <code class=\"docutils literal notranslate\">TestCase</code> if any of the named database features\nare <em>not</em> supported.</p>\n<p>For example, the following test will only be executed if the database\nsupports transactions (e.g., it would run under PostgreSQL, but <em>not</em>\nunder MySQL with MyISAM tables):</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\">MyTests</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    <span class=\"nd\">@skipUnlessDBFeature</span><span class=\"p\">(</span><span class=\"s2\">&quot;supports_transactions&quot;</span><span class=\"p\">)</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_transaction_behavior</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"c1\"># ... conditional test code</span>\n        <span class=\"k\">pass</span>\n</code></pre></div>\n</section>","rootId":"testing-tools","toc":[{"title":"テストクライアント","anchor":"the-test-client","children":[{"title":"概要と簡単な例","anchor":"overview-and-a-quick-example","children":[]},{"title":"リクエストの作成","anchor":"making-requests","children":[]},{"title":"レスポンスのテスト","anchor":"testing-responses","children":[]},{"title":"例外","anchor":"exceptions","children":[]},{"title":"永続的なステート","anchor":"persistent-state","children":[]},{"title":"言語の設定","anchor":"setting-the-language","children":[]},{"title":"カスタマイズ例","anchor":"example","children":[]}]},{"title":"提供されるテストケースのクラス","anchor":"provided-test-case-classes","children":[{"title":"SimpleTestCase","anchor":"simpletestcase","children":[]},{"title":"TransactionTestCase","anchor":"transactiontestcase","children":[]},{"title":"TestCase","anchor":"testcase","children":[]},{"title":"LiveServerTestCase","anchor":"liveservertestcase","children":[]}]},{"title":"テストケースの機能","anchor":"test-cases-features","children":[{"title":"デフォルトのテストクライアント","anchor":"default-test-client","children":[]},{"title":"テストクライアントをカスタムする","anchor":"customizing-the-test-client","children":[]},{"title":"フィクスチャーのロード","anchor":"fixture-loading","children":[]},{"title":"URLconf の設定","anchor":"urlconf-configuration","children":[]},{"title":"マルチデータベースのサポート","anchor":"multi-database-support","children":[]},{"title":"設定のオーバーライド","anchor":"overriding-settings","children":[]},{"title":"アプリの隔離","anchor":"isolating-apps","children":[]},{"title":"Emptying the test outbox","anchor":"emptying-the-test-outbox","children":[]},{"title":"アサーション","anchor":"assertions","children":[]},{"title":"Tagging tests","anchor":"tagging-tests","children":[]}]},{"title":"Testing asynchronous code","anchor":"testing-asynchronous-code","children":[]},{"title":"Email services","anchor":"email-services","children":[]},{"title":"管理コマンド","anchor":"management-commands","children":[]},{"title":"テストのスキップ","anchor":"skipping-tests","children":[]}],"breadcrumbs":[{"docname":"topics/index","title":"Django を使う","url":"/ja/4.2/topics/"},{"docname":"topics/testing/index","title":"Django におけるテスト","url":"/ja/4.2/topics/testing/"}],"prev":{"docname":"topics/testing/overview","title":"テストを書いて実行する","url":"/ja/4.2/topics/testing/overview/"},"next":{"docname":"topics/testing/advanced","title":"Advanced testing topics","url":"/ja/4.2/topics/testing/advanced/"},"formats":{"html":"/ja/4.2/topics/testing/tools/","markdown":"/ja/4.2/topics/testing/tools.md","json":"/ja/4.2/topics/testing/tools.json"},"source":"https://github.com/django/django/blob/stable/4.2.x/docs/topics/testing/tools.txt","official":"https://docs.djangoproject.com/ja/4.2/topics/testing/tools/","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","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}