Django 中的测试

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 number of problems:

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

Testing a web application is a complex task, because a web application is made of several layers of logic -- from HTTP-level request handling, to form validation and processing, to template rendering. With Django's test-execution framework and assorted utilities, you can simulate requests, insert test data, inspect your application's output and generally verify your code is doing what it should be doing.

在 Django 中编写测试的首选方式是使用 Python 标准库中内置的 unittest 模块。这一点在 编写并运行测试 文档中有详细介绍。

你也可以使用 另一个 Python 测试框架;Django 提供了用于这种集成的 API 和工具。这在 进阶测试主题使用不同的测试框架 章节中有介绍。

Back to Top