{"title":"Menulis dan menjalankan percobaan","version":"4.2","locale":"id","docname":"topics/testing/overview","url":"/id/4.2/topics/testing/overview/","canonical":"https://djangodocs.dev/id/4.2/topics/testing/overview/","summary":"Dokumen ini dipisah menjadi dua bagian utama. Pertama, kami menjelaskan bagaimana menulis percobaan dengan Django.","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\" role=\"note\">\n<p class=\"admonition-title\">Lihat juga</p>\n<p><a class=\"reference internal\" href=\"/id/4.2/intro/tutorial05/\"><span class=\"doc\">testing tutorial</span></a>, the <a class=\"reference internal\" href=\"/id/4.2/topics/testing/tools/\"><span class=\"doc\">testing tools reference</span></a>, dan <a class=\"reference internal\" href=\"/id/4.2/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\">unittest</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/4.2/topics/testing/tools/#django.test.TestCase\" title=\"django.test.TestCase\"><code class=\"xref py py-class docutils literal notranslate\">django.test.TestCase</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\">unittest.TestCase</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=\"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    <span class=\"k\">def</span> <span class=\"nf\">setUp</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        Animal<span class=\"o\">.</span>objects<span class=\"o\">.</span>create<span class=\"p\">(</span>name<span class=\"o\">=</span><span class=\"s2\">&quot;lion&quot;</span><span class=\"p\">,</span> sound<span class=\"o\">=</span><span class=\"s2\">&quot;roar&quot;</span><span class=\"p\">)</span>\n        Animal<span class=\"o\">.</span>objects<span class=\"o\">.</span>create<span class=\"p\">(</span>name<span class=\"o\">=</span><span class=\"s2\">&quot;cat&quot;</span><span class=\"p\">,</span> sound<span class=\"o\">=</span><span class=\"s2\">&quot;meow&quot;</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</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=\"sd\">&quot;&quot;&quot;Animals that can speak are correctly identified&quot;&quot;&quot;</span>\n        lion <span class=\"o\">=</span> Animal<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span>name<span class=\"o\">=</span><span class=\"s2\">&quot;lion&quot;</span><span class=\"p\">)</span>\n        cat <span class=\"o\">=</span> Animal<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span>name<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>assertEqual<span class=\"p\">(</span>lion<span class=\"o\">.</span>speak<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>assertEqual<span class=\"p\">(</span>cat<span class=\"o\">.</span>speak<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\">unittest.TestCase</code></a>) in any file whose name begins with <code class=\"docutils literal notranslate\">test</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\">unittest</code></a>, lihat dokumentasi Python.</p>\n<aside class=\"admonition-where-should-the-tests-live admonition\" role=\"note\">\n<p class=\"admonition-title\">Dimana seharusnya percobaan berada?</p>\n<p>Cetakan awalan <a class=\"reference internal\" href=\"/id/4.2/ref/django-admin/#django-admin-startapp\"><code class=\"xref std std-djadmin docutils literal notranslate\">startapp</code></a> membuat file <code class=\"docutils literal notranslate\">tests.py</code> di aplikasi baru. Ini mungkin baik-baik saja jika anda hanya memiliki beberapa pengujian, tetapi saat rangkaian pengujian anda berkembang, anda mungkin ingin menyusunnya kembali menjadi paket pengujian sehingga anda dapat membagi pengujian anda menjadi submodul yang berbeda seperti <code class=\"docutils literal notranslate\">test_models.py</code>, ` test_views.py`, <code class=\"docutils literal notranslate\">test_forms.py</code>, dll. Silakan pilih skema organisasi apa pun yang anda suka.</p>\n<p>Lihat juga <a class=\"reference internal\" href=\"/id/4.2/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/4.2/topics/testing/tools/#django.test.TestCase\" title=\"django.test.TestCase\"><code class=\"xref py py-class docutils literal notranslate\">django.test.TestCase</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\">unittest.TestCase</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\">unittest.TestCase</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>Setelah anda menulis pengujian, jalankan dengan menggunakan perintah <a class=\"reference internal\" href=\"/id/4.2/ref/django-admin/#django-admin-test\"><code class=\"xref std std-djadmin docutils literal notranslate\">test</code></a> dari utilitas <code class=\"docutils literal notranslate\">manage.py</code> proyek anda:</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>$ ./manage.py <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=\"(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 <code class=\"docutils literal notranslate\">test*.py</code> under the current working directory.</p>\n<p>Anda dapat menentukan pengujian tertentu untuk dijalankan dengan menyediakan sejumlah &quot;label pengujian&quot; ke <code class=\"docutils literal notranslate\">./manage.py test</code>. Setiap label pengujian bisa berupa jalur bertitik penuh Python ke paket, modul, subkelas <code class=\"docutils literal notranslate\">TestCase</code>, atau metode pengujian. Contohnya:</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$ ./manage.py <span class=\"nb\">test</span> animals.tests\n\n<span class=\"c1\"># Run all the tests found within the &#39;animals&#39; package</span>\n$ ./manage.py <span class=\"nb\">test</span> animals\n\n<span class=\"c1\"># Run just one test case</span>\n$ ./manage.py <span class=\"nb\">test</span> animals.tests.AnimalTestCase\n\n<span class=\"c1\"># Run just one test method</span>\n$ ./manage.py <span class=\"nb\">test</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>$ ./manage.py <span class=\"nb\">test</span> animals/\n</code></pre></div>\n<p>Anda dapat menentukan kecocokan pola nama berkas penyesuaian menggunakan pilihan <code class=\"docutils literal notranslate\">-p</code> (atau <code class=\"docutils literal notranslate\">--pattern</code>), jika file pengujian anda dinamai berbeda dari pola <code class=\"docutils literal notranslate\">test*.py</code>:</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>$ ./manage.py <span class=\"nb\">test</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\">Ctrl-C</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\">Ctrl-C</code> can be very useful if you forget to pass the <a class=\"reference internal\" href=\"/id/4.2/ref/django-admin/#cmdoption-test-failfast\"><code class=\"xref std std-option docutils literal notranslate\">--failfast</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\">Ctrl-C</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\" role=\"note\">\n<p class=\"admonition-title\">Percobaan dengan peringatan diadakan</p>\n<p>Sebaiknya jalankan pengujian anda dengan peringatan Python diaktifkan: <code class=\"docutils literal notranslate\">python -Wa manage.py test</code>. Bendera <code class=\"docutils literal notranslate\">-Wa</code> memberi tahu Python untuk menampilkan peringatan penghentian. Django, seperti banyak pustaka Python lainnya, menggunakan peringatan ini untuk menandai ketika fitur akan hilang. Itu juga mungkin menandai area dalam kode anda yang tidak sepenuhnya salah tetapi bisa mendapat manfaat dari implementasi yang lebih baik.</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/4.2/ref/django-admin/#cmdoption-test-keepdb\"><code class=\"xref std std-option docutils literal notranslate\">test --keepdb</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=\"/id/4.2/ref/django-admin/#cmdoption-test-noinput\"><code class=\"xref std std-option docutils literal notranslate\">test\n--noinput</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\">test_</code> to the\nvalue of each <a class=\"reference internal\" href=\"/id/4.2/ref/settings/#std-setting-NAME\"><code class=\"xref std std-setting docutils literal notranslate\">NAME</code></a> in <a class=\"reference internal\" href=\"/id/4.2/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\">DATABASES</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/4.2/ref/settings/#std-setting-DATABASE-TEST\"><code class=\"xref std std-setting docutils literal notranslate\">TEST</code></a> dictionary in <a class=\"reference internal\" href=\"/id/4.2/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\">DATABASES</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/4.2/ref/settings/#std-setting-TEST_NAME\"><code class=\"xref std std-setting docutils literal notranslate\">NAME</code></a> in the <a class=\"reference internal\" href=\"/id/4.2/ref/settings/#std-setting-DATABASE-TEST\"><code class=\"xref std std-setting docutils literal notranslate\">TEST</code></a> dictionary for any given database in <a class=\"reference internal\" href=\"/id/4.2/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\">DATABASES</code></a>.</p>\n<p>On PostgreSQL, <a class=\"reference internal\" href=\"/id/4.2/ref/settings/#std-setting-USER\"><code class=\"xref std std-setting docutils literal notranslate\">USER</code></a> will also need read access to the built-in\n<code class=\"docutils literal notranslate\">postgres</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/4.2/ref/settings/#std-setting-DATABASE-ENGINE\"><code class=\"xref std std-setting docutils literal notranslate\">ENGINE</code></a>, <a class=\"reference internal\" href=\"/id/4.2/ref/settings/#std-setting-USER\"><code class=\"xref std std-setting docutils literal notranslate\">USER</code></a>, <a class=\"reference internal\" href=\"/id/4.2/ref/settings/#std-setting-HOST\"><code class=\"xref std std-setting docutils literal notranslate\">HOST</code></a>, etc. The\ntest database is created by the user specified by <a class=\"reference internal\" href=\"/id/4.2/ref/settings/#std-setting-USER\"><code class=\"xref std std-setting docutils literal notranslate\">USER</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/4.2/ref/settings/#std-setting-TEST_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\">CHARSET</code></a> TEST option. If you're using\nMySQL, you can also use the <a class=\"reference internal\" href=\"/id/4.2/ref/settings/#std-setting-TEST_COLLATION\"><code class=\"xref std std-setting docutils literal notranslate\">COLLATION</code></a> option to\ncontrol the particular collation used by the test database. See the\n<a class=\"reference internal\" href=\"/id/4.2/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\" role=\"note\">\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/4.2/ref/applications/#django.apps.AppConfig.ready\" title=\"django.apps.AppConfig.ready\"><code class=\"xref py py-meth docutils literal notranslate\">ready()</code></a>.</p>\n</aside>\n<aside class=\"admonition admonition-seealso\" role=\"note\">\n<p class=\"admonition-title\">Lihat juga</p>\n<p><a class=\"reference internal\" href=\"/id/4.2/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\">TestCase</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/4.2/topics/testing/tools/#django.test.TestCase\" title=\"django.test.TestCase\"><code class=\"xref py py-class docutils literal notranslate\">TestCase</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/4.2/topics/testing/tools/#django.test.SimpleTestCase\" title=\"django.test.SimpleTestCase\"><code class=\"xref py py-class docutils literal notranslate\">SimpleTestCase</code></a>, including\n<a class=\"reference internal\" href=\"/id/4.2/topics/testing/tools/#django.test.TransactionTestCase\" title=\"django.test.TransactionTestCase\"><code class=\"xref py py-class docutils literal notranslate\">TransactionTestCase</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\">unittest.TestCase</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/4.2/topics/testing/tools/#django.test.TransactionTestCase\" title=\"django.test.TransactionTestCase\"><code class=\"xref py py-class docutils literal notranslate\">TransactionTestCase</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\">Catatan</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=\"/id/4.2/ref/django-admin/#cmdoption-test-shuffle\"><code class=\"xref std std-option docutils literal notranslate\">test --shuffle</code></a> and <a class=\"reference internal\" href=\"/id/4.2/ref/django-admin/#cmdoption-test-reverse\"><code class=\"xref std std-option docutils literal notranslate\">--reverse</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\">TestCase</code>\ntests and not in <code class=\"docutils literal notranslate\">TransactionTestCase</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\">TransactionTestCase</code>\nsuch as <a class=\"reference internal\" href=\"/id/4.2/topics/testing/tools/#django.test.LiveServerTestCase\" title=\"django.test.LiveServerTestCase\"><code class=\"xref py py-class docutils literal notranslate\">LiveServerTestCase</code></a> and\n<a class=\"reference internal\" href=\"/id/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>.</p>\n<p>Django can reload that data for you on a per-testcase basis by\nsetting the <code class=\"docutils literal notranslate\">serialized_rollback</code> option to <code class=\"docutils literal notranslate\">True</code> in the body of the\n<code class=\"docutils literal notranslate\">TestCase</code> or <code class=\"docutils literal notranslate\">TransactionTestCase</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\">TestCase</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/4.2/ref/settings/#std-setting-TEST_NON_SERIALIZED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">TEST_NON_SERIALIZED_APPS</code></a>.</p>\n<p>To prevent serialized data from being loaded twice, setting\n<code class=\"docutils literal notranslate\">serialized_rollback=True</code> disables the\n<a class=\"reference internal\" href=\"/id/4.2/ref/signals/#django.db.models.signals.post_migrate\" title=\"django.db.models.signals.post_migrate\"><code class=\"xref py py-data docutils literal notranslate\">post_migrate</code></a> signal 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/4.2/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\">DEBUG</code></a> setting in your configuration\nfile, all Django tests run with <a class=\"reference internal\" href=\"/id/4.2/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\">DEBUG</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>Cache tidak dihapus setelah setiap pengujian, dan menjalankan <code class=\"docutils literal notranslate\">manage.py test fooapp</code> dapat memasukkan data dari pengujian ke dalam cache sistem langsung jika anda menjalankan pengujian dalam produksi karena, tidak seperti basis data, &quot;cache pengujian&quot; terpisah tidak digunakan. Perilaku ini <a class=\"extlink-ticket reference external\" href=\"https://code.djangoproject.com/ticket/11505\">may change</a> di masa datang.</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\">verbosity</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=\"nb\">test</span> database...\nCreating table myapp_animal\nCreating table 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=\"m\">22</span> tests <span class=\"k\">in</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: test_was_published_recently_with_future_poll <span class=\"o\">(</span>polls.tests.PollMethodTests<span class=\"o\">)</span>\n----------------------------------------------------------------------\nTraceback <span class=\"o\">(</span>most recent call last<span class=\"o\">)</span>:\n  File <span class=\"s2\">&quot;/dev/mysite/polls/tests.py&quot;</span>, line <span class=\"m\">16</span>, <span class=\"k\">in</span> test_was_published_recently_with_future_poll\n    self.assertIs<span class=\"o\">(</span>future_poll.was_published_recently<span class=\"o\">()</span>, False<span class=\"o\">)</span>\nAssertionError: True is not False\n\n----------------------------------------------------------------------\nRan <span class=\"m\">1</span> <span class=\"nb\">test</span> <span class=\"k\">in</span> <span class=\"m\">0</span>.003s\n\nFAILED <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=\"(di Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\">unittest</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<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, the return code was 0 for unexpected successes.</p>\n</aside>\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/4.2/ref/django-admin/#cmdoption-test-parallel\"><code class=\"xref std std-option docutils literal notranslate\">test --parallel</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/4.2/ref/settings/#std-setting-PASSWORD_HASHERS\"><code class=\"xref std std-setting docutils literal notranslate\">PASSWORD_HASHERS</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>PASSWORD_HASHERS <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=\"/id/4.2/ref/settings/#std-setting-PASSWORD_HASHERS\"><code class=\"xref std std-setting docutils literal notranslate\">PASSWORD_HASHERS</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/4.2/ref/django-admin/#cmdoption-test-keepdb\"><code class=\"xref std std-option docutils literal notranslate\">test --keepdb</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<aside class=\"version-note version-added\" data-version=\"4.2\" role=\"note\">\n<p class=\"version-note-title\">New in Django 4.2</p></aside>\n<p>The <a class=\"reference internal\" href=\"/id/4.2/ref/files/storage/#django.core.files.storage.InMemoryStorage\" title=\"django.core.files.storage.InMemoryStorage\"><code class=\"xref py py-class docutils literal notranslate\">InMemoryStorage</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":"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":[]},{"title":"Avoiding disk access for media files","anchor":"avoiding-disk-access-for-media-files","children":[]}]}]}],"breadcrumbs":[{"docname":"topics/index","title":"Menggunakan Django","url":"/id/4.2/topics/"},{"docname":"topics/testing/index","title":"Percobaan di Django","url":"/id/4.2/topics/testing/"}],"prev":{"docname":"topics/testing/index","title":"Percobaan di Django","url":"/id/4.2/topics/testing/"},"next":{"docname":"topics/testing/tools","title":"Alat percobaan","url":"/id/4.2/topics/testing/tools/"},"formats":{"html":"/id/4.2/topics/testing/overview/","markdown":"/id/4.2/topics/testing/overview.md","json":"/id/4.2/topics/testing/overview.json"},"source":"https://github.com/django/django/blob/stable/4.2.x/docs/topics/testing/overview.txt","official":"https://docs.djangoproject.com/id/4.2/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","it","pt-br","ko","es","el","pl"]}