Additional assertions and test conveniences for testing django sites.
Table of Contents
All classes inherit from django.test.TestCase so you can access the Django assertions and the HTTP client as usual.
django-test-mixins is available on PyPI. Installation is simply a matter of:
$ pip install django_test_mixins
HttpCodeTestCase
provides the following assertions for exact HTTP responses:
assertHttpOK(response)
(200)assertHttpCreated(response)
(201)assertHttpBadRequest(response)
(400)assertHttpUnauthorized(response)
(401)assertHttpForbidden(response)
(403)assertHttpNotFound(response)
(404)assertHttpMethodNotAllowed(response)
(405)
It also provides the following assertions for groups of HTTP responses:
assertHttpRedirect(response, location=None)
(3XX)
Example:
from django.core.urlresolvers import reverse
from django_test_mixins import HttpCodeTestCase
class TestIndex(HttpCodeTestCase):
def test_home_page(self):
response = self.client.get(reverse('index'))
self.assertHttpOK(response)
EmptyCacheTestCase
provides no extra assertions, but ensures that
every test starts with a empty cache.
FormValidationTestCase
provides the following assertion:
assertFormInvalid(response, form_name="form")
Example:
Assume we have a view that looks like this:
from django.shortcuts import render, redirect
from .forms import HouseForm
def create_house(request):
if request.method == 'POST':
form = HouseForm(request.POST)
if form.is_valid():
form.save()
return redirect('index')
else:
form = HouseForm()
return render(request, "create_house.html", {'house_form': form})
We can then write a test like this. Note that form_name
needs to
match the context we rendered the page with (defaults to "form"
).
from django.core.urlresolvers import reverse
from django_test_mixins import FormValidationTestCase
class TestCreateHouse(FormValidationTestCase):
def test_create_requires_name(self):
response = self.client.post(reverse('create_house'), {})
self.assertFormInvalid(response, form_name="house_form")
RedirectTestCase
provides the following assertions:
assertRedirectsTo(response, expected_url)
Django has TestCase.assertRedirects
(docs)
but this does not support redirects to external URLs because of
limitations in the test client.
assertRedirectsTo
does not support chained redirects, so you can't
do self.client.get(SOME_URL, follow=True)
.
Example:
from django.core.urlresolvers import reverse
from django_test_mixins import FormValidationTestCase
class TestIndex(RedirectTestCase):
def test_home_redirects_to_login(self):
"""If the user isn't logged in, index should redirect to the login page.
"""
response = self.client.get(reverse('index'))
self.assertRedirectsTo(response, reverse('log_in'))
You can freely combine these classes by simply inheriting from multiple classes.
from django.core.urlresolvers import reverse
from django_test_mixins import FreshCacheTestCase, HttpCodeTestCase
class TestIndex(FreshCacheTestCase, HttpCodeTestCase):
def test_home_page(self):
response = self.client.get(reverse('index'))
self.assertHttpOK(response)
Releasing a new version is a matter of:
$ python setup.py sdist upload