Introducing automated testing
What are automated tests?
Tests are simple routines that check the operation of your code.
Testing operates at different levels. Some tests might apply to a tiny detail (does a particular model method return values as expected?) while others examine the overall operation of the software (does a sequence of user inputs on the site produce the desired result?). That’s no different from the kind of testing we did earlier in the book, using the shell to examine the behavior of a method, or running the application and entering data to check how it behaves.
What’s different in automated tests is that the testing work is done for you by the system. You create a set of tests once, and then as you make changes to your app, you can check that your code still works as you originally intended, without having to perform time consuming manual testing.
Why you need to create tests
So why create tests, and why now?
You may feel that you have quite enough on your plate just learning Python/Django, and having yet another thing to learn and do may seem overwhelming and perhaps unnecessary. After all, our polls application is working quite happily now; going through the trouble of creating automated tests is not going to make it work any better. If creating the polls application is the last bit of Django programming you will ever do, then true, you don’t need to know how to create automated tests. But, if that’s not the case, now is an excellent time to learn.
TESTS WILL SAVE YOU TIME
Up to a certain point, ‘checking that it seems to work’ will be a satisfactory test. In a more sophisticated application, you might have dozens of complex interactions between components.
A change in any of those components could have unexpected consequences on the application’s behavior. Checking that it still ‘seems to work’ could mean running through your code’s functionality with twenty different variations of your test data just to make sure you haven’t broken something – not a good use of your time.
That’s especially true when automated tests could do this for you in seconds. If something’s gone wrong, tests will also assist in identifying the code that’s causing the unexpected behavior.
Sometimes it may seem a chore to tear yourself away from your productive, creative programming work to face the unglamorous and unexciting business of writing tests, particularly when you know your code is working properly.
However, the task of writing tests is a lot more fulfilling than spending hours testing your application manually or trying to identify the cause of a newly-introduced problem.
TESTS DON’T JUST IDENTIFY PROBLEMS, THEY PREVENT THEM
It’s a mistake to think of tests merely as a negative aspect of development.
Without tests, the purpose or intended behavior of an application might be rather opaque. Even when it’s your own code, you will sometimes find yourself poking around in it trying to find out what exactly it’s doing.
Tests change that; they light up your code from the inside, and when something goes wrong, they focus light on the part that has gone wrong – even if you hadn’t even realized it had gone wrong.
TESTS MAKE YOUR CODE MORE ATTRACTIVE
You might have created a brilliant piece of software, but you will find that many other developers will simply refuse to look at it because it lacks tests; without tests, they won’t trust it. Jacob Kaplan-Moss, one of Django’s original developers, says “Code without tests is broken by design.”
That other developers want to see tests in your software before they take it seriously is yet another reason for you to start writing tests.
TESTS HELP TEAMS WORK TOGETHER
The previous points are written from the point of view of a single developer maintaining an application. Complex applications will be maintained by teams. Tests guarantee that colleagues don’t inadvertently break your code (and that you don’t break theirs without knowing). If you want to make a living as a Django programmer, you must be good at writing tests!
Basic testing strategies
There are many ways to approach writing tests.
Some programmers follow a discipline called “test-driven development“; they actually write their tests before they write their code. This might seem counter-intuitive, but in fact it’s similar to what most people will often do anyway: they describe a problem, then create some code to solve it. Test-driven development simply formalizes the problem in a Python test case.
More often, a newcomer to testing will create some code and later decide that it should have some tests. Perhaps it would have been better to write some tests earlier, but it’s never too late to get started.
Sometimes it’s difficult to figure out where to get started with writing tests. If you have written several thousand lines of Python, choosing something to test might not be easy. In such a case, it’s fruitful to write your first test the next time you make a change, either when you add a new feature or fix a bug.
Writing tests
Django’s unit tests use a Python standard library module: unittest
. This module defines tests using a class-based approach.
Here is an example which subclasses from django.test.TestCase
, which is a subclass of unittest.TestCase
that runs each test inside a transaction to provide isolation:
from django.test import TestCase
from myapp.models import Animal
class AnimalTestCase(TestCase):
def setUp(self):
Animal.objects.create(name="lion", sound="roar")
Animal.objects.create(name="cat", sound="meow")
def test_animals_can_speak(self):
"""Animals that can speak are correctly identified"""
lion = Animal.objects.get(name="lion")
cat = Animal.objects.get(name="cat")
self.assertEqual(lion.speak(), "The lion says "roar"")
self.assertEqual(cat.speak(), "The cat says "meow"")
When you run your tests, the default behavior of the test utility is to find all the test cases (that is, subclasses of unittest.TestCase
) in any file whose name begins with test
, automatically build a test suite out of those test cases, and run that suite.
For more details about unittest
, see the Python documentation.
Warning
If your tests rely on database access such as creating or querying models, be sure to create your test classes as subclasses of django.test.TestCase
rather than unittest.TestCase
.
Using unittest.TestCase
avoids the cost of running each test in a transaction and flushing the database, but if your tests interact with the database their behavior will vary based on the order that the test runner executes them. This can lead to unit tests that pass when run in isolation but fail when run in a suite.
Running tests
Once you’ve written tests, run them using the test command of your project’s manage.py
utility:
$ ./manage.py test
Test discovery is based on the unittest module’s built-in test discovery. By default, this will discover tests in any file named “test*.py” under the current working directory.
You can specify particular tests to run by supplying any number of “test labels” to ./manage.py test
. Each test label can be a full Python dotted path to a package, module, TestCase
subclass, or test method. For instance:
# Run all the tests in the animals.tests module
$ ./manage.py test animals.tests
# Run all the tests found within the "animals" package
$ ./manage.py test animals
# Run just one test case
$ ./manage.py test animals.tests.AnimalTestCase
# Run just one test method
$ ./manage.py test animals.tests.AnimalTestCase.test_animals_can_speak
You can also provide a path to a directory to discover tests below that directory:
$ ./manage.py test animals/
You can specify a custom filename pattern match using the -p
(or --pattern
) option, if your test files are named differently from the test*.py
pattern:
$ ./manage.py test --pattern="tests_*.py"
If you press Ctrl-C
while the tests are running, the test runner will wait for the currently running test to complete and then exit gracefully. During a graceful exit the test runner will output details of any test failures, report on how many tests were run and how many errors and failures were encountered, and destroy any test databases as usual. Thus pressing Ctrl-C
can be very useful if you forget to pass the --failfast
option, notice that some tests are unexpectedly failing, and want to get details on the failures without waiting for the full test run to complete.
If you do not want to wait for the currently running test to finish, you can press Ctrl-C
a second time and the test run will halt immediately, but not gracefully. No details of the tests run before the interruption will be reported, and any test databases created by the run will not be destroyed.
Test with warnings enabled
It’s a good idea to run your tests with Python warnings enabled: python -Wall manage.py test
. The -Wall
flag tells Python to display deprecation warnings. Django, like many other Python libraries, uses these warnings to flag when features are going away. It also might flag areas in your code that aren’t strictly wrong but could benefit from a better implementation.
The test database
Tests that require a database (namely, model tests) will not use your “real” (production) database. Separate, blank databases are created for the tests.
Regardless of whether the tests pass or fail, the test databases are destroyed when all the tests have been executed.
You can prevent the test databases from being destroyed by adding the --keepdb
flag to the test command. This will preserve the test database between runs. If the database does not exist, it will first be created. Any migrations will also be applied in order to keep it up to date.
By default the test databases get their names by prepending test_
to the value of the NAME
settings for the databases defined in DATABASES
. When using the SQLite database engine the tests will by default use an in-memory database (i.e., the database will be created in memory, bypassing the filesystem entirely!). If you want to use a different database name, specify NAME <TEST_NAME>
in the TEST <DATABASE-TEST>
dictionary for any given database in DATABASES
.
On PostgreSQL, USER
will also need read access to the built-in postgres
database.
Aside from using a separate database, the test runner will otherwise use all of the same database settings you have in your settings file: ENGINE <DATABASE-ENGINE>
, USER
, HOST
, etc. The test database is created by the user specified by USER
, so you’ll need to make sure that the given user account has sufficient privileges to create a new database on the system.
For fine-grained control over the character encoding of your test database, use the CHARSET <TEST_CHARSET>
TEST option. If you’re using MySQL, you can also use the COLLATION <TEST_COLLATION>
option to control the particular collation used by the test database. See the settings documentation for details of these and other advanced settings.
If using a SQLite in-memory database with Python 3.4+ and SQLite 3.7.13+, shared cache will be enabled, so you can write tests with ability to share the database between threads.
Finding data from your production database when running tests?
If your code attempts to access the database when its modules are compiled, this will occur before the test database is set up, with potentially unexpected results. For example, if you have a database query in module-level code and a real database exists, production data could pollute your tests. It is a bad idea to have such import-time database queries in your code anyway – rewrite your code so that it doesn’t do this.
This also applies to customized implementations of ready()
.
Order in which tests are executed
In order to guarantee that all TestCase
code starts with a clean database, the Django test runner reorders tests in the following way:
- All
TestCase
subclasses are run first. - Then, all other Django-based tests (test cases based on
SimpleTestCase
, includingTransactionTestCase
) are run with no particular ordering guaranteed nor enforced among them. - Then any other
unittest.TestCase
tests (including doctests) that may alter the database without restoring it to its original state are run.
Note
The new ordering of tests may reveal unexpected dependencies on test case ordering. This is the case with doctests that relied on state left in the database by a given TransactionTestCase
test, they must be updated to be able to run independently.
You may reverse the execution order inside groups by passing --reverse
to the test command. This can help with ensuring your tests are independent from each other.
Rollback emulation
Any initial data loaded in migrations will only be available in TestCase
tests and not in TransactionTestCase
tests, and additionally only on backends where transactions are supported (the most important exception being MyISAM). This is also true for tests which rely on TransactionTestCase
such as LiveServerTestCase
andStaticLiveServerTestCase
.
Django can reload that data for you on a per-testcase basis by setting the serialized_rollback
option to True
in the body of the TestCase
or TransactionTestCase
, but note that this will slow down that test suite by approximately 3x.
Third-party apps or those developing against MyISAM will need to set this; in general, however, you should be developing your own projects against a transactional database and be using TestCase
for most tests, and thus not need this setting.
The initial serialization is usually very quick, but if you wish to exclude some apps from this process (and speed up test runs slightly), you may add those apps to TEST_NON_SERIALIZED_APPS
.
Other test conditions
Regardless of the value of the DEBUG
setting in your configuration file, all Django tests run withDEBUG
=False. This is to ensure that the observed output of your code matches what will be seen in a production setting.
Caches are not cleared after each test, and running “manage.py test fooapp” can insert data from the tests into the cache of a live system if you run your tests in production because, unlike databases, a separate “test cache” is not used. This behavior may change in the future.
Understanding the test output
When you run your tests, you’ll see a number of messages as the test runner prepares itself. You can control the level of detail of these messages with the verbosity
option on the command line:
Creating test database...
Creating table myapp_animal
Creating table myapp_mineral
This tells you that the test runner is creating a test database, as described in the previous section.
Once the test database has been created, Django will run your tests. If everything goes well, you’ll see something like this:
----------------------------------------------------------------------
Ran 22 tests in 0.221s
OK
If there are test failures, however, you’ll see full details about which tests failed:
======================================================================
FAIL: test_was_published_recently_with_future_poll (polls.tests.PollMethodTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/dev/mysite/polls/tests.py", line 16, in test_was_published_recently_with_future_poll
self.assertEqual(future_poll.was_published_recently(), False)
AssertionError: True != False
----------------------------------------------------------------------
Ran 1 test in 0.003s
FAILED (failures=1)
A full explanation of this error output is beyond the scope of this document, but it’s pretty intuitive. You can consult the documentation of Python’s unittest
library for details.
Note that the return code for the test-runner script is 1 for any number of failed and erroneous tests. If all the tests pass, the return code is 0. This feature is useful if you’re using the test-runner script in a shell script and need to test for success or failure at that level.
Speeding up the tests
In recent versions of Django, the default password hasher is rather slow by design. If during your tests you are authenticating many users, you may want to use a custom settings file and set the PASSWORD_HASHERS
setting to a faster hashing algorithm:
PASSWORD_HASHERS = [
"django.contrib.auth.hashers.MD5PasswordHasher",
]
Don’t forget to also include in PASSWORD_HASHERS
any hashing algorithm used in fixtures, if any.
Testing tools
Django provides a small set of tools that come in handy when writing tests.
The test client
The test client is a Python class that acts as a dummy Web browser, allowing you to test your views and interact with your Django-powered application programmatically.
Some of the things you can do with the test client are:
- Simulate GET and POST requests on a URL and observe the response – everything from low-level HTTP (result headers and status codes) to page content.
- See the chain of redirects (if any) and check the URL and status code at each step.
- Test that a given request is rendered by a given Django template, with a template context that contains certain values.
Note that the test client is not intended to be a replacement for Selenium or other “in-browser” frameworks. Django’s test client has a different focus. In short:
- Use Django’s test client to establish that the correct template is being rendered and that the template is passed the correct context data.
- Use in-browser frameworks like Selenium to test rendered HTML and the behavior of Web pages, namely JavaScript functionality. Django also provides special support for those frameworks; see the section on
LiveServerTestCase
for more details.
A comprehensive test suite should use a combination of both test types.
OVERVIEW AND A QUICK EXAMPLE
To use the test client, instantiate django.test.Client
and retrieve Web pages:
>>> from django.test import Client
>>> c = Client()
>>> response = c.post("/login/", {"username": "john", "password": "smith"})
>>> response.status_code
200
>>> response = c.get("/customer/details/")
>>> response.content
"<!DOCTYPE html..."
As this example suggests, you can instantiate Client
from within a session of the Python interactive interpreter.
Note a few important things about how the test client works:
The test client does not require the Web server to be running. In fact, it will run just fine with no Web server running at all! That’s because it avoids the overhead of HTTP and deals directly with the Django framework. This helps make the unit tests run quickly.
When retrieving pages, remember to specify the path of the URL, not the whole domain. For example, this is correct:
>>> c.get("/login/")
This is incorrect:
>>> c.get("http://www.example.com/login/")
The test client is not capable of retrieving Web pages that are not powered by your Django project. If you need to retrieve other Web pages, use a Python standard library module such as
urllib
.To resolve URLs, the test client uses whatever URLconf is pointed-to by your
ROOT_URLCONF
setting.Although the above example would work in the Python interactive interpreter, some of the test client’s functionality, notably the template-related functionality, is only available while tests are running.
The reason for this is that Django’s test runner performs a bit of black magic in order to determine which template was loaded by a given view. This black magic (essentially a patching of Django’s template system in memory) only happens during test running.
By default, the test client will disable any CSRF checks performed by your site.
If, for some reason, you want the test client to perform CSRF checks, you can create an instance of the test client that enforces CSRF checks. To do this, pass in the
enforce_csrf_checks
argument when you construct your client:>>> from django.test import Client >>> csrf_client = Client(enforce_csrf_checks=True)
MAKING REQUESTS
Use the django.test.Client
class to make requests.
class Client
(enforce_csrf_checks=False, **defaults)
It requires no arguments at time of construction. However, you can use keywords arguments to specify some default headers. For example, this will send a User-Agent
HTTP header in each request:
>>> c = Client(HTTP_USER_AGENT="Mozilla/5.0")
The values from the extra
keywords arguments passed to get()
, post()
, etc. have precedence over the defaults passed to the class constructor.
The enforce_csrf_checks
argument can be used to test CSRF protection (see above).
Once you have a Client
instance, you can call any of the following methods:
get
(path, data=None, follow=False, secure=False, **extra)
Makes a GET request on the provided path
and returns a Response
object, which is documented below.
The key-value pairs in the data
dictionary are used to create a GET data payload. For example:
>>> c = Client()
>>> c.get("/customers/details/", {"name": "fred", "age": 7})
…will result in the evaluation of a GET request equivalent to:
/customers/details/?name=fred&age=7
The extra
keyword arguments parameter can be used to specify headers to be sent in the request. For example:
>>> c = Client()
>>> c.get("/customers/details/", {"name": "fred", "age": 7},
... HTTP_X_REQUESTED_WITH="XMLHttpRequest")
…will send the HTTP header HTTP_X_REQUESTED_WITH
to the details view, which is a good way to test code paths that use the django.http.HttpRequest.is_ajax()
method.
CGI specification
The headers sent via **extra
should follow CGI specification. For example, emulating a different “Host” header as sent in the HTTP request from the browser to the server should be passed as HTTP_HOST
.
If you already have the GET arguments in URL-encoded form, you can use that encoding instead of using the data argument. For example, the previous GET request could also be posed as:
>>> c = Client()
>>> c.get("/customers/details/?name=fred&age=7")
If you provide a URL with both an encoded GET data and a data argument, the data argument will take precedence.
If you set follow
to True
the client will follow any redirects and a redirect_chain
attribute will be set in the response object containing tuples of the intermediate urls and status codes.
If you had a URL /redirect_me/
that redirected to /next/
, that redirected to /final/
, this is what you’d see:
>>> response = c.get("/redirect_me/", follow=True)
>>> response.redirect_chain
[("http://testserver/next/", 302), ("http://testserver/final/", 302)]
If you set secure
to True
the client will emulate an HTTPS request.
post
(path, data=None, content_type=MULTIPART_CONTENT, follow=False, secure=False, **extra)
Makes a POST request on the provided path
and returns a Response
object, which is documented below.
The key-value pairs in the data
dictionary are used to submit POST data. For example:
>>> c = Client()
>>> c.post("/login/", {"name": "fred", "passwd": "secret"})
…will result in the evaluation of a POST request to this URL:
/login/
…with this POST data:
name=fred&passwd=secret
If you provide content_type
(e.g. text/xml for an XML payload), the contents of data
will be sent as-is in the POST request, using content_type
in the HTTP Content-Type
header.
If you don’t provide a value for content_type
, the values in data
will be transmitted with a content type ofmultipart/form-data. In this case, the key-value pairs in data
will be encoded as a multipart message and used to create the POST data payload.
To submit multiple values for a given key – for example, to specify the selections for a <select multiple>
– provide the values as a list or tuple for the required key. For example, this value of data
would submit three selected values for the field named choices
:
{"choices": ("a", "b", "d")}
Submitting files is a special case. To POST a file, you need only provide the file field name as a key, and a file handle to the file you wish to upload as a value. For example:
>>> c = Client()
>>> with open("wishlist.doc") as fp:
... c.post("/customers/wishes/", {"name": "fred", "attachment": fp})
(The name attachment
here is not relevant; use whatever name your file-processing code expects.)
You may also provide any file-like object (e.g., StringIO
or BytesIO
) as a file handle.
Note that if you wish to use the same file handle for multiple post()
calls then you will need to manually reset the file pointer between posts. The easiest way to do this is to manually close the file after it has been provided to post()
, as demonstrated above.
You should also ensure that the file is opened in a way that allows the data to be read. If your file contains binary data such as an image, this means you will need to open the file in rb
(read binary) mode.
The extra
argument acts the same as for Client.get()
.
If the URL you request with a POST contains encoded parameters, these parameters will be made available in the request.GET data. For example, if you were to make the request:
>>> c.post("/login/?visitor=true", {"name": "fred", "passwd": "secret"})
… the view handling this request could interrogate request.POST to retrieve the username and password, and could interrogate request.GET to determine if the user was a visitor.
If you set follow
to True
the client will follow any redirects and a redirect_chain
attribute will be set in the response object containing tuples of the intermediate urls and status codes.
If you set secure
to True
the client will emulate an HTTPS request.
head
(path, data=None, follow=False, secure=False, **extra)
Makes a HEAD request on the provided path
and returns a Response
object. This method works just likeClient.get()
, including the follow
, secure
and extra
arguments, except it does not return a message body.
options
(path, data=”, content_type=’application/octet-stream’, follow=False, secure=False, **extra)
Makes an OPTIONS request on the provided path
and returns a Response
object. Useful for testing RESTful interfaces.
When data
is provided, it is used as the request body, and a Content-Type
header is set to content_type
.
The follow
, secure
and extra
arguments act the same as for Client.get()
.
put
(path, data=”, content_type=’application/octet-stream’, follow=False, secure=False, **extra)
Makes a PUT request on the provided path
and returns a Response
object. Useful for testing RESTful interfaces.
When data
is provided, it is used as the request body, and a Content-Type
header is set to content_type
.
The follow
, secure
and extra
arguments act the same as for Client.get()
.
patch
(path, data=”, content_type=’application/octet-stream’, follow=False, secure=False, **extra)
Makes a PATCH request on the provided path
and returns a Response
object. Useful for testing RESTful interfaces.
The follow
, secure
and extra
arguments act the same as for Client.get()
.
delete
(path, data=”, content_type=’application/octet-stream’, follow=False, secure=False, **extra)
Makes an DELETE request on the provided path
and returns a Response
object. Useful for testing RESTful interfaces.
When data
is provided, it is used as the request body, and a Content-Type
header is set to content_type
.
The follow
, secure
and extra
arguments act the same as for Client.get()
.
trace
(path, follow=False, secure=False, **extra)
Makes a TRACE request on the provided path
and returns a Response
object. Useful for simulating diagnostic probes.
Unlike the other request methods, data
is not provided as a keyword parameter in order to comply withRFC 2616, which mandates that TRACE requests should not have an entity-body.
The follow
, secure
, and extra
arguments act the same as for Client.get()
.
login
(**credentials)
If your site uses Django’s authentication system and you deal with logging in users, you can use the test client’s login()
method to simulate the effect of a user logging into the site.
After you call this method, the test client will have all the cookies and session data required to pass any login-based tests that may form part of a view.
The format of the credentials
argument depends on which authentication backend you’re using (which is configured by your AUTHENTICATION_BACKENDS
setting). If you’re using the standard authentication backend provided by Django (ModelBackend
), credentials
should be the user’s username and password, provided as keyword arguments:
>>> c = Client()
>>> c.login(username="fred", password="secret")
# Now you can access a view that"s only available to logged-in users.
If you’re using a different authentication backend, this method may require different credentials. It requires whichever credentials are required by your backend’s authenticate()
method.
login()
returns True
if it the credentials were accepted and login was successful.
Finally, you’ll need to remember to create user accounts before you can use this method. As we explained above, the test runner is executed using a test database, which contains no users by default. As a result, user accounts that are valid on your production site will not work under test conditions. You’ll need to create users as part of the test suite – either manually (using the Django model API) or with a test fixture. Remember that if you want your test user to have a password, you can’t set the user’s password by setting the password attribute directly – you must use the set_password()
function to store a correctly hashed password. Alternatively, you can use the create_user()
helper method to create a new user with a correctly hashed password.
logout
()
If your site uses Django’s authentication system, the logout()
method can be used to simulate the effect of a user logging out of your site.
After you call this method, the test client will have all the cookies and session data cleared to defaults. Subsequent requests will appear to come from an AnonymousUser
.
TESTING RESPONSES
The get()
and post()
methods both return a Response
object. This Response
object is not the same as theHttpResponse
object returned by Django views; the test response object has some additional data useful for test code to verify.
Specifically, a Response
object has the following attributes:
class Response
client
The test client that was used to make the request that resulted in the response.
content
The body of the response, as a string. This is the final page content as rendered by the view, or any error message.
context
The template Context
instance that was used to render the template that produced the response content.
If the rendered page used multiple templates, then context
will be a list of Context
objects, in the order in which they were rendered.
Regardless of the number of templates used during rendering, you can retrieve context values using the []
operator. For example, the context variable name
could be retrieved using:
>>> response = client.get("/foo/")
>>> response.context["name"]
"Arthur"
request
The request data that stimulated the response.
wsgi_request
The WSGIRequest
instance generated by the test handler that generated the response.
status_code
The HTTP status of the response, as an integer. See RFC 2616#section-10 for a full list of HTTP status codes.
templates
A list of Template
instances used to render the final content, in the order they were rendered. For each template in the list, use template.name
to get the template’s file name, if the template was loaded from a file. (The name is a string such as "admin/index.html"
.)
resolver_match
An instance of ResolverMatch
for the response. You can use the func
attribute, for example, to verify the view that served the response:
# my_view here is a function based view
self.assertEqual(response.resolver_match.func, my_view)
# class based views need to be compared by name, as the functions
# generated by as_view() won"t be equal
self.assertEqual(response.resolver_match.func.__name__, MyView.as_view().__name__)
If the given URL is not found, accessing this attribute will raise a Resolver404
exception.
You can also use dictionary syntax on the response object to query the value of any settings in the HTTP headers. For example, you could determine the content type of a response using response["Content-Type"]
.
EXCEPTIONS
If you point the test client at a view that raises an exception, that exception will be visible in the test case. You can then use a standard try ... except
block or assertRaises()
to test for exceptions.
The only exceptions that are not visible to the test client are Http404
, PermissionDenied
, SystemExit
, andSuspiciousOperation
. Django catches these exceptions internally and converts them into the appropriate HTTP response codes. In these cases, you can check response.status_code
in your test.
PERSISTENT STATE
The test client is stateful. If a response returns a cookie, then that cookie will be stored in the test client and sent with all subsequent get()
and post()
requests.
Expiration policies for these cookies are not followed. If you want a cookie to expire, either delete it manually or create a new Client
instance (which will effectively delete all cookies).
A test client has two attributes that store persistent state information. You can access these properties as part of a test condition.
Client.
cookies
A Python SimpleCookie
object, containing the current values of all the client cookies. See the documentation of the http.cookies
module for more.
Client.
session
A dictionary-like object containing session information. See the session documentation for full details.
To modify the session and then save it, it must be stored in a variable first (because a new SessionStore
is created every time this property is accessed):
def test_something(self):
session = self.client.session
session["somekey"] = "test"
session.save()
EXAMPLE
The following is a simple unit test using the test client:
import unittest
from django.test import Client
class SimpleTest(unittest.TestCase):
def setUp(self):
# Every test needs a client.
self.client = Client()
def test_details(self):
# Issue a GET request.
response = self.client.get("/customer/details/")
# Check that the response is 200 OK.
self.assertEqual(response.status_code, 200)
# Check that the rendered context contains 5 customers.
self.assertEqual(len(response.context["customers"]), 5)
Provided test case classes
Normal Python unit test classes extend a base class of unittest.TestCase
. Django provides a few extensions of this base class:
SIMPLETESTCASE
class SimpleTestCase
A thin subclass of unittest.TestCase
, it extends it with some basic functionality like:
- Saving and restoring the Python warning machinery state.
- Some useful assertions like:
- Checking that a callable
raises a certain exception
. - Testing form field
rendering and error treatment
. - Testing
HTML responses for the presence/lack of a given fragment
. - Verifying that a template
has/hasn"t been used to generate a given response content
. - Verifying a HTTP
redirect
is performed by the app. - Robustly testing two
HTML fragments
for equality/inequality orcontainment
. - Robustly testing two
XML fragments
for equality/inequality. - Robustly testing two
JSON fragments
for equality.
- Checking that a callable
- The ability to run tests with modified settings.
- Using the
client
Client
. - Custom test-time
URL maps
.
If you need any of the other more complex and heavyweight Django-specific features like:
- Testing or using the ORM.
- Database
fixtures
. - Test skipping based on database backend features.
- The remaining specialized
assert*
methods.
then you should use TransactionTestCase
or TestCase
instead.
SimpleTestCase
inherits from unittest.TestCase
.
Warning
SimpleTestCase
and its subclasses (e.g. TestCase
, …) rely on setUpClass()
and tearDownClass()
to perform some class-wide initialization (e.g. overriding settings). If you need to override those methods, don’t forget to call the super
implementation:
class MyTestCase(TestCase):
@classmethod
def setUpClass(cls):
super(cls, MyTestCase).setUpClass() # Call parent first
...
@classmethod
def tearDownClass(cls):
...
super(cls, MyTestCase).tearDownClass() # Call parent last
TRANSACTIONTESTCASE
class TransactionTestCase
Django’s TestCase
class (described below) makes use of database transaction facilities to speed up the process of resetting the database to a known state at the beginning of each test. A consequence of this, however, is that some database behaviors cannot be tested within a Django TestCase
class. For instance, you cannot test that a block of code is executing within a transaction, as is required when usingselect_for_update()
. In those cases, you should use TransactionTestCase
.
In older versions of Django, the effects of transaction commit and rollback could not be tested within aTestCase
. With the completion of the deprecation cycle of the old-style transaction management in Django 1.8, transaction management commands (e.g. transaction.commit()
) are no longer disabled within TestCase
.
TransactionTestCase
and TestCase
are identical except for the manner in which the database is reset to a known state and the ability for test code to test the effects of commit and rollback:
- A
TransactionTestCase
resets the database after the test runs by truncating all tables. ATransactionTestCase
may call commit and rollback and observe the effects of these calls on the database. - A
TestCase
, on the other hand, does not truncate tables after a test. Instead, it encloses the test code in a database transaction that is rolled back at the end of the test. This guarantees that the rollback at the end of the test restores the database to its initial state.
Warning
TestCase
running on a database that does not support rollback (e.g. MySQL with the MyISAM storage engine), and all instances of TransactionTestCase
, will roll back at the end of the test by deleting all data from the test database.
Apps will not see their data reloaded; if you need this functionality (for example, third-party apps should enable this) you can set serialized_rollback = True
inside the TestCase
body.
TransactionTestCase
inherits from SimpleTestCase
.
TESTCASE
class TestCase
This class provides some additional capabilities that can be useful for testing Web sites.
Converting a normal unittest.TestCase
to a Django TestCase
is easy: Just change the base class of your test from "unittest.TestCase"
to "django.test.TestCase"
. All of the standard Python unit test functionality will continue to be available, but it will be augmented with some useful additions, including:
- Automatic loading of fixtures.
- Wraps the tests within two nested
atomic
blocks: one for the whole class and one for each test. - Creates a TestClient instance.
- Django-specific assertions for testing for things like redirection and form errors.
classmethod TestCase.
setUpTestData
()
The class-level atomic
block described above allows the creation of initial data at the class level, once for the whole TestCase
. This technique allows for faster tests as compared to using setUp()
.
For example:
from django.test import TestCase
class MyTests(TestCase):
@classmethod
def setUpTestData(cls):
# Set up data for the whole TestCase
cls.foo = Foo.objects.create(bar="Test")
...
def test1(self):
# Some test using self.foo
...
def test2(self):
# Some other test using self.foo
...
Note that if the tests are run on a database with no transaction support (for instance, MySQL with the MyISAM engine), setUpTestData()
will be called before each test, negating the speed benefits.
Warning
If you want to test some specific database transaction behavior, you should use TransactionTestCase
, asTestCase
wraps test execution within an atomic()
block.
TestCase
inherits from TransactionTestCase
.
LIVESERVERTESTCASE
class LiveServerTestCase
LiveServerTestCase
does basically the same as TransactionTestCase
with one extra feature: it launches a live Django server in the background on setup, and shuts it down on teardown. This allows the use of automated test clients other than the Django dummy client such as, for example, the Selenium client, to execute a series of functional tests inside a browser and simulate a real user’s actions.
By default the live server’s address is "localhost:8081"
and the full URL can be accessed during the tests with self.live_server_url
. If you’d like to change the default address (in the case, for example, where the 8081 port is already taken) then you may pass a different one to the test command via the --liveserver
option, for example:
./manage.py test --liveserver=localhost:8082
Another way of changing the default server address is by setting the DJANGO_LIVE_TEST_SERVER_ADDRESSenvironment variable somewhere in your code (for example, in a custom test runner):
import os
os.environ["DJANGO_LIVE_TEST_SERVER_ADDRESS"] = "localhost:8082"
In the case where the tests are run by multiple processes in parallel (for example, in the context of several simultaneous continuous integration builds), the processes will compete for the same address, and therefore your tests might randomly fail with an “Address already in use” error. To avoid this problem, you can pass a comma-separated list of ports or ranges of ports (at least as many as the number of potential parallel processes). For example:
./manage.py test --liveserver=localhost:8082,8090-8100,9000-9200,7041
Then, during test execution, each new live test server will try every specified port until it finds one that is free and takes it.
To demonstrate how to use LiveServerTestCase
, let’s write a simple Selenium test. First of all, you need to install the selenium package into your Python path:
pip install selenium
Then, add a LiveServerTestCase
-based test to your app’s tests module (for example: myapp/tests.py
). The code for this test may look as follows:
from django.test import LiveServerTestCase
from selenium.webdriver.firefox.webdriver import WebDriver
class MySeleniumTests(LiveServerTestCase):
fixtures = ["user-data.json"]
@classmethod
def setUpClass(cls):
super(MySeleniumTests, cls).setUpClass()
cls.selenium = WebDriver()
@classmethod
def tearDownClass(cls):
cls.selenium.quit()
super(MySeleniumTests, cls).tearDownClass()
def test_login(self):
self.selenium.get("%s%s" % (self.live_server_url, "/login/"))
username_input = self.selenium.find_element_by_name("username")
username_input.send_keys("myuser")
password_input = self.selenium.find_element_by_name("password")
password_input.send_keys("secret")
self.selenium.find_element_by_xpath("//input[@value="Log in"]").click()
Finally, you may run the test as follows:
./manage.py test myapp.tests.MySeleniumTests.test_login
This example will automatically open Firefox then go to the login page, enter the credentials and press the “Log in” button. Selenium offers other drivers in case you do not have Firefox installed or wish to use another browser. The example above is just a tiny fraction of what the Selenium client can do; check out the full reference for more details.
Tip
If you use the staticfiles
app in your project and need to perform live testing, then you might want to use the StaticLiveServerTestCase
subclass which transparently serves all the assets during execution of its tests in a way very similar to what we get at development time with DEBUG=True
, i.e. without having to collect them using collectstatic
.
Note
When using an in-memory SQLite database to run the tests, the same database connection will be shared by two threads in parallel: the thread in which the live server is run and the thread in which the test case is run. It’s important to prevent simultaneous database queries via this shared connection by the two threads, as that may sometimes randomly cause the tests to fail. So you need to ensure that the two threads don’t access the database at the same time. In particular, this means that in some cases (for example, just after clicking a link or submitting a form), you might need to check that a response is received by Selenium and that the next page is loaded before proceeding with further test execution. Do this, for example, by making Selenium wait until the <body>
HTML tag is found in the response (requires Selenium > 2.13):
def test_login(self):
from selenium.webdriver.support.wait import WebDriverWait
timeout = 2
...
self.selenium.find_element_by_xpath("//input[@value="Log in"]").click()
# Wait until the response is received
WebDriverWait(self.selenium, timeout).until(
lambda driver: driver.find_element_by_tag_name("body"))
The tricky thing here is that there’s really no such thing as a “page load,” especially in modern Web apps that generate HTML dynamically after the server generates the initial document. So, simply checking for the presence of <body>
in the response might not necessarily be appropriate for all use cases. Please refer to the Selenium FAQ and Selenium documentation for more information.
Test cases features
DEFAULT TEST CLIENT
SimpleTestCase.
client
Every test case in a django.test.*TestCase
instance has access to an instance of a Django test client. This client can be accessed as self.client
. This client is recreated for each test, so you don’t have to worry about state (such as cookies) carrying over from one test to another.
This means, instead of instantiating a Client
in each test:
import unittest
from django.test import Client
class SimpleTest(unittest.TestCase):
def test_details(self):
client = Client()
response = client.get("/customer/details/")
self.assertEqual(response.status_code, 200)
def test_index(self):
client = Client()
response = client.get("/customer/index/")
self.assertEqual(response.status_code, 200)
…you can just refer to self.client
, like so:
from django.test import TestCase
class SimpleTest(TestCase):
def test_details(self):
response = self.client.get("/customer/details/")
self.assertEqual(response.status_code, 200)
def test_index(self):
response = self.client.get("/customer/index/")
self.assertEqual(response.status_code, 200)
CUSTOMIZING THE TEST CLIENT
SimpleTestCase.
client_class
If you want to use a different Client
class (for example, a subclass with customized behavior), use theclient_class
class attribute:
from django.test import TestCase, Client
class MyTestClient(Client):
# Specialized methods for your environment
...
class MyTest(TestCase):
client_class = MyTestClient
def test_my_stuff(self):
# Here self.client is an instance of MyTestClient...
call_some_test_code()
FIXTURE LOADING
TransactionTestCase.
fixtures
A test case for a database-backed Web site isn’t much use if there isn’t any data in the database. To make it easy to put test data into the database, Django’s custom TransactionTestCase
class provides a way of loading fixtures.
A fixture is a collection of data that Django knows how to import into a database. For example, if your site has user accounts, you might set up a fixture of fake user accounts in order to populate your database during tests.
The most straightforward way of creating a fixture is to use the manage.py dumpdata command. This assumes you already have some data in your database. See the dumpdata documentation for more details.
Once you’ve created a fixture and placed it in a fixtures
directory in one of your INSTALLED_APPS
, you can use it in your unit tests by specifying a fixtures
class attribute on your django.test.TestCase
subclass:
from django.test import TestCase
from myapp.models import Animal
class AnimalTestCase(TestCase):
fixtures = ["mammals.json", "birds"]
def setUp(self):
# Test definitions as before.
call_setup_methods()
def testFluffyAnimals(self):
# A test that uses the fixtures.
call_some_test_code()
Here’s specifically what will happen:
- At the start of each test case, before
setUp()
is run, Django will flush the database, returning the database to the state it was in directly aftermigrate
was called. - Then, all the named fixtures are installed. In this example, Django will install any JSON fixture named
mammals
, followed by any fixture namedbirds
. See theloaddata
documentation for more details on defining and installing fixtures.
This flush/load procedure is repeated for each test in the test case, so you can be certain that the outcome of a test will not be affected by another test, or by the order of test execution.
By default, fixtures are only loaded into the default
database. If you are using multiple databases and setmulti_db=True
, fixtures will be loaded into all databases.
MULTI-DATABASE SUPPORT
TransactionTestCase.
multi_db
Django sets up a test database corresponding to every database that is defined in the DATABASES
definition in your settings file. However, a big part of the time taken to run a Django TestCase is consumed by the call to flush
that ensures that you have a clean database at the start of each test run. If you have multiple databases, multiple flushes are required (one for each database), which can be a time consuming activity – especially if your tests don’t need to test multi-database activity.
As an optimization, Django only flushes the default
database at the start of each test run. If your setup contains multiple databases, and you have a test that requires every database to be clean, you can use themulti_db
attribute on the test suite to request a full flush.
For example:
class TestMyViews(TestCase):
multi_db = True
def testIndexPageView(self):
call_some_test_code()
This test case will flush all the test databases before running testIndexPageView
.
The multi_db
flag also affects into which databases the attr:TransactionTestCase.fixtures are loaded. By default (when multi_db=False
), fixtures are only loaded into the default
database. If multi_db=True
, fixtures are loaded into all databases.
OVERRIDING SETTINGS
Warning
Use the functions below to temporarily alter the value of settings in tests. Don’t manipulatedjango.conf.settings
directly as Django won’t restore the original values after such manipulations.
SimpleTestCase.
settings
()
For testing purposes it’s often useful to change a setting temporarily and revert to the original value after running the testing code. For this use case Django provides a standard Python context manager (see PEP 343) called settings()
, which can be used like this:
from django.test import TestCase
class LoginTestCase(TestCase):
def test_login(self):
# First check for the default behavior
response = self.client.get("/sekrit/")
self.assertRedirects(response, "/accounts/login/?next=/sekrit/")
# Then override the LOGIN_URL setting
with self.settings(LOGIN_URL="/other/login/"):
response = self.client.get("/sekrit/")
self.assertRedirects(response, "/other/login/?next=/sekrit/")
This example will override the LOGIN_URL
setting for the code in the with
block and reset its value to the previous state afterwards.
SimpleTestCase.
modify_settings
()
It can prove unwieldy to redefine settings that contain a list of values. In practice, adding or removing values is often sufficient. The modify_settings()
context manager makes it easy:
from django.test import TestCase
class MiddlewareTestCase(TestCase):
def test_cache_middleware(self):
with self.modify_settings(MIDDLEWARE_CLASSES={
"append": "django.middleware.cache.FetchFromCacheMiddleware",
"prepend": "django.middleware.cache.UpdateCacheMiddleware",
"remove": [
"django.contrib.sessions.middleware.SessionMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
],
}):
response = self.client.get("/")
# ...
For each action, you can supply either a list of values or a string. When the value already exists in the list,append
and prepend
have no effect; neither does remove
when the value doesn’t exist.
override_settings
()
In case you want to override a setting for a test method, Django provides the override_settings()
decorator (see PEP 318). It’s used like this:
from django.test import TestCase, override_settings
class LoginTestCase(TestCase):
@override_settings(LOGIN_URL="/other/login/")
def test_login(self):
response = self.client.get("/sekrit/")
self.assertRedirects(response, "/other/login/?next=/sekrit/")
The decorator can also be applied to TestCase
classes:
from django.test import TestCase, override_settings
@override_settings(LOGIN_URL="/other/login/")
class LoginTestCase(TestCase):
def test_login(self):
response = self.client.get("/sekrit/")
self.assertRedirects(response, "/other/login/?next=/sekrit/")
modify_settings
()
Likewise, Django provides the modify_settings()<