Django 中的测试¶
自动化测试是现代 web 开发者非常有用的 bug 消灭工具。你可以使用一组测试 -- 一个 测试套件 -- 来解决或避免一些问题:
当你编写新代码时,你可以利用测试确保代码按照期望的方式运行。
当你重构或修改旧代码,你可以利用测试来确保你的修改不会使应用运行出错。
测试一个 web 应用是一项复杂的任务,因为一个 web 应用由多个层次的逻辑组成 -- 从处理 HTTP 请求的层面,到表单验证和处理,再到模板渲染。使用 Django 的测试执行框架和各种实用工具,你可以模拟请求、插入测试数据、检查应用程序的输出,通常验证你的代码是否按照预期执行。
The preferred way to write tests in Django is using the unittest module
built-in to the Python standard library. This is covered in detail in the
编写并运行测试 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 使用不同的测试框架 section of 进阶测试主题.