Skip to content

Instantly share code, notes, and snippets.

@jphalip
Last active August 29, 2015 14:02
Show Gist options
  • Save jphalip/a83cc1d2a3455a915ba5 to your computer and use it in GitHub Desktop.
Save jphalip/a83cc1d2a3455a915ba5 to your computer and use it in GitHub Desktop.
Example using the WebTest client for Django tests
from django.test import TestCase
from django_webtest import WebTest
"""
WebTest rocks. You should check it out:
http://webtest.readthedocs.org/en/latest/
https://pypi.python.org/pypi/django-webtest
"""
class TraditionalTest()
"""
Traditional way of testig a Django form, which
does not test the form's HTML at all. The Django
view is called directly via `self.client.post()`
without ever checking if the form's HTML code
actually works. Therefore no potential bugs in
the form's HTML will ever get caught by this test.
"""
response = self.client.post(reverse('contact'), {
'subject': 'aaa',
'message': 'bbb',
})
# ...
class BetterTest(WebTest):
"""
This test does exactly the same except it also
validates the form's HTML code (as if rendered by a
real browser). So, any HTML bug preventing the actual
form from working properly will cause this test to fail.
The CSRF token will also be checked.
"""
form = self.app.get(reverse('contact')).form
form['subject'] = 'aaa'
form['message'] = 'bbb'
response = form.submit()
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment