Chapter 1. Django Testing Overview
How do you know when code you have written is working as intended? Well, you test it. But how? For a web application, you can test the code by manually bringing up the pages of your application in a web browser and verifying that they are correct. This involves more than a quick glance to see whether they have the correct content, as you must also ensure, for example, that all the links work and that any forms work properly. As you can imagine, this sort of manual testing quickly becomes impossible to rely on as an application grows beyond a few simple pages. For any non-trivial application, automated testing is essential.
Automated testing of Django applications makes use of the fundamental test support built-in to the Python language: doctests and unit tests. When you create a new Django application with manage.py startapp
, one of the generated files contains a sample doctest and unit test, intended to jump-start your own test writing. In this chapter, we will begin our study of testing Django applications. Specifically, we will:
Examine in detail the contents of the sample
tests.py
file, reviewing the fundamentals of Python's test support as we do soSee how to use Django utilities to run the tests contained in
tests.py
Learn how to interpret the output of the tests, both when the tests succeed and when they fail
Review the effects of the various command-line options that can be used when testing