diff options
| author | Russell Keith-Magee <russell@keith-magee.com> | 2007-05-05 03:03:33 +0000 |
|---|---|---|
| committer | Russell Keith-Magee <russell@keith-magee.com> | 2007-05-05 03:03:33 +0000 |
| commit | a0ef3ba2f7e815b4f3617f6e2827f66c13de3194 (patch) | |
| tree | 5f2289eaef2d65c8c5bdc43b4632c57da2de342c /django/test/testcases.py | |
| parent | e986e8aba4d301c2be2b3a2607ee57e7de2fb494 (diff) | |
Added a default test Client to TestCase, and added some assertions for some common testing patterns.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5150 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/test/testcases.py')
| -rw-r--r-- | django/test/testcases.py | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/django/test/testcases.py b/django/test/testcases.py index 2bfb9a733a..efe392590f 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -1,8 +1,10 @@ import re, doctest, unittest +from urlparse import urlparse from django.db import transaction from django.core import management from django.db.models import get_apps - +from django.test.client import Client + normalize_long_ints = lambda s: re.sub(r'(?<![\w])(\d+)L(?![\w])', '\\1', s) class OutputChecker(doctest.OutputChecker): @@ -46,5 +48,33 @@ class TestCase(unittest.TestCase): super(). """ + self.client = Client() self.install_fixtures() super(TestCase, self).run(result) + + def assertRedirects(self, response, expected_path): + """Assert that a response redirected to a specific URL, and that the + redirect URL can be loaded. + + """ + self.assertEqual(response.status_code, 302, + "Response didn't redirect: Reponse code was %d" % response.status_code) + scheme, netloc, path, params, query, fragment = urlparse(response['Location']) + self.assertEqual(path, expected_path, + "Response redirected to '%s', expected '%s'" % (path, expected_path)) + redirect_response = self.client.get(path) + self.assertEqual(redirect_response.status_code, 200, + "Couldn't retrieve redirection page '%s'" % path) + + def assertContains(self, response, text, count=1): + """Assert that a response indicates that a page was retreived successfully, + (i.e., the HTTP status code was 200), and that ``text`` occurs ``count`` + times in the content of the response. + + """ + self.assertEqual(response.status_code, 200, + "Couldn't retrieve page'") + real_count = response.content.count(text) + self.assertEqual(real_count, count, + "Could only find %d of %d instances of '%s' in response" % (real_count, count, text)) +
\ No newline at end of file |
