{"title":"Testing in Django","version":"2.1","locale":"zh-hans","docname":"topics/testing/index","url":"/zh-hans/2.1/topics/testing/","canonical":"https://djangodocs.dev/zh-hans/2.1/topics/testing/","summary":"Automated testing is an extremely useful bug-killing tool for the modern Web developer. You can use a collection of tests -- a test suite -- to solve, or avoid, a…","html":"<h1>Testing in Django<a class=\"heading-anchor\" href=\"#testing-in-django\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Automated testing is an extremely useful bug-killing tool for the modern\nWeb developer. You can use a collection of tests -- a <strong>test suite</strong> -- to\nsolve, or avoid, a number of problems:</p>\n<ul class=\"simple\">\n<li><p>When you're writing new code, you can use tests to validate your code\nworks as expected.</p></li>\n<li><p>When you're refactoring or modifying old code, you can use tests to\nensure your changes haven't affected your application's behavior\nunexpectedly.</p></li>\n</ul>\n<p>Testing a Web application is a complex task, because a Web application is made\nof several layers of logic -- from HTTP-level request handling, to form\nvalidation and processing, to template rendering. With Django's test-execution\nframework and assorted utilities, you can simulate requests, insert test data,\ninspect your application's output and generally verify your code is doing what\nit should be doing.</p>\n<p>The best part is, it's really easy.</p>\n<p>The preferred way to write tests in Django is using the <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> module\nbuilt in to the Python standard library. This is covered in detail in the\n<a class=\"reference internal\" href=\"/zh-hans/2.1/topics/testing/overview/\"><span class=\"doc\">Writing and running tests</span></a> document.</p>\n<p>You can also use any <em>other</em> Python test framework; Django provides an API and\ntools for that kind of integration. They are described in the\n<a class=\"reference internal\" href=\"/zh-hans/2.1/topics/testing/advanced/#other-testing-frameworks\"><span class=\"std std-ref\">Using different testing frameworks</span></a> section of <a class=\"reference internal\" href=\"/zh-hans/2.1/topics/testing/advanced/\"><span class=\"doc\">Advanced testing topics</span></a>.</p>\n<div class=\"toctree-wrapper compound\">\n<ul>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/zh-hans/2.1/topics/testing/overview/\">Writing and running tests</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/zh-hans/2.1/topics/testing/tools/\">Testing tools</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/zh-hans/2.1/topics/testing/advanced/\">Advanced testing topics</a></li>\n</ul>\n</div>","rootId":"testing-in-django","toc":[],"breadcrumbs":[{"docname":"topics/index","title":"Using Django","url":"/zh-hans/2.1/topics/"}],"prev":{"docname":"topics/files","title":"Managing files","url":"/zh-hans/2.1/topics/files/"},"next":{"docname":"topics/testing/overview","title":"Writing and running tests","url":"/zh-hans/2.1/topics/testing/overview/"},"formats":{"html":"/zh-hans/2.1/topics/testing/","markdown":"/zh-hans/2.1/topics/testing.md","json":"/zh-hans/2.1/topics/testing.json"},"source":"https://github.com/django/django/blob/stable/2.1.x/docs/topics/testing/index.txt","official":"https://docs.djangoproject.com/zh-hans/2.1/topics/testing/","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"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}