Testing in 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:
- When youβre writing new code, you can use tests to validate your code works as expected.
- When youβre refactoring or modifying old code, you can use tests to ensure your changes havenβt affected your applicationβs behavior unexpectedly.
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.
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
Writing and running tests 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 Using different testing frameworks section of Advanced testing topics.