Skip to main content

Instant integration of Ian Bicking's WebTest (http://pythonpaste.org/webtest/) with django's testing framework.

Project description

django-webtest is an app for instant integration of Ian Bicking’s WebTest (http://pythonpaste.org/webtest/) with django’s testing framework.

Usage

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 = {'REMOTE_ADDR': '10.20.30.1'}

    def testBlog(self):
        # pretend to be logged in as user `kmike` and go to the index page
        index = self.app.get('/', 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
        # it's body.
        assert 'My Article' in index.click('Blog')

django-webtest provides django.test.TestCase subclass (WebTest) that creates webtest.TestApp around django wsgi interface and make it available in tests as self.app.

It also features optional user argument for self.app.get and self.app.post methods to help making authorized requests. This argument should be django.contrib.auth.models.User instance or a string with user’s username for user who is supposed to be logged in.

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

You also get the response.template 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.

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)
        login_form = self.app.get(reverse('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 app.post 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)
        self.app.post('/')

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 http://pythonpaste.org/webtest/ for API help. It can follow links, submit forms, parse html, xml and json responses with different parsing libraries, upload files and more.

Installation

$ pip install webtest
$ pip install django-webtest

or

$ easy_install webtest
$ easy_install django-webtest

or grab latest versions from bitbucket (http://bitbucket.org/ianb/webtest, http://bitbucket.org/kmike/django-webtest)

Why?

While django.test.client.Client is fine for it’s 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 nice API, is fast, small, talk to django application via WSGI instead of HTTP and is an easy way to write functional/integration/acceptance tests.

Twill is also a great tool and it also can be easily integrated with django (see django-test-utils package) and I also enjoy it much. But I prefer WebTest over twill because twill is old (last release is in 2007), communicate via HTTP instead of WSGI (though there is workaround for that), lacks support for unicode and have a much larger codebase to hack on. django-webtest also is able to provide access to the names of rendered templates and template context just like native django TestClient. Twill however understands HTML better and is more mature so consider it if WebTest doesn’t fit for some reason.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-webtest-1.2.1.tar.gz (6.0 kB view details)

Uploaded Source

File details

Details for the file django-webtest-1.2.1.tar.gz.

File metadata

File hashes

Hashes for django-webtest-1.2.1.tar.gz
Algorithm Hash digest
SHA256 54e48cc79401ca7a3978f49b8cc09d3f7d2a406d785791da9be49ae23fbc3ec5
MD5 76712d2ab7a40c2359e0b4d9ccdd7209
BLAKE2b-256 60385e6983e2cfc16593ed4673b84606ca600896ee15d7665ae5b7b0c67ff39b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page