{"title":"Menulis dan menjalankan percobaan","version":"2.1","locale":"id","docname":"topics/testing/overview","url":"/id/2.1/topics/testing/overview/","canonical":"https://djangodocs.dev/id/2.1/topics/testing/overview/","summary":"Lihat juga testing tutorial , the testing tools reference , dan advanced testing topics . Dokumen ini dipisah menjadi dua bagian utama. Pertama, kami menjelaskan…","html":"<span id=\"writing-and-running-tests\"></span><h1>Menulis dan menjalankan percobaan<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\">Lihat juga</p>\n<p><a class=\"reference internal\" href=\"/id/2.1/intro/tutorial05/\"><span class=\"doc\">testing tutorial</span></a>, the <a class=\"reference internal\" href=\"/id/2.1/topics/testing/tools/\"><span class=\"doc\">testing tools reference</span></a>, dan <a class=\"reference internal\" href=\"/id/2.1/topics/testing/advanced/\"><span class=\"doc\">advanced testing topics</span></a>.</p>\n</aside>\n<p>Dokumen ini dipisah menjadi dua bagian utama. Pertama, kami menjelaskan bagaimana menulis percobaan dengan Django. Kemudian, kami menjelaskan bagaimana menjalankan mereka.</p>\n<section id=\"writing-tests\">\n<h2>Menulis percobaan<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=\"(di 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=\"/id/2.1/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=\"(di 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<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 cases (that is, subclasses of\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.TestCase\" title=\"(di 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 cases, and run that suite.</p>\n<p>Untuk lebih rinci tentang <a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#module-unittest\" title=\"(di Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">unittest</span></code></a>, lihat dokumentasi Python.</p>\n<aside class=\"admonition-where-should-the-tests-live admonition\">\n<p class=\"admonition-title\">Dimana seharusnya percobaan berada?</p>\n<p>The default <a class=\"reference internal\" href=\"/id/2.1/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>Lihat juga <a class=\"reference internal\" href=\"/id/2.1/topics/testing/advanced/#testing-reusable-applications\"><span class=\"std std-ref\">Menggunakan penjalan percobaan Django untuk menjalankan aplikasi digunakan kembali</span></a>.</p>\n</aside>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Peringatan</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=\"/id/2.1/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=\"(di 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=\"(di 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>Menjalankan percobaan<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=\"/id/2.1/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=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ ./manage.py test\n</code></pre></div>\n<p>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=\"(di 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 &quot;test*.py&quot; 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=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code># Run all the tests in the animals.tests module\n$ ./manage.py test animals.tests\n\n# Run all the tests found within the &#39;animals&#39; package\n$ ./manage.py test animals\n\n# Run just one test case\n$ ./manage.py test animals.tests.AnimalTestCase\n\n# Run just one test method\n$ ./manage.py test animals.tests.AnimalTestCase.test_animals_can_speak\n</code></pre></div>\n<p>Anda dapat juga menyediakan jalur ke direktori untuk menemukan percobaan dibawah direktori itu:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ ./manage.py test animals/\n</code></pre></div>\n<p>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=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ ./manage.py test --pattern=&quot;tests_*.py&quot;\n</code></pre></div>\n<p>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=\"/id/2.1/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\">Percobaan dengan peringatan diadakan</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>Basisdata percobaan<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=\"/id/2.1/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>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=\"/id/2.1/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=\"/id/2.1/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=\"/id/2.1/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=\"/id/2.1/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=\"/id/2.1/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=\"/id/2.1/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=\"/id/2.1/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=\"/id/2.1/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=\"/id/2.1/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=\"/id/2.1/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=\"/id/2.1/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=\"/id/2.1/ref/settings/#std-setting-USER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USER</span></code></a>, so you'll\nneed to make sure that the given user account has sufficient privileges to\ncreate a new database on the system.</p>\n<p>For fine-grained control over the character encoding of your test\ndatabase, use the <a class=\"reference internal\" href=\"/id/2.1/ref/settings/#std-setting-TEST_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CHARSET</span></code></a> TEST option. If you're using\nMySQL, you can also use the <a class=\"reference internal\" href=\"/id/2.1/ref/settings/#std-setting-TEST_COLLATION\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">COLLATION</span></code></a> option to\ncontrol the particular collation used by the test database. See the\n<a class=\"reference internal\" href=\"/id/2.1/ref/settings/\"><span class=\"doc\">settings documentation</span></a> for details of these\nand other advanced settings.</p>\n<p>If using an SQLite in-memory database with 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\">Menemukan data dari basisdata produksi anda ketika menjalankan percobaan?</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>Ini juga berlaku pada penerapan disesuaikan dari <a class=\"reference internal\" href=\"/id/2.1/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\">Lihat juga</p>\n<p><a class=\"reference internal\" href=\"/id/2.1/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>Urutkan dimana percobaan dijalankan<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>Semua subkelas <a class=\"reference internal\" href=\"/id/2.1/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> berjalan pertama.</p></li>\n<li><p>Then, all other Django-based tests (test cases based on\n<a class=\"reference internal\" href=\"/id/2.1/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=\"/id/2.1/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=\"(di 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\">Catatan</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=\"/id/2.1/topics/testing/tools/#django.test.TransactionTestCase\" title=\"django.test.TransactionTestCase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">TransactionTestCase</span></code></a> test, they\nmust be updated to be able to run independently.</p>\n</aside>\n<p>You may reverse the execution order inside groups using the <a class=\"reference internal\" href=\"/id/2.1/ref/django-admin/#cmdoption-test-reverse\"><code class=\"xref std std-option docutils literal notranslate\"><span class=\"pre\">test</span>\n<span class=\"pre\">--reverse</span></code></a> option. This can help with ensuring your tests are independent from\neach other.</p>\n</section>\n<section id=\"rollback-emulation\">\n<span id=\"test-case-serialized-rollback\"></span><h3>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=\"/id/2.1/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=\"/id/2.1/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=\"/id/2.1/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=\"/id/2.1/ref/signals/#django.db.models.signals.post_migrate\" title=\"django.db.models.signals.post_migrate\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">post_migrate</span></code></a> signal when flushing the test\ndatabase.</p>\n</section>\n<section id=\"other-test-conditions\">\n<h3>Kondisi percobaan lain<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=\"/id/2.1/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=\"/id/2.1/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 &quot;manage.py test fooapp&quot; can\ninsert 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=\"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>Memahami keluaran percobaan<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=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">Creating</span> <span class=\"n\">test</span> <span class=\"n\">database</span><span class=\"o\">...</span>\n<span class=\"n\">Creating</span> <span class=\"n\">table</span> <span class=\"n\">myapp_animal</span>\n<span class=\"n\">Creating</span> <span class=\"n\">table</span> <span class=\"n\">myapp_mineral</span>\n</code></pre></div>\n<p>This tells you that the test runner is creating a test database, as described\nin the previous section.</p>\n<p>Sekali basisdata telah dibuat, Django akan menjalankan percobaan anda. Jika semuanya berjalan baik, anda akan melihat sesuatu seperti ini:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"o\">----------------------------------------------------------------------</span>\n<span class=\"n\">Ran</span> <span class=\"mi\">22</span> <span class=\"n\">tests</span> <span class=\"ow\">in</span> <span class=\"mf\">0.221</span><span class=\"n\">s</span>\n\n<span class=\"n\">OK</span>\n</code></pre></div>\n<p>Jika ada kegagalan percobaan, bagaimanapun, anda akan melihat rincian penuh tetnang percobaan mana yang gagal:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"o\">======================================================================</span>\n<span class=\"n\">FAIL</span><span class=\"p\">:</span> <span class=\"n\">test_was_published_recently_with_future_poll</span> <span class=\"p\">(</span><span class=\"n\">polls</span><span class=\"o\">.</span><span class=\"n\">tests</span><span class=\"o\">.</span><span class=\"n\">PollMethodTests</span><span class=\"p\">)</span>\n<span class=\"o\">----------------------------------------------------------------------</span>\n<span class=\"n\">Traceback</span> <span class=\"p\">(</span><span class=\"n\">most</span> <span class=\"n\">recent</span> <span class=\"n\">call</span> <span class=\"n\">last</span><span class=\"p\">):</span>\n  <span class=\"n\">File</span> <span class=\"s2\">&quot;/dev/mysite/polls/tests.py&quot;</span><span class=\"p\">,</span> <span class=\"n\">line</span> <span class=\"mi\">16</span><span class=\"p\">,</span> <span class=\"ow\">in</span> <span class=\"n\">test_was_published_recently_with_future_poll</span>\n    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertIs</span><span class=\"p\">(</span><span class=\"n\">future_poll</span><span class=\"o\">.</span><span class=\"n\">was_published_recently</span><span class=\"p\">(),</span> <span class=\"kc\">False</span><span class=\"p\">)</span>\n<span class=\"ne\">AssertionError</span><span class=\"p\">:</span> <span class=\"kc\">True</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"kc\">False</span>\n\n<span class=\"o\">----------------------------------------------------------------------</span>\n<span class=\"n\">Ran</span> <span class=\"mi\">1</span> <span class=\"n\">test</span> <span class=\"ow\">in</span> <span class=\"mf\">0.003</span><span class=\"n\">s</span>\n\n<span class=\"n\">FAILED</span> <span class=\"p\">(</span><span class=\"n\">failures</span><span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>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=\"(di 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 and erroneous tests. 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>Mempercepat percobaan<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>Menjalankan percobaan secara sejalan<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=\"/id/2.1/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>Campuran sandi<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=\"/id/2.1/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=\"s1\">&#39;django.contrib.auth.hashers.MD5PasswordHasher&#39;</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=\"/id/2.1/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=\"/id/2.1/ref/django-admin/#cmdoption-test-keepdb\"><code class=\"xref std std-option docutils literal notranslate\"><span class=\"pre\">test</span> <span class=\"pre\">--keepdb</span></code></a> option preserves the test database between test\nruns. It skips the create and destroy actions which can greatly decrease the\ntime to run tests.</p>\n</section>\n</section>\n</section>","rootId":"module-django.test","toc":[{"title":"Menulis percobaan","anchor":"writing-tests","children":[]},{"title":"Menjalankan percobaan","anchor":"running-tests","children":[{"title":"Basisdata percobaan","anchor":"the-test-database","children":[]},{"title":"Urutkan dimana percobaan dijalankan","anchor":"order-in-which-tests-are-executed","children":[]},{"title":"Rollback emulation","anchor":"rollback-emulation","children":[]},{"title":"Kondisi percobaan lain","anchor":"other-test-conditions","children":[]},{"title":"Memahami keluaran percobaan","anchor":"understanding-the-test-output","children":[]},{"title":"Mempercepat percobaan","anchor":"speeding-up-the-tests","children":[{"title":"Menjalankan percobaan secara sejalan","anchor":"running-tests-in-parallel","children":[]},{"title":"Campuran sandi","anchor":"password-hashing","children":[]},{"title":"Preserving the test database","anchor":"preserving-the-test-database","children":[]}]}]}],"breadcrumbs":[{"docname":"topics/index","title":"Menggunakan Django","url":"/id/2.1/topics/"},{"docname":"topics/testing/index","title":"Percobaan di Django","url":"/id/2.1/topics/testing/"}],"prev":{"docname":"topics/testing/index","title":"Percobaan di Django","url":"/id/2.1/topics/testing/"},"next":{"docname":"topics/testing/tools","title":"Alat percobaan","url":"/id/2.1/topics/testing/tools/"},"formats":{"html":"/id/2.1/topics/testing/overview/","markdown":"/id/2.1/topics/testing/overview.md","json":"/id/2.1/topics/testing/overview.json"},"source":"https://github.com/django/django/blob/stable/2.1.x/docs/topics/testing/overview.txt","official":"https://docs.djangoproject.com/id/2.1/topics/testing/overview/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}