{"title":"Writing and running tests","version":"6.0","locale":"ga","docname":"topics/testing/overview","url":"/ga/6.0/topics/testing/overview/","canonical":"https://djangodocs.dev/ga/6.0/topics/testing/overview/","summary":"See also The testing tutorial , the testing tools reference , and the advanced testing topics . This document is split into two primary sections. First, we explain…","html":"<span id=\"writing-and-running-tests\"></span><h1>Writing and running tests<a class=\"heading-anchor\" href=\"#module-django.test\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<aside class=\"admonition admonition-seealso\">\n<p class=\"admonition-title\">See also</p>\n<p>The <a class=\"reference internal\" href=\"/ga/6.0/intro/tutorial05/\"><span class=\"doc\">testing tutorial</span></a>, the <a class=\"reference internal\" href=\"/ga/6.0/topics/testing/tools/\"><span class=\"doc\">testing tools\nreference</span></a>, and the <a class=\"reference internal\" href=\"/ga/6.0/topics/testing/advanced/\"><span class=\"doc\">advanced testing topics</span></a>.</p>\n</aside>\n<p>This document is split into two primary sections. First, we explain how to\nwrite tests with Django. Then, we explain how to run them.</p>\n<section id=\"writing-tests\">\n<h2>Tástála scríbhneo<a class=\"heading-anchor\" href=\"#writing-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django's unit tests use a Python standard library module: <a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#module-unittest\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">unittest</span></code></a>. This\nmodule defines tests using a class-based approach.</p>\n<p>Here is an example which subclasses from <a class=\"reference internal\" href=\"/ga/6.0/topics/testing/tools/#django.test.TestCase\" title=\"django.test.TestCase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.test.TestCase</span></code></a>,\nwhich is a subclass of <a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.TestCase\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">unittest.TestCase</span></code></a> that runs each test inside a\ntransaction to provide isolation:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.test</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">TestCase</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">myapp.models</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Animal</span>\n\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">AnimalTestCase</span><span class=\"p\">(</span><span class=\"n\">TestCase</span><span class=\"p\">):</span>\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">setUp</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"n\">Animal</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">create</span><span class=\"p\">(</span><span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s2\">&quot;lion&quot;</span><span class=\"p\">,</span> <span class=\"n\">sound</span><span class=\"o\">=</span><span class=\"s2\">&quot;roar&quot;</span><span class=\"p\">)</span>\n        <span class=\"n\">Animal</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">create</span><span class=\"p\">(</span><span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s2\">&quot;cat&quot;</span><span class=\"p\">,</span> <span class=\"n\">sound</span><span class=\"o\">=</span><span class=\"s2\">&quot;meow&quot;</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">test_animals_can_speak</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;Animals that can speak are correctly identified&quot;&quot;&quot;</span>\n        <span class=\"n\">lion</span> <span class=\"o\">=</span> <span class=\"n\">Animal</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s2\">&quot;lion&quot;</span><span class=\"p\">)</span>\n        <span class=\"n\">cat</span> <span class=\"o\">=</span> <span class=\"n\">Animal</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s2\">&quot;cat&quot;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertEqual</span><span class=\"p\">(</span><span class=\"n\">lion</span><span class=\"o\">.</span><span class=\"n\">speak</span><span class=\"p\">(),</span> <span class=\"s1\">&#39;The lion says &quot;roar&quot;&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertEqual</span><span class=\"p\">(</span><span class=\"n\">cat</span><span class=\"o\">.</span><span class=\"n\">speak</span><span class=\"p\">(),</span> <span class=\"s1\">&#39;The cat says &quot;meow&quot;&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>When you <a class=\"reference internal\" href=\"#running-tests\"><span class=\"std std-ref\">run your tests</span></a>, the default behavior of the\ntest utility is to find all the test case classes (that is, subclasses of\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.TestCase\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">unittest.TestCase</span></code></a>) in any file whose name begins with <code class=\"docutils literal notranslate\"><span class=\"pre\">test</span></code>,\nautomatically build a test suite out of those test case classes, and run that\nsuite.</p>\n<p>For more details about <a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#module-unittest\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">unittest</span></code></a>, see the Python documentation.</p>\n<aside class=\"admonition-where-should-the-tests-live admonition\">\n<p class=\"admonition-title\">Where should the tests live?</p>\n<p>The default <a class=\"reference internal\" href=\"/ga/6.0/ref/django-admin/#django-admin-startapp\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">startapp</span></code></a> template creates a <code class=\"docutils literal notranslate\"><span class=\"pre\">tests.py</span></code> file in the\nnew application. This might be fine if you only have a few tests, but as\nyour test suite grows you'll likely want to restructure it into a tests\npackage so you can split your tests into different submodules such as\n<code class=\"docutils literal notranslate\"><span class=\"pre\">test_models.py</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">test_views.py</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">test_forms.py</span></code>, etc. Feel free to\npick whatever organizational scheme you like.</p>\n<p>See also <a class=\"reference internal\" href=\"/ga/6.0/topics/testing/advanced/#testing-reusable-applications\"><span class=\"std std-ref\">Using the Django test runner to test reusable applications</span></a>.</p>\n</aside>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Warning</p>\n<p>If your tests rely on database access such as creating or querying models,\nbe sure to create your test classes as subclasses of\n<a class=\"reference internal\" href=\"/ga/6.0/topics/testing/tools/#django.test.TestCase\" title=\"django.test.TestCase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.test.TestCase</span></code></a> rather than <a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.TestCase\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">unittest.TestCase</span></code></a>.</p>\n<p>Using <a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.TestCase\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">unittest.TestCase</span></code></a> avoids the cost of running each test in a\ntransaction and flushing the database, but if your tests interact with\nthe database their behavior will vary based on the order that the test\nrunner executes them. This can lead to unit tests that pass when run in\nisolation but fail when run in a suite.</p>\n</aside>\n</section>\n<section id=\"running-tests\">\n<span id=\"id1\"></span><h2>Rith tástálacha<a class=\"heading-anchor\" href=\"#running-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Once you've written tests, run them using the <a class=\"reference internal\" href=\"/ga/6.0/ref/django-admin/#django-admin-test\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">test</span></code></a> command of\nyour project's <code class=\"docutils literal notranslate\"><span class=\"pre\">manage.py</span></code> utility:</p>\n<div class=\"code-block\" data-language=\"shell\"><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=\"w\"> </span>./manage.py<span class=\"w\"> </span><span class=\"nb\">test</span>\n</code></pre></div>\n<p>Test discovery is based on the unittest module's <a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest-test-discovery\" title=\"(in Python v3.14)\"><span class=\"xref std std-ref\">built-in test\ndiscovery</span></a>. By default, this will discover tests in\nany file named <code class=\"docutils literal notranslate\"><span class=\"pre\">test*.py</span></code> under the current working directory.</p>\n<p>You can specify particular tests to run by supplying any number of &quot;test\nlabels&quot; to <code class=\"docutils literal notranslate\"><span class=\"pre\">./manage.py</span> <span class=\"pre\">test</span></code>. Each test label can be a full Python dotted\npath to a package, module, <code class=\"docutils literal notranslate\"><span class=\"pre\">TestCase</span></code> subclass, or test method. For instance:</p>\n<div class=\"code-block\" data-language=\"shell\"><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=\"c1\"># Run all the tests in the animals.tests module</span>\n$<span class=\"w\"> </span>./manage.py<span class=\"w\"> </span><span class=\"nb\">test</span><span class=\"w\"> </span>animals.tests\n\n<span class=\"c1\"># Run all the tests found within the &#39;animals&#39; package</span>\n$<span class=\"w\"> </span>./manage.py<span class=\"w\"> </span><span class=\"nb\">test</span><span class=\"w\"> </span>animals\n\n<span class=\"c1\"># Run just one test case class</span>\n$<span class=\"w\"> </span>./manage.py<span class=\"w\"> </span><span class=\"nb\">test</span><span class=\"w\"> </span>animals.tests.AnimalTestCase\n\n<span class=\"c1\"># Run just one test method</span>\n$<span class=\"w\"> </span>./manage.py<span class=\"w\"> </span><span class=\"nb\">test</span><span class=\"w\"> </span>animals.tests.AnimalTestCase.test_animals_can_speak\n</code></pre></div>\n<p>You can also provide a path to a directory to discover tests below that\ndirectory:</p>\n<div class=\"code-block\" data-language=\"shell\"><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=\"w\"> </span>./manage.py<span class=\"w\"> </span><span class=\"nb\">test</span><span class=\"w\"> </span>animals/\n</code></pre></div>\n<p>You can specify a custom filename pattern match using the <code class=\"docutils literal notranslate\"><span class=\"pre\">-p</span></code> (or\n<code class=\"docutils literal notranslate\"><span class=\"pre\">--pattern</span></code>) option, if your test files are named differently from the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">test*.py</span></code> pattern:</p>\n<div class=\"code-block\" data-language=\"shell\"><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=\"w\"> </span>./manage.py<span class=\"w\"> </span><span class=\"nb\">test</span><span class=\"w\"> </span>--pattern<span class=\"o\">=</span><span class=\"s2\">&quot;tests_*.py&quot;</span>\n</code></pre></div>\n<p>If you press <code class=\"docutils literal notranslate\"><span class=\"pre\">Ctrl-C</span></code> while the tests are running, the test runner will\nwait for the currently running test to complete and then exit gracefully.\nDuring a graceful exit the test runner will output details of any test\nfailures, report on how many tests were run and how many errors and failures\nwere encountered, and destroy any test databases as usual. Thus pressing\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Ctrl-C</span></code> can be very useful if you forget to pass the <a class=\"reference internal\" href=\"/ga/6.0/ref/django-admin/#cmdoption-test-failfast\"><code class=\"xref std std-option docutils literal notranslate\"><span class=\"pre\">--failfast</span></code></a> option, notice that some tests are unexpectedly failing and\nwant to get details on the failures without waiting for the full test run to\ncomplete.</p>\n<p>If you do not want to wait for the currently running test to finish, you\ncan press <code class=\"docutils literal notranslate\"><span class=\"pre\">Ctrl-C</span></code> a second time and the test run will halt immediately,\nbut not gracefully. No details of the tests run before the interruption will\nbe reported, and any test databases created by the run will not be destroyed.</p>\n<aside class=\"admonition-test-with-warnings-enabled admonition\">\n<p class=\"admonition-title\">Test with warnings enabled</p>\n<p>It's a good idea to run your tests with Python warnings enabled:\n<code class=\"docutils literal notranslate\"><span class=\"pre\">python</span> <span class=\"pre\">-Wa</span> <span class=\"pre\">manage.py</span> <span class=\"pre\">test</span></code>. The <code class=\"docutils literal notranslate\"><span class=\"pre\">-Wa</span></code> flag tells Python to\ndisplay deprecation warnings. Django, like many other Python libraries,\nuses these warnings to flag when features are going away. It also might\nflag areas in your code that aren't strictly wrong but could benefit\nfrom a better implementation.</p>\n</aside>\n<section id=\"the-test-database\">\n<span id=\"id2\"></span><h3>The test database<a class=\"heading-anchor\" href=\"#the-test-database\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Tests that require a database (namely, model tests) will not use your &quot;real&quot;\n(production) database. Separate, blank databases are created for the tests.</p>\n<p>Regardless of whether the tests pass or fail, the test databases are destroyed\nwhen all the tests have been executed.</p>\n<p>You can prevent the test databases from being destroyed by using the\n<a class=\"reference internal\" href=\"/ga/6.0/ref/django-admin/#cmdoption-test-keepdb\"><code class=\"xref std std-option docutils literal notranslate\"><span class=\"pre\">test</span> <span class=\"pre\">--keepdb</span></code></a> option. This will preserve the test database between\nruns. If the database does not exist, it will first be created. Any migrations\nwill also be applied in order to keep it up to date.</p>\n<p>As described in the previous section, if a test run is forcefully interrupted,\nthe test database may not be destroyed. On the next run, you'll be asked\nwhether you want to reuse or destroy the database. Use the <a class=\"reference internal\" href=\"/ga/6.0/ref/django-admin/#cmdoption-test-noinput\"><code class=\"xref std std-option docutils literal notranslate\"><span class=\"pre\">test</span>\n<span class=\"pre\">--noinput</span></code></a> option to suppress that prompt and automatically destroy the\ndatabase. This can be useful when running tests on a continuous integration\nserver where tests may be interrupted by a timeout, for example.</p>\n<p>The default test database names are created by prepending <code class=\"docutils literal notranslate\"><span class=\"pre\">test_</span></code> to the\nvalue of each <a class=\"reference internal\" href=\"/ga/6.0/ref/settings/#std-setting-NAME\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">NAME</span></code></a> in <a class=\"reference internal\" href=\"/ga/6.0/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DATABASES</span></code></a>. When using SQLite, the\ntests will use an in-memory database by default (i.e., the database will be\ncreated in memory, bypassing the filesystem entirely!). The <a class=\"reference internal\" href=\"/ga/6.0/ref/settings/#std-setting-DATABASE-TEST\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TEST</span></code></a> dictionary in <a class=\"reference internal\" href=\"/ga/6.0/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DATABASES</span></code></a> offers a number of settings\nto configure your test database. For example, if you want to use a different\ndatabase name, specify <a class=\"reference internal\" href=\"/ga/6.0/ref/settings/#std-setting-TEST_NAME\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">NAME</span></code></a> in the <a class=\"reference internal\" href=\"/ga/6.0/ref/settings/#std-setting-DATABASE-TEST\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TEST</span></code></a> dictionary for any given database in <a class=\"reference internal\" href=\"/ga/6.0/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DATABASES</span></code></a>.</p>\n<p>On PostgreSQL, <a class=\"reference internal\" href=\"/ga/6.0/ref/settings/#std-setting-USER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USER</span></code></a> will also need read access to the built-in\n<code class=\"docutils literal notranslate\"><span class=\"pre\">postgres</span></code> database.</p>\n<p>Aside from using a separate database, the test runner will otherwise\nuse all of the same database settings you have in your settings file:\n<a class=\"reference internal\" href=\"/ga/6.0/ref/settings/#std-setting-DATABASE-ENGINE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">ENGINE</span></code></a>, <a class=\"reference internal\" href=\"/ga/6.0/ref/settings/#std-setting-USER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USER</span></code></a>, <a class=\"reference internal\" href=\"/ga/6.0/ref/settings/#std-setting-HOST\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">HOST</span></code></a>, etc. The\ntest database is created by the user specified by <a class=\"reference internal\" href=\"/ga/6.0/ref/settings/#std-setting-USER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USER</span></code></a>, so you'll\nneed to make sure that the given user account has sufficient privileges to\ncreate a new database on the system.</p>\n<p>For fine-grained control over the character encoding of your test database, use\nthe <a class=\"reference internal\" href=\"/ga/6.0/ref/settings/#std-setting-TEST_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CHARSET</span></code></a> TEST option. If you're using MySQL, you\ncan also use the <a class=\"reference internal\" href=\"/ga/6.0/ref/settings/#std-setting-TEST_COLLATION\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">COLLATION</span></code></a> option to control the\nparticular collation used by the test database. See the <a class=\"reference internal\" href=\"/ga/6.0/ref/settings/\"><span class=\"doc\">settings\ndocumentation</span></a> for details of these and other advanced\nsettings.</p>\n<p>If using an SQLite in-memory database with SQLite, <a class=\"reference external\" href=\"https://www.sqlite.org/sharedcache.html\">shared cache</a> is enabled, so you can write tests\nwith ability to share the database between threads.</p>\n<aside class=\"admonition-finding-data-from-your-production-database-when-running-tests admonition\">\n<p class=\"admonition-title\">Finding data from your production database when running tests?</p>\n<p>If your code attempts to access the database when its modules are compiled,\nthis will occur <em>before</em> the test database is set up, with potentially\nunexpected results. For example, if you have a database query in\nmodule-level code and a real database exists, production data could pollute\nyour tests. <em>It is a bad idea to have such import-time database queries in\nyour code</em> anyway - rewrite your code so that it doesn't do this.</p>\n<p>This also applies to customized implementations of\n<a class=\"reference internal\" href=\"/ga/6.0/ref/applications/#django.apps.AppConfig.ready\" title=\"django.apps.AppConfig.ready\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">ready()</span></code></a>.</p>\n</aside>\n<aside class=\"admonition admonition-seealso\">\n<p class=\"admonition-title\">See also</p>\n<p>The <a class=\"reference internal\" href=\"/ga/6.0/topics/testing/advanced/#topics-testing-advanced-multidb\"><span class=\"std std-ref\">advanced multi-db testing topics</span></a>.</p>\n</aside>\n</section>\n<section id=\"order-in-which-tests-are-executed\">\n<span id=\"order-of-tests\"></span><h3>Order in which tests are executed<a class=\"heading-anchor\" href=\"#order-in-which-tests-are-executed\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>In order to guarantee that all <code class=\"docutils literal notranslate\"><span class=\"pre\">TestCase</span></code> code starts with a clean database,\nthe Django test runner reorders tests in the following way:</p>\n<ul class=\"simple\">\n<li><p>All <a class=\"reference internal\" href=\"/ga/6.0/topics/testing/tools/#django.test.TestCase\" title=\"django.test.TestCase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">TestCase</span></code></a> subclasses are run first.</p></li>\n<li><p>Then, all other Django-based tests (test case classes based on\n<a class=\"reference internal\" href=\"/ga/6.0/topics/testing/tools/#django.test.SimpleTestCase\" title=\"django.test.SimpleTestCase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">SimpleTestCase</span></code></a>, including\n<a class=\"reference internal\" href=\"/ga/6.0/topics/testing/tools/#django.test.TransactionTestCase\" title=\"django.test.TransactionTestCase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">TransactionTestCase</span></code></a>) are run with no particular\nordering guaranteed nor enforced among them.</p></li>\n<li><p>Then any other <a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.TestCase\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">unittest.TestCase</span></code></a> tests (including doctests) that may\nalter the database without restoring it to its original state are run.</p></li>\n</ul>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>The new ordering of tests may reveal unexpected dependencies on test case\nordering. This is the case with doctests that relied on state left in the\ndatabase by a given <a class=\"reference internal\" href=\"/ga/6.0/topics/testing/tools/#django.test.TransactionTestCase\" title=\"django.test.TransactionTestCase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">TransactionTestCase</span></code></a> test, they\nmust be updated to be able to run independently.</p>\n</aside>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>Failures detected when loading tests are ordered before all of the above\nfor quicker feedback. This includes things like test modules that couldn't\nbe found or that couldn't be loaded due to syntax errors.</p>\n</aside>\n<p>You may randomize and/or reverse the execution order inside groups using the\n<a class=\"reference internal\" href=\"/ga/6.0/ref/django-admin/#cmdoption-test-shuffle\"><code class=\"xref std std-option docutils literal notranslate\"><span class=\"pre\">test</span> <span class=\"pre\">--shuffle</span></code></a> and <a class=\"reference internal\" href=\"/ga/6.0/ref/django-admin/#cmdoption-test-reverse\"><code class=\"xref std std-option docutils literal notranslate\"><span class=\"pre\">--reverse</span></code></a> options. This\ncan help with ensuring your tests are independent from each other.</p>\n</section>\n<section id=\"rollback-emulation\">\n<span id=\"test-case-serialized-rollback\"></span><h3>Rollback emulation<a class=\"heading-anchor\" href=\"#rollback-emulation\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Any initial data loaded in migrations will only be available in <code class=\"docutils literal notranslate\"><span class=\"pre\">TestCase</span></code>\ntests and not in <code class=\"docutils literal notranslate\"><span class=\"pre\">TransactionTestCase</span></code> tests, and additionally only on\nbackends where transactions are supported (the most important exception being\nMyISAM). This is also true for tests which rely on <code class=\"docutils literal notranslate\"><span class=\"pre\">TransactionTestCase</span></code>\nsuch as <a class=\"reference internal\" href=\"/ga/6.0/topics/testing/tools/#django.test.LiveServerTestCase\" title=\"django.test.LiveServerTestCase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">LiveServerTestCase</span></code></a> and\n<a class=\"reference internal\" href=\"/ga/6.0/ref/contrib/staticfiles/#django.contrib.staticfiles.testing.StaticLiveServerTestCase\" title=\"django.contrib.staticfiles.testing.StaticLiveServerTestCase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">StaticLiveServerTestCase</span></code></a>.</p>\n<p>Django can reload that data for you on a per-testcase basis by\nsetting the <code class=\"docutils literal notranslate\"><span class=\"pre\">serialized_rollback</span></code> option to <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> in the body of the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">TestCase</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">TransactionTestCase</span></code>, but note that this will slow down\nthat test suite by approximately 3x.</p>\n<p>Third-party apps or those developing against MyISAM will need to set this;\nin general, however, you should be developing your own projects against a\ntransactional database and be using <code class=\"docutils literal notranslate\"><span class=\"pre\">TestCase</span></code> for most tests, and thus\nnot need this setting.</p>\n<p>The initial serialization is usually very quick, but if you wish to exclude\nsome apps from this process (and speed up test runs slightly), you may add\nthose apps to <a class=\"reference internal\" href=\"/ga/6.0/ref/settings/#std-setting-TEST_NON_SERIALIZED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TEST_NON_SERIALIZED_APPS</span></code></a>.</p>\n<p>To prevent serialized data from being loaded twice, setting\n<code class=\"docutils literal notranslate\"><span class=\"pre\">serialized_rollback=True</span></code> disables the\n<a class=\"reference internal\" href=\"/ga/6.0/ref/signals/#django.db.models.signals.post_migrate\" title=\"django.db.models.signals.post_migrate\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">post_migrate</span></code></a> signal when flushing the test\ndatabase.</p>\n<aside class=\"version-note version-changed\" data-version=\"5.2\">\n<p class=\"version-note-title\">Changed in Django 5.2</p><p>For <a class=\"reference internal\" href=\"/ga/6.0/topics/testing/tools/#django.test.TransactionTestCase\" title=\"django.test.TransactionTestCase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">TransactionTestCase</span></code></a>, serialized migration data is made\navailable during <code class=\"docutils literal notranslate\"><span class=\"pre\">setUpClass()</span></code>.</p>\n</aside>\n</section>\n<section id=\"other-test-conditions\">\n<h3>Other test conditions<a class=\"heading-anchor\" href=\"#other-test-conditions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Regardless of the value of the <a class=\"reference internal\" href=\"/ga/6.0/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEBUG</span></code></a> setting in your configuration\nfile, all Django tests run with <a class=\"reference internal\" href=\"/ga/6.0/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEBUG</span></code></a>=False. This is to ensure that\nthe observed output of your code matches what will be seen in a production\nsetting.</p>\n<p>Caches are not cleared after each test, and running <code class=\"docutils literal notranslate\"><span class=\"pre\">manage.py</span> <span class=\"pre\">test</span> <span class=\"pre\">fooapp</span></code>\ncan insert data from the tests into the cache of a live system if you run your\ntests in production because, unlike databases, a separate &quot;test cache&quot; is not\nused. This behavior <a class=\"extlink-ticket reference external\" href=\"https://code.djangoproject.com/ticket/11505\">may change</a> in the future.</p>\n</section>\n<section id=\"understanding-the-test-output\">\n<h3>Understanding the test output<a class=\"heading-anchor\" href=\"#understanding-the-test-output\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>When you run your tests, you'll see a number of messages as the test runner\nprepares itself. You can control the level of detail of these messages with the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">verbosity</span></code> option on the command line:</p>\n<div class=\"code-block\" data-language=\"shell\"><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>Creating<span class=\"w\"> </span><span class=\"nb\">test</span><span class=\"w\"> </span>database...\nCreating<span class=\"w\"> </span>table<span class=\"w\"> </span>myapp_animal\nCreating<span class=\"w\"> </span>table<span class=\"w\"> </span>myapp_mineral\n</code></pre></div>\n<p>This tells you that the test runner is creating a test database, as described\nin the previous section.</p>\n<p>Once the test database has been created, Django will run your tests.\nIf everything goes well, you'll see something like this:</p>\n<div class=\"code-block\" data-language=\"shell\"><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>----------------------------------------------------------------------\nRan<span class=\"w\"> </span><span class=\"m\">22</span><span class=\"w\"> </span>tests<span class=\"w\"> </span><span class=\"k\">in</span><span class=\"w\"> </span><span class=\"m\">0</span>.221s\n\nOK\n</code></pre></div>\n<p>If there are test failures, however, you'll see full details about which tests\nfailed:</p>\n<div class=\"code-block\" data-language=\"shell\"><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=\"o\">======================================================================</span>\nFAIL:<span class=\"w\"> </span>test_was_published_recently_with_future_poll<span class=\"w\"> </span><span class=\"o\">(</span>polls.tests.PollMethodTests<span class=\"o\">)</span>\n----------------------------------------------------------------------\nTraceback<span class=\"w\"> </span><span class=\"o\">(</span>most<span class=\"w\"> </span>recent<span class=\"w\"> </span>call<span class=\"w\"> </span>last<span class=\"o\">)</span>:\n<span class=\"w\">  </span>File<span class=\"w\"> </span><span class=\"s2\">&quot;/dev/mysite/polls/tests.py&quot;</span>,<span class=\"w\"> </span>line<span class=\"w\"> </span><span class=\"m\">16</span>,<span class=\"w\"> </span><span class=\"k\">in</span><span class=\"w\"> </span>test_was_published_recently_with_future_poll\n<span class=\"w\">    </span>self.assertIs<span class=\"o\">(</span>future_poll.was_published_recently<span class=\"o\">()</span>,<span class=\"w\"> </span>False<span class=\"o\">)</span>\nAssertionError:<span class=\"w\"> </span>True<span class=\"w\"> </span>is<span class=\"w\"> </span>not<span class=\"w\"> </span>False\n\n----------------------------------------------------------------------\nRan<span class=\"w\"> </span><span class=\"m\">1</span><span class=\"w\"> </span><span class=\"nb\">test</span><span class=\"w\"> </span><span class=\"k\">in</span><span class=\"w\"> </span><span class=\"m\">0</span>.003s\n\nFAILED<span class=\"w\"> </span><span class=\"o\">(</span><span class=\"nv\">failures</span><span class=\"o\">=</span><span class=\"m\">1</span><span class=\"o\">)</span>\n</code></pre></div>\n<p>A full explanation of this error output is beyond the scope of this document,\nbut it's pretty intuitive. You can consult the documentation of Python's\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#module-unittest\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">unittest</span></code></a> library for details.</p>\n<p>Note that the return code for the test-runner script is 1 for any number of\nfailed tests (whether the failure was caused by an error, a failed assertion,\nor an unexpected success). If all the tests pass, the return code is 0. This\nfeature is useful if you're using the test-runner script in a shell script and\nneed to test for success or failure at that level.</p>\n</section>\n<section id=\"speeding-up-the-tests\">\n<span id=\"speeding-up-tests-auth-hashers\"></span><h3>Speeding up the tests<a class=\"heading-anchor\" href=\"#speeding-up-the-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<section id=\"running-tests-in-parallel\">\n<h4>Tástálacha reatha go comhthreomhar<a class=\"heading-anchor\" href=\"#running-tests-in-parallel\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>As long as your tests are properly isolated, you can run them in parallel to\ngain a speed up on multi-core hardware. See <a class=\"reference internal\" href=\"/ga/6.0/ref/django-admin/#cmdoption-test-parallel\"><code class=\"xref std std-option docutils literal notranslate\"><span class=\"pre\">test</span> <span class=\"pre\">--parallel</span></code></a>.</p>\n</section>\n<section id=\"password-hashing\">\n<h4>Password hashing<a class=\"heading-anchor\" href=\"#password-hashing\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>The default password hasher is rather slow by design. If you're authenticating\nmany users in your tests, you may want to use a custom settings file and set\nthe <a class=\"reference internal\" href=\"/ga/6.0/ref/settings/#std-setting-PASSWORD_HASHERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">PASSWORD_HASHERS</span></code></a> setting to a faster hashing algorithm:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">PASSWORD_HASHERS</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"s2\">&quot;django.contrib.auth.hashers.MD5PasswordHasher&quot;</span><span class=\"p\">,</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>Don't forget to also include in <a class=\"reference internal\" href=\"/ga/6.0/ref/settings/#std-setting-PASSWORD_HASHERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">PASSWORD_HASHERS</span></code></a> any hashing\nalgorithm used in fixtures, if any.</p>\n</section>\n<section id=\"preserving-the-test-database\">\n<h4>Preserving the test database<a class=\"heading-anchor\" href=\"#preserving-the-test-database\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>The <a class=\"reference internal\" href=\"/ga/6.0/ref/django-admin/#cmdoption-test-keepdb\"><code class=\"xref std std-option docutils literal notranslate\"><span class=\"pre\">test</span> <span class=\"pre\">--keepdb</span></code></a> option preserves the test database between test\nruns. It skips the create and destroy actions which can greatly decrease the\ntime to run tests.</p>\n</section>\n<section id=\"avoiding-disk-access-for-media-files\">\n<h4>Avoiding disk access for media files<a class=\"heading-anchor\" href=\"#avoiding-disk-access-for-media-files\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>The <a class=\"reference internal\" href=\"/ga/6.0/ref/files/storage/#django.core.files.storage.InMemoryStorage\" title=\"django.core.files.storage.InMemoryStorage\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">InMemoryStorage</span></code></a> is a convenient way to\nprevent disk access for media files. All data is kept in memory, then it gets\ndiscarded after tests run.</p>\n</section>\n</section>\n</section>","rootId":"module-django.test","toc":[{"title":"Tástála scríbhneo","anchor":"writing-tests","children":[]},{"title":"Rith tástálacha","anchor":"running-tests","children":[{"title":"The test database","anchor":"the-test-database","children":[]},{"title":"Order in which tests are executed","anchor":"order-in-which-tests-are-executed","children":[]},{"title":"Rollback emulation","anchor":"rollback-emulation","children":[]},{"title":"Other test conditions","anchor":"other-test-conditions","children":[]},{"title":"Understanding the test output","anchor":"understanding-the-test-output","children":[]},{"title":"Speeding up the tests","anchor":"speeding-up-the-tests","children":[{"title":"Tástálacha reatha go comhthreomhar","anchor":"running-tests-in-parallel","children":[]},{"title":"Password hashing","anchor":"password-hashing","children":[]},{"title":"Preserving the test database","anchor":"preserving-the-test-database","children":[]},{"title":"Avoiding disk access for media files","anchor":"avoiding-disk-access-for-media-files","children":[]}]}]}],"breadcrumbs":[{"docname":"topics/index","title":"Using Django","url":"/ga/6.0/topics/"},{"docname":"topics/testing/index","title":"Testing in Django","url":"/ga/6.0/topics/testing/"}],"prev":{"docname":"topics/testing/index","title":"Testing in Django","url":"/ga/6.0/topics/testing/"},"next":{"docname":"topics/testing/tools","title":"Testing tools","url":"/ga/6.0/topics/testing/tools/"},"formats":{"html":"/ga/6.0/topics/testing/overview/","markdown":"/ga/6.0/topics/testing/overview.md","json":"/ga/6.0/topics/testing/overview.json"},"source":"https://github.com/django/django/blob/stable/6.0.x/docs/topics/testing/overview.txt","official":"https://docs.djangoproject.com/ga/6.0/topics/testing/overview/","inVersions":["6.1","6.0","5.2"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}