Django Tutorial Part 10: Testing a Django web application

  • Previous
  • Overview: Django Web Framework (Python)
  • As websites grow they become harder to test manually. Not only is there more to test, but, as interactions between components become more complex, a small change in one area can impact other areas, so more changes will be required to ensure everything keeps working and errors are not introduced as more changes are made. One way to mitigate these problems is to write automated tests, which can easily and reliably be run every time you make a change. This tutorial shows how to automate unit testing of your website using Django's test framework.

    Prerequisites: Complete all previous tutorial topics, including Django Tutorial Part 9: Working with forms . Objective: To understand how to write unit tests for Django-based websites.

Overview

The Local Library currently has pages to display lists of all books and authors, detail views for Book and Author items, a page to renew BookInstance items, and pages to create, update, and delete Author items (and Book records too, if you completed the challenge in the forms tutorial ). Even with this relatively small site, manually navigating to each page and superficially checking that everything works as expected can take several minutes. As we make changes and grow the site, the time required to manually check that everything works "properly" will only grow. If we were to continue as we are, eventually we'd be spending most of our time testing, and very little time improving our code.

Automated tests can really help with this problem! The obvious benefits are that they can be run much faster than manual tests, can test to a much lower level of detail, and test exactly the same functionality every time (human testers are nowhere near as reliable!) Because they are fast, automated tests can be executed more regularly, and if a test fails, they point to exactly where code is not performing as expected.

In addition, automated tests can act as the first real-world "user" of your code, forcing you to be rigorous about defining and documenting how your website should behave. Often they are the basis for your code examples and documentation. For these reasons, some software development processes start with test definition and implementation, after which the code is written to match the required behavior (e.g. test-driven and behavior-driven development).

This tutorial shows how to write automated tests for Django, by adding a number of tests to the LocalLibrary website.

Types of testing

There are numerous types, levels, and classifications of tests and testing approaches. The most important automated tests are:

Unit tests

Verify functional behavior of individual components, often to class and function level.

Regression tests

Tests that reproduce historic bugs. Each test is initially run to verify that the bug has been fixed, and then re-run to ensure that it has not been reintroduced following later changes to the code.

Integration tests

Verify how groupings of components work when used together. Integration tests are aware of the required interactions between components, but not necessarily of the internal operations of each component. They may cover simple groupings of components through to the whole website.

Note: Other common types of tests include black box, white box, manual, automated, canary, smoke, conformance, acceptance, functional, system, performance, load, and stress tests. Look them up for more information.