Testing in Django

Automated testing is an extremely useful bug-killing tool for the modernWeb developer. You can use a collection of tests — a test suite — tosolve, or avoid, a number of problems:

  • When you're writing new code, you can use tests to validate your codeworks as expected.
  • When you're refactoring or modifying old code, you can use tests toensure your changes haven't affected your application's behaviorunexpectedly.
    Testing a Web application is a complex task, because a Web application is madeof several layers of logic — from HTTP-level request handling, to formvalidation and processing, to template rendering. With Django's test-executionframework and assorted utilities, you can simulate requests, insert test data,inspect your application's output and generally verify your code is doing whatit should be doing.

The best part is, it's really easy.

The preferred way to write tests in Django is using the unittest modulebuilt in to the Python standard library. This is covered in detail in theWriting and running tests document.

You can also use any other Python test framework; Django provides an API andtools for that kind of integration. They are described in theUsing different testing frameworks section of Advanced testing topics.