django-webtest provides integration of Ian Bicking's WebTest ( with django's testing framework.
Python HTML
Latest commit 49887a8 Feb 21, 2017 @gawel gawel remove py26 tests



PyPI Version License Build Status

django-webtest is an app for instant integration of Ian Bicking's WebTest ( with django's testing framework.


$ pip install django-webtest


from django_webtest import WebTest

class MyTestCase(WebTest):

    # optional: we want some initial data to be able to login
    fixtures = ['users', 'blog_posts']

    # optional: default extra_environ for this TestCase
    extra_environ = {'HTTP_ACCEPT_LANGUAGE': 'ru'}

    def testBlog(self):
        # pretend to be logged in as user `kmike` and go to the index page
        index ='/', user='kmike')

        # All the webtest API is available. For example, we click
        # on a <a href='/tech-blog/'>Blog</a> link, check that it
        # works (result page doesn't raise exceptions and returns 200 http
        # code) and test if result page have 'My Article' text in
        # its body.
        assert 'My Article' in'Blog')

django-webtest provides a django.test.TestCase subclass (django_webtest.WebTest) that creates webtest.TestApp around django wsgi interface and makes it available in tests as

It also features optional user argument for and methods to help making authorized requests. This argument should be a django.contrib.auth.models.User instance or a string with user's username for the user who is supposed to be logged in. To log out again, call, clearing all cookies.

For 500 errors original traceback is shown instead of usual html result from handler500.

You also get the response.templates and response.context goodness that is usually only available if you use django's native test client. These attributes contain a list of templates that were used to render the response and the context used to render these templates. All of django's native asserts ( assertFormError, assertTemplateUsed, assertTemplateNotUsed, assertContains, assertNotContains, assertRedirects) are also supported for WebTest responses.

The session dictionary is available via, and has the same content than django's native test client.

Unlike django's native test client CSRF checks are not suppressed by default so missing CSRF tokens will cause test fails (and that's good).

If forms are submitted via WebTest forms API then all form fields (including CSRF token) are submitted automagically:

class AuthTest(WebTest):
    fixtures = ['users.json']

    def test_login(self)
        form ='auth_login')).form
        form['username'] = 'foo'
        form['password'] = 'bar'
        response = form.submit().follow()
        self.assertEqual(response.context['user'].username, 'foo')

However if forms are submitted via raw POST requests using then csrf tokens become hard to construct. CSRF checks can be disabled by setting csrf_checks attribute to False in this case:

class MyTestCase(WebTest):
    csrf_checks = False
    def test_post(self)'/')

When a subclass of django's TransactionTestCase is desired, use django_webtest.TransactionWebTest.

All of these features can be easily set up manually (thanks to WebTest architecture) and they are even not neccessary for using WebTest with django but it is nice to have some sort of integration instantly.

See for API help. Webtest can follow links, submit forms, parse html, xml and json responses with different parsing libraries, upload files and more.


While django.test.client.Client is fine for its purposes, it is not well-suited for functional or integration testing. From django's test client docstring:

This is not intended as a replacement for Twill/Selenium or the like - it is here to allow testing against the contexts and templates produced by a view, rather than the HTML rendered to the end-user.

WebTest plays on the same field as twill. WebTest has a nice API, is fast, small, talks to the django application via WSGI instead of HTTP and is an easy way to write functional/integration/acceptance tests. django-webtest is able to provide access to the names of rendered templates and template context just like native django TestClient.


Development happens at github: Issue tracker:

Feel free to submit ideas, bugs or pull requests.

Running tests

Make sure tox is installed and run

$ tox

from the source checkout.