{"title":"Django 中的测试","version":"6.0","locale":"zh-hans","docname":"topics/testing/index","url":"/zh-hans/6.0/topics/testing/","canonical":"https://djangodocs.dev/zh-hans/6.0/topics/testing/","summary":"自动化测试是现代 web 开发者非常有用的 bug 消灭工具。你可以使用一组测试 -- 一个 测试套件 -- 来解决或避免一些问题： 当你编写新代码时，你可以利用测试确保代码按照期望的方式运行。 当你重构或修改旧代码，你可以利用测试来确保你的修改不会使应用运行出错。 测试一个 web 应用是一项复杂的任务，因为一个 web…","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>自动化测试是现代 web 开发者非常有用的 bug 消灭工具。你可以使用一组测试 -- 一个 <strong>测试套件</strong> -- 来解决或避免一些问题：</p>\n<ul class=\"simple\">\n<li><p>当你编写新代码时，你可以利用测试确保代码按照期望的方式运行。</p></li>\n<li><p>当你重构或修改旧代码，你可以利用测试来确保你的修改不会使应用运行出错。</p></li>\n</ul>\n<p>测试一个 web 应用是一项复杂的任务，因为一个 web 应用由多个层次的逻辑组成 -- 从处理 HTTP 请求的层面，到表单验证和处理，再到模板渲染。使用 Django 的测试执行框架和各种实用工具，你可以模拟请求、插入测试数据、检查应用程序的输出，通常验证你的代码是否按照预期执行。</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/6.0/topics/testing/overview/\"><span class=\"doc\">编写并运行测试</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/6.0/topics/testing/advanced/#other-testing-frameworks\"><span class=\"std std-ref\">使用不同的测试框架</span></a> section of <a class=\"reference internal\" href=\"/zh-hans/6.0/topics/testing/advanced/\"><span class=\"doc\">进阶测试主题</span></a>.</p>\n<div class=\"toctree-wrapper compound\">\n<ul>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/zh-hans/6.0/topics/testing/overview/\">编写并运行测试</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/zh-hans/6.0/topics/testing/tools/\">测试工具</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/zh-hans/6.0/topics/testing/advanced/\">进阶测试主题</a></li>\n</ul>\n</div>","rootId":"testing-in-django","toc":[],"breadcrumbs":[{"docname":"topics/index","title":"使用 Django","url":"/zh-hans/6.0/topics/"}],"prev":{"docname":"topics/files","title":"管理文件","url":"/zh-hans/6.0/topics/files/"},"next":{"docname":"topics/testing/overview","title":"编写并运行测试","url":"/zh-hans/6.0/topics/testing/overview/"},"formats":{"html":"/zh-hans/6.0/topics/testing/","markdown":"/zh-hans/6.0/topics/testing.md","json":"/zh-hans/6.0/topics/testing.json"},"source":"https://github.com/django/django/blob/stable/6.0.x/docs/topics/testing/index.txt","official":"https://docs.djangoproject.com/zh-hans/6.0/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","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}