Django Introduction Part10 - Testing a Django web application
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.
Overview
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 behaviour-driven development).
This article 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.
What does Django provide for testing?
Testing a website is a complex task, because it is made of several layers of logic – from HTTP-level request handling, queries models, to form validation and processing, and template rendering.
Django provides a test framework with a small hierarchy of classes that build on the Python standard unittest library. Despite the name, this test framework is suitable for both unit and integration tests. The Django framework adds API methods and tools to help test web and Django-specific behaviour. These allow you to simulate requests, insert test data, and inspect your application’s output. Django also provides an API LiveServerTestCase and tools for using different testing frameworks, for example you can integrate with the popular Selenium framework to simulate a user interacting with a live browser.
To write a test you derive from any of the Django (or unittest) test base classes and then write separate methods to check that specific functionality works as expected (tests use “assert” methods to test that expressions result in True or False values, or that two values are equal, etc.) When you start a test run, the framework executes the chosen test methods in your derived classes. The test methods are run independently, with common setup and/or tear-down behaviour defined in the class, as shown below.
1 | class YourTestClass(TestCase): |
The best base class for most tests is django.test.TestCase. This test class creates a clean database before its tests are run, and runs every test function in its own transaction. The class also owns a test Client that you can use to simulate a user interacting with the code at the view level.
Test structure overview
Before we go into the detail of “what to test”, let’s first briefly look at where and how tests are defined.
Django uses the unittest module’s built-in test discovery, which will discover tests under the current working directory in any file named with the pattern test*.py. Provided you name the files appropriately, you can use any structure you like. We recommend that you create a module for your test code, and have separate files for models, views, forms, and any other types of code you need to test. For example:
1 | movie/ |
Delete the skeleton test.py file as we won’t need it.
Open /tests/test_models.py. The file should import django.test.TestCase, as shown:
1 | from django.test import TestCase |
Often you will add a test class for each model/view/form you want to test, with individual methods for testing specific functionality. In other cases you may wish to have a separate class for testing a specific use case, with individual test functions that test aspects of that use-case (for example, a class to test that a model field is properly validated, with functions to test each of the possible failure cases). Again, the structure is very much up to you, but it is best if you are consistent.
Add the test class below to the bottom of the file. The class demonstrates how to construct a test case class by deriving from TestCase.
1 | class YourTestClass(TestCase): |
The new class defines two methods that you can use for pre-test configuration (for example, to create any models or other objects you will need for the test):
setUpTestData()is called once at the beginning of the test run for class-level setup. You’d use this to create objects that aren’t going to be modified or changed in any of the test methods.setUp()is called before every test function to set up any objects that may be modified by the test (every test function will get a “fresh” version of these objects).
The test classes also have a tearDown() method which we haven’t used. This method isn’t particularly useful for database tests, since the TestCase base class takes care of database teardown for you.
Below those we have a number of test methods, which use Assert functions to test whether conditions are true, false or equal (AssertTrue, AssertFalse, AssertEqual). If the condition does not evaluate as expected then the test will fail and report the error to your console.
The AssertTrue, AssertFalse, AssertEqual are standard assertions provided by unittest. There are other standard assertions in the framework, and also Django-specific assertions to test if a view redirects (assertRedirects), to test if a particular template has been used (assertTemplateUsed), etc.
You should not normally include print() functions in your tests as shown above. We do that here only so that you can see the order that the setup functions are called in the console (in the following section).
How to run the tests
The easiest way to run all the tests is to use the command:
1 | python manage.py test |
If you get errors similar to: ValueError: Missing staticfiles manifest entry ... this may be because testing does not run collectstatic by default and your app is using a storage class that requires it (see manifest_strict for more information). There are a number of ways you can overcome this problem - the easiest is to simply run collectstatic before running the tests:
1 | python3 manage.py collectstatic |
Run the tests in the root directory of yourproject. You should see an output like the one below.
1 | > python3 manage.py test |
Here we see that we had one test failure, and we can see exactly what function failed and why (this failure is expected, because False is not True!).
Tip: The most important thing to learn from the test output above is that it is much more valuable if you use descriptive/informative names for your objects and methods.
Showing more test information
If you want to get more information about the test run you can change the verbosity. For example, to list the test successes as well as failures (and a whole bunch of information about how the testing database is set up) you can set the verbosity to “2” as shown:
1 | python3 manage.py test --verbosity 2 |
The allowed verbosity levels are 0, 1, 2, and 3, with the default being “1”.
Running specific tests
If you want to run a subset of your tests you can do so by specifying the full dot path to the package(s), module, TestCase subclass or method:
1 | # Run the specified module |
Practical examples
Now we know how to run our tests and what sort of things we need to test, let’s look at some practical examples.
Models
For example, consider the Author model below. Here we should test the labels for all the fields, because even though we haven’t explicitly specified most of them, we have a design that says what these values should be. If we don’t test the values, then we don’t know that the field labels have their intended values. Similarly while we trust that Django will create a field of the specified length, it is worthwhile to specify a test for this length to ensure that it was implemented as planned.
1 | class Author(models.Model): |
Open our test_models.py, here you’ll see that we first import TestCase and derive our test class (AuthorModelTest) from it, using a descriptive name so we can easily identify any failing tests in the test output. We then call setUpTestData() to create an author object that we will use but not modify in any of the tests.
1 | from django.test import TestCase |
Run the tests now. If you created the Author model as we described in the models tutorial it is quite likely that you will get an error for the date_of_death label as shown below. The test is failing because it was written expecting the label definition to follow Django’s convention of not capitalising the first letter of the label.
Forms
The philosophy for testing your forms is the same as for testing your models; you need to test anything that you’ve coded or your design specifies, but not the behaviour of the underlying framework and other third party libraries.
Consider our form for renewing books. This has just one field for the renewal date, which will have a label and help text that we will need to verify.
1 | class RenewBookForm(forms.Form): |
Open our test_forms.py file and replace any existing code with the following test code for the RenewBookForm form. We start by importing our form and some Python and Django libraries to help test time-related functionality. We then declare our form test class in the same way as we did for models, using a descriptive name for our TestCase-derived test class.
1 | import datetime |
The first two functions test that the field’s label and help_text are as expected. We have to access the field using the fields dictionary (e.g. form.fields['renewal_date']). Note here that we also have to test whether the label value is None, because even though Django will render the correct label it returns None if the value is not explicitly set.
The rest of the functions test that the form is valid for renewal dates just inside the acceptable range and invalid for values outside the range. Note how we construct test date values around our current date (datetime.date.today()) using datetime.timedelta() (in this case specifying a number of days or weeks). We then just create the form, passing in our data, and test if it is valid.
That’s all for forms; we do have some others, but they are automatically created by our generic class-based editing views, and should be tested there! Run the tests and confirm that our code still passes!
Views
To validate our view behaviour we use the Django test Client. This class acts like a dummy web browser that we can use to simulate GET and POST requests on a URL and observe the response. We can see almost everything about the response, from low-level HTTP (result headers and status codes) through to the template we’re using to render the HTML and the context data we’re passing to it. We can also see the chain of redirects (if any) and check the URL and status code at each step. This allows us to verify that each view is doing what is expected.
Let’s start with one of our simplest views, which provides a list of all Authors. This is displayed at URL /catalog/authors/ (an URL named ‘authors’ in the URL configuration).
1 | class AuthorListView(generic.ListView): |
As this is a generic list view almost everything is done for us by Django. Arguably if you trust Django then the only thing you need to test is that the view is accessible at the correct URL and can be accessed using its name. However if you’re using a test-driven development process you’ll start by writing tests that confirm that the view displays all Authors, paginating them in lots of 10.
Open the test_views.py file and replace any existing text with the following test code for AuthorListView. As before we import our model and some useful classes. In the setUpTestData() method we set up a number of Author objects so that we can test our pagination.
1 | from django.test import TestCase |
All the tests use the client (belonging to our TestCase's derived class) to simulate a GETrequest and get a response. The first version checks a specific URL (note, just the specific path without the domain) while the second generates the URL from its name in the URL configuration.
1 | response = self.client.get('/catalog/authors/') |
Once we have the response we query it for its status code, the template used, whether or not the response is paginated, the number of items returned, and the total number of items.
Views that are restricted to logged in users
In some cases you’ll want to test a view that is restricted to just logged in users. For example our LoanedBooksByUserListView is very similar to our previous view but is only available to logged in users, and only displays BookInstance records that are borrowed by the current user, have the ‘on loan’ status, and are ordered “oldest first”.
1 | from django.contrib.auth.mixins import LoginRequiredMixin |
Add the following test code to test_views.py. Here we first use SetUp() to create some user login accounts and BookInstance objects (along with their associated books and other records) that we’ll use later in the tests. Half of the books are borrowed by each test user, but we’ve initially set the status of all books to “maintenance”. We’ve used SetUp() rather than setUpTestData() because we’ll be modifying some of these objects later.
Note: The setUp() code below creates a book with a specified Language, but your code may not include the Language model as this was created as a challenge. If this is the case, simply comment out the parts of the code that create or import Language objects. You should also do this in the RenewBookInstancesViewTest section that follows.
1 | import datetime |
To verify that the view will redirect to a login page if the user is not logged in we use assertRedirects, as demonstrated in test_redirect_if_not_logged_in(). To verify that the page is displayed for a logged in user we first log in our test user, and then access the page again and check that we get a status_code of 200 (success).
The rest of the tests verify that our view only returns books that are on loan to our current borrower. Copy the code below and paste it onto the end of the test class above.
1 | def test_only_borrowed_books_in_list(self): |
You could also add pagination tests, should you so wish!
Testing views with forms
Testing views with forms is a little more complicated than in the cases above, because you need to test more code paths: initial display, display after data validation has failed, and display after validation has succeeded. The good news is that we use the client for testing in almost exactly the same way as we did for display-only views.
To demonstrate, let’s write some tests for the view used to renew books (renew_book_librarian()):
1 | from catalog.forms import RenewBookForm |
We’ll need to test that the view is only available to users who have the can_mark_returnedpermission, and that users are redirected to an HTTP 404 error page if they attempt to renew a BookInstance that does not exist. We should check that the initial value of the form is seeded with a date three weeks in the future, and that if validation succeeds we’re redirected to the “all-borrowed books” view. As part of checking the validation-fail tests we’ll also check that our form is sending the appropriate error messages.
Add the first part of the test class (shown below) to the bottom of test_views.py. This creates two users and two book instances, but only gives one user the permission required to access the view. The code to grant permissions during tests is shown in bold:
1 | import uuid |
Add the following tests to the bottom of the test class. These check that only users with the correct permissions (testuser2) can access the view. We check all the cases: when the user is not logged in, when a user is logged in but does not have the correct permissions, when the user has permissions but is not the borrower (should succeed), and what happens when they try to access a BookInstance that doesn’t exist. We also check that the correct template is used.
1 | def test_redirect_if_not_logged_in(self): |
Add the next test method, as shown below. This checks that the initial date for the form is three weeks in the future. Note how we are able to access the value of the initial value of the form field (shown in bold).
1 | def test_form_renewal_date_initially_has_date_three_weeks_in_future(self): |
If you use the form class RenewBookModelForm(forms.ModelForm) instead of class RenewBookForm(forms.Form), then the form field name is ‘due_back’ instead of ‘renewal_date’.
The next test (add this to the class too) checks that the view redirects to a list of all borrowed books if renewal succeeds. What differs here is that for the first time we show how you can POST data using the client. The post data is the second argument to the post function, and is specified as a dictionary of key/values.
1 | def test_redirects_to_all_borrowed_book_list_on_success(self): |
The all-borrowed view was added as a challenge, and your code may instead redirect to the home page ‘/’. If so, modify the last two lines of the test code to be like the code below. The follow=True in the request ensures that the request returns the final destination URL (hence checking /catalog/ rather than /).
1 | response = self.client.post(reverse('renew-book-librarian', kwargs={'pk':self.test_bookinstance1.pk,}), {'renewal_date':valid_date_in_future}, follow=True ) |
Copy the last two functions into the class, as seen below. These again test POST requests, but in this case with invalid renewal dates. We use assertFormError()to verify that the error messages are as expected.
1 | def test_form_invalid_renewal_date_past(self): |
The same sorts of techniques can be used to test the other view.
Templates
Django provides test APIs to check that the correct template is being called by your views, and to allow you to verify that the correct information is being sent. There is however no specific API support for testing in Django that your HTML output is rendered as expected.
REFERENCES
Django’s test framework can help you write effective unit and integration tests — we’ve only scratched the surface of what the underlying unittest framework can do, let alone Django’s additions (for example, check out how you can use unittest.mock to patch third party libraries so you can more thoroughly test your own code).
While there are numerous other test tools that you can use, we’ll just highlight two:
-
Coverage: This Python tool reports on how much of your code is actually executed by your tests. It is particularly useful when you’re getting started, and you are trying to work out exactly what you should test.
-
Selenium is a framework to automate testing in a real browser. It allows you to simulate a real user interacting with the site, and provides a great framework for system testing your site (the next step up from integration testing).
-
https://docs.djangoproject.com/en/3.1/topics/testing/overview/
-
https://docs.djangoproject.com/en/3.1/topics/testing/advanced/






