{"title":"Django 中的测试","version":"3.0","locale":"zh-hans","docname":"topics/testing/index","url":"/zh-hans/3.0/topics/testing/","canonical":"https://djangodocs.dev/zh-hans/3.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/3.0/topics/testing/overview/\"><span class=\"doc\">编写并运行测试</span></a> document.</p>\n<p>你也可以使用 <em>另一个</em> Python 测试框架；Django 提供了用于这种集成的 API 和工具。这在 <a class=\"reference internal\" href=\"/zh-hans/3.0/topics/testing/advanced/\"><span class=\"doc\">Advanced testing topics</span></a> 的 <a class=\"reference internal\" href=\"/zh-hans/3.0/topics/testing/advanced/#other-testing-frameworks\"><span class=\"std std-ref\">Using different testing frameworks</span></a> 章节中有介绍。</p>\n<div class=\"toctree-wrapper compound\">\n<ul>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/zh-hans/3.0/topics/testing/overview/\">编写并运行测试</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/zh-hans/3.0/topics/testing/tools/\">Testing tools</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/zh-hans/3.0/topics/testing/advanced/\">Advanced testing topics</a></li>\n</ul>\n</div>","rootId":"testing-in-django","toc":[],"breadcrumbs":[{"docname":"topics/index","title":"使用 Django","url":"/zh-hans/3.0/topics/"}],"prev":{"docname":"topics/files","title":"管理文件","url":"/zh-hans/3.0/topics/files/"},"next":{"docname":"topics/testing/overview","title":"编写并运行测试","url":"/zh-hans/3.0/topics/testing/overview/"},"formats":{"html":"/zh-hans/3.0/topics/testing/","markdown":"/zh-hans/3.0/topics/testing.md","json":"/zh-hans/3.0/topics/testing.json"},"source":"https://github.com/django/django/blob/stable/3.0.x/docs/topics/testing/index.txt","official":"https://docs.djangoproject.com/zh-hans/3.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","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}