---
title: "Django 中的测试"
version: 3.0
locale: zh-hans
source: https://docs.djangoproject.com/zh-hans/3.0/topics/testing/
canonical: https://djangodocs.dev/zh-hans/3.0/topics/testing/
---
# Django 中的测试

对现在的 Web 开发者来说，自动化测试是一个非常有用的发现 bug 的工具。你可以使用一套测试—— **测试套件** ——去解决，避免一堆的问题：

- 当你编写新代码时，你可以利用测试确保代码按照期望的方式运行。
- 当你重构或修改旧代码，你可以利用测试来确保你的修改不会使应用运行出错。

测试一个 Web 应用是一项复杂的工作，因为 Web 应用包含了多层业务逻辑——从 HTTP 层响应请求，到表单有效性检测和处理，再到模板渲染。利用 Django 的测试执行框架和配套的工具，你可以模拟其你去，插入测试数据，检查应用的输出，以此检验你的代码是否按照期望运行。

The preferred way to write tests in Django is using the [`unittest`](https://docs.python.org/3/library/unittest.html#module-unittest) module
built-in to the Python standard library. This is covered in detail in the
[编写并运行测试](/zh-hans/3.0/topics/testing/overview/) document.

你也可以使用 *另一个* Python 测试框架；Django 提供了用于这种集成的 API 和工具。这在 [Advanced testing topics](/zh-hans/3.0/topics/testing/advanced/) 的 [Using different testing frameworks](/zh-hans/3.0/topics/testing/advanced/#other-testing-frameworks) 章节中有介绍。

- [编写并运行测试](/zh-hans/3.0/topics/testing/overview/)
- [Testing tools](/zh-hans/3.0/topics/testing/tools/)
- [Advanced testing topics](/zh-hans/3.0/topics/testing/advanced/)
