---
title: "Django 中的测试"
version: 6.1
locale: zh-hans
source: https://docs.djangoproject.com/zh-hans/6.1/topics/testing/
canonical: https://djangodocs.dev/zh-hans/6.1/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/6.1/topics/testing/overview/) document.

You can also use any *other* Python test framework; Django provides an API and
tools for that kind of integration. They are described in the
[使用不同的测试框架](/zh-hans/6.1/topics/testing/advanced/#other-testing-frameworks) section of [进阶测试主题](/zh-hans/6.1/topics/testing/advanced/).

- [编写并运行测试](/zh-hans/6.1/topics/testing/overview/)
- [测试工具](/zh-hans/6.1/topics/testing/tools/)
- [进阶测试主题](/zh-hans/6.1/topics/testing/advanced/)
