{"title":"Django 中的测试","version":"4.1","locale":"zh-hans","docname":"topics/testing/index","url":"/zh-hans/4.1/topics/testing/","canonical":"https://djangodocs.dev/zh-hans/4.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>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>当你编写新代码时，你可以利用测试确保代码按照期望的方式运行。</p></li>\n<li><p>当你重构或修改旧代码，你可以利用测试来确保你的修改不会使应用运行出错。</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>在 Django 中编写测试的首选方式是使用 Python 标准库中内置的 <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> 模块。这一点在 <a class=\"reference internal\" href=\"/zh-hans/4.1/topics/testing/overview/\"><span class=\"doc\">编写并运行测试</span></a> 文档中有详细介绍。</p>\n<p>你也可以使用 <em>另一个</em> Python 测试框架；Django 提供了用于这种集成的 API 和工具。这在 <a class=\"reference internal\" href=\"/zh-hans/4.1/topics/testing/advanced/\"><span class=\"doc\">进阶测试主题</span></a> 的 <a class=\"reference internal\" href=\"/zh-hans/4.1/topics/testing/advanced/#other-testing-frameworks\"><span class=\"std std-ref\">使用不同的测试框架</span></a> 章节中有介绍。</p>\n<div class=\"toctree-wrapper compound\">\n<ul>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/zh-hans/4.1/topics/testing/overview/\">编写并运行测试</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/zh-hans/4.1/topics/testing/tools/\">测试工具</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/zh-hans/4.1/topics/testing/advanced/\">进阶测试主题</a></li>\n</ul>\n</div>","rootId":"testing-in-django","toc":[],"breadcrumbs":[{"docname":"topics/index","title":"使用 Django","url":"/zh-hans/4.1/topics/"}],"prev":{"docname":"topics/files","title":"管理文件","url":"/zh-hans/4.1/topics/files/"},"next":{"docname":"topics/testing/overview","title":"编写并运行测试","url":"/zh-hans/4.1/topics/testing/overview/"},"formats":{"html":"/zh-hans/4.1/topics/testing/","markdown":"/zh-hans/4.1/topics/testing.md","json":"/zh-hans/4.1/topics/testing.json"},"source":"https://github.com/django/django/blob/stable/4.1.x/docs/topics/testing/index.txt","official":"https://docs.djangoproject.com/zh-hans/4.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","it","pt-br","ko","es","el","pl"]}