Permalink
Browse files

Fixed #10314 -- Added a message prefix argument to Django's test asse…

…rtions. Thanks to Wes Winham for the original suggestion, and Chistian Oudard for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12273 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 1b3dc8a commit 7ca9d9306c5e5e175668af513725a16ba3b26d9d @freakboy3742 freakboy3742 committed Jan 22, 2010
Showing with 198 additions and 56 deletions.
  1. +1 −0 AUTHORS
  2. +74 −49 django/test/testcases.py
  3. +15 −6 docs/topics/testing.txt
  4. +108 −1 tests/regressiontests/test_client_regress/models.py
View
@@ -339,6 +339,7 @@ answer newbie questions, and generally made Django that much better:
Afonso Fernández Nogueira <fonzzo.django@gmail.com>
Neal Norwitz <nnorwitz@google.com>
Todd O'Bryan <toddobryan@mac.com>
+ Christian Oudard <christian.oudard@gmail.com>
oggie rob <oz.robharvey@gmail.com>
oggy <ognjen.maric@gmail.com>
Jay Parlar <parlar@gmail.com>
@@ -287,34 +287,41 @@ def _urlconf_teardown(self):
clear_url_caches()
def assertRedirects(self, response, expected_url, status_code=302,
- target_status_code=200, host=None):
+ target_status_code=200, host=None, msg_prefix=''):
"""Asserts that a response redirected to a specific URL, and that the
redirect URL can be loaded.
Note that assertRedirects won't work for external links since it uses
TestClient to do a request.
"""
+ if msg_prefix:
+ msg_prefix += ": "
+
if hasattr(response, 'redirect_chain'):
# The request was a followed redirect
self.failUnless(len(response.redirect_chain) > 0,
- ("Response didn't redirect as expected: Response code was %d"
- " (expected %d)" % (response.status_code, status_code)))
+ msg_prefix + "Response didn't redirect as expected: Response"
+ " code was %d (expected %d)" %
+ (response.status_code, status_code))
self.assertEqual(response.redirect_chain[0][1], status_code,
- ("Initial response didn't redirect as expected: Response code was %d"
- " (expected %d)" % (response.redirect_chain[0][1], status_code)))
+ msg_prefix + "Initial response didn't redirect as expected:"
+ " Response code was %d (expected %d)" %
+ (response.redirect_chain[0][1], status_code))
url, status_code = response.redirect_chain[-1]
self.assertEqual(response.status_code, target_status_code,
- ("Response didn't redirect as expected: Final Response code was %d"
- " (expected %d)" % (response.status_code, target_status_code)))
+ msg_prefix + "Response didn't redirect as expected: Final"
+ " Response code was %d (expected %d)" %
+ (response.status_code, target_status_code))
else:
# Not a followed redirect
self.assertEqual(response.status_code, status_code,
- ("Response didn't redirect as expected: Response code was %d"
- " (expected %d)" % (response.status_code, status_code)))
+ msg_prefix + "Response didn't redirect as expected: Response"
+ " code was %d (expected %d)" %
+ (response.status_code, status_code))
url = response['Location']
scheme, netloc, path, query, fragment = urlsplit(url)
@@ -324,63 +331,74 @@ def assertRedirects(self, response, expected_url, status_code=302,
# Get the redirection page, using the same client that was used
# to obtain the original response.
self.assertEqual(redirect_response.status_code, target_status_code,
- ("Couldn't retrieve redirection page '%s': response code was %d"
- " (expected %d)") %
- (path, redirect_response.status_code, target_status_code))
+ msg_prefix + "Couldn't retrieve redirection page '%s':"
+ " response code was %d (expected %d)" %
+ (path, redirect_response.status_code, target_status_code))
e_scheme, e_netloc, e_path, e_query, e_fragment = urlsplit(expected_url)
if not (e_scheme or e_netloc):
expected_url = urlunsplit(('http', host or 'testserver', e_path,
e_query, e_fragment))
self.assertEqual(url, expected_url,
- "Response redirected to '%s', expected '%s'" % (url, expected_url))
-
+ msg_prefix + "Response redirected to '%s', expected '%s'" %
+ (url, expected_url))
- def assertContains(self, response, text, count=None, status_code=200):
+ def assertContains(self, response, text, count=None, status_code=200,
+ msg_prefix=''):
"""
Asserts that a response indicates that a page was retrieved
successfully, (i.e., the HTTP status code was as expected), and that
``text`` occurs ``count`` times in the content of the response.
If ``count`` is None, the count doesn't matter - the assertion is true
if the text occurs at least once in the response.
"""
+ if msg_prefix:
+ msg_prefix += ": "
+
self.assertEqual(response.status_code, status_code,
- "Couldn't retrieve page: Response code was %d (expected %d)'" %
- (response.status_code, status_code))
+ msg_prefix + "Couldn't retrieve page: Response code was %d"
+ " (expected %d)" % (response.status_code, status_code))
text = smart_str(text, response._charset)
real_count = response.content.count(text)
if count is not None:
self.assertEqual(real_count, count,
- "Found %d instances of '%s' in response (expected %d)" %
- (real_count, text, count))
+ msg_prefix + "Found %d instances of '%s' in response"
+ " (expected %d)" % (real_count, text, count))
else:
self.failUnless(real_count != 0,
- "Couldn't find '%s' in response" % text)
+ msg_prefix + "Couldn't find '%s' in response" % text)
- def assertNotContains(self, response, text, status_code=200):
+ def assertNotContains(self, response, text, status_code=200,
+ msg_prefix=''):
"""
Asserts that a response indicates that a page was retrieved
successfully, (i.e., the HTTP status code was as expected), and that
``text`` doesn't occurs in the content of the response.
"""
+ if msg_prefix:
+ msg_prefix += ": "
+
self.assertEqual(response.status_code, status_code,
- "Couldn't retrieve page: Response code was %d (expected %d)'" %
- (response.status_code, status_code))
+ msg_prefix + "Couldn't retrieve page: Response code was %d"
+ " (expected %d)" % (response.status_code, status_code))
text = smart_str(text, response._charset)
- self.assertEqual(response.content.count(text),
- 0, "Response should not contain '%s'" % text)
+ self.assertEqual(response.content.count(text), 0,
+ msg_prefix + "Response should not contain '%s'" % text)
- def assertFormError(self, response, form, field, errors):
+ def assertFormError(self, response, form, field, errors, msg_prefix=''):
"""
Asserts that a form used to render the response has a specific field
error.
"""
+ if msg_prefix:
+ msg_prefix += ": "
+
# Put context(s) into a list to simplify processing.
contexts = to_list(response.context)
if not contexts:
- self.fail('Response did not use any contexts to render the'
- ' response')
+ self.fail(msg_prefix + "Response did not use any contexts to"
+ "render the response")
# Put error(s) into a list to simplify processing.
errors = to_list(errors)
@@ -396,50 +414,57 @@ def assertFormError(self, response, form, field, errors):
if field in context[form].errors:
field_errors = context[form].errors[field]
self.failUnless(err in field_errors,
- "The field '%s' on form '%s' in"
- " context %d does not contain the"
- " error '%s' (actual errors: %s)" %
- (field, form, i, err,
- repr(field_errors)))
+ msg_prefix + "The field '%s' on form '%s' in"
+ " context %d does not contain the error '%s'"
+ " (actual errors: %s)" %
+ (field, form, i, err, repr(field_errors)))
elif field in context[form].fields:
- self.fail("The field '%s' on form '%s' in context %d"
- " contains no errors" % (field, form, i))
+ self.fail(msg_prefix + "The field '%s' on form '%s'"
+ " in context %d contains no errors" %
+ (field, form, i))
else:
- self.fail("The form '%s' in context %d does not"
- " contain the field '%s'" %
+ self.fail(msg_prefix + "The form '%s' in context %d"
+ " does not contain the field '%s'" %
(form, i, field))
else:
non_field_errors = context[form].non_field_errors()
self.failUnless(err in non_field_errors,
- "The form '%s' in context %d does not contain the"
- " non-field error '%s' (actual errors: %s)" %
+ msg_prefix + "The form '%s' in context %d does not"
+ " contain the non-field error '%s'"
+ " (actual errors: %s)" %
(form, i, err, non_field_errors))
if not found_form:
- self.fail("The form '%s' was not used to render the response" %
- form)
+ self.fail(msg_prefix + "The form '%s' was not used to render the"
+ " response" % form)
- def assertTemplateUsed(self, response, template_name):
+ def assertTemplateUsed(self, response, template_name, msg_prefix=''):
"""
Asserts that the template with the provided name was used in rendering
the response.
"""
+ if msg_prefix:
+ msg_prefix += ": "
+
template_names = [t.name for t in to_list(response.template)]
if not template_names:
- self.fail('No templates used to render the response')
+ self.fail(msg_prefix + "No templates used to render the response")
self.failUnless(template_name in template_names,
- (u"Template '%s' was not a template used to render the response."
- u" Actual template(s) used: %s") % (template_name,
- u', '.join(template_names)))
+ msg_prefix + "Template '%s' was not a template used to render"
+ " the response. Actual template(s) used: %s" %
+ (template_name, u', '.join(template_names)))
- def assertTemplateNotUsed(self, response, template_name):
+ def assertTemplateNotUsed(self, response, template_name, msg_prefix=''):
"""
Asserts that the template with the provided name was NOT used in
rendering the response.
"""
+ if msg_prefix:
+ msg_prefix += ": "
+
template_names = [t.name for t in to_list(response.template)]
self.failIf(template_name in template_names,
- (u"Template '%s' was used unexpectedly in rendering the"
- u" response") % template_name)
+ msg_prefix + "Template '%s' was used unexpectedly in rendering"
+ " the response" % template_name)
def connections_support_transactions():
"""
@@ -1104,23 +1104,32 @@ Assertions
.. versionadded:: 1.0
+.. versionchanged:: 1.2
+ Addded ``msg_prefix`` argument.
+
As Python's normal ``unittest.TestCase`` class implements assertion methods
such as ``assertTrue`` and ``assertEquals``, Django's custom ``TestCase`` class
provides a number of custom assertion methods that are useful for testing Web
applications:
-.. method:: TestCase.assertContains(response, text, count=None, status_code=200)
+The failure messages given by the assertion methods can be customized
+with the ``msg_prefix`` argument. This string will be prefixed to any
+failure message generated by the assertion. This allows you to provide
+additional details that may help you to identify the location and
+cause of an failure in your test suite.
+
+.. method:: TestCase.assertContains(response, text, count=None, status_code=200, msg_prefix='')
Asserts that a ``Response`` instance produced the given ``status_code`` and
that ``text`` appears in the content of the response. If ``count`` is
provided, ``text`` must occur exactly ``count`` times in the response.
-.. method:: TestCase.assertNotContains(response, text, status_code=200)
+.. method:: TestCase.assertNotContains(response, text, status_code=200, msg_prefix='')
Asserts that a ``Response`` instance produced the given ``status_code`` and
that ``text`` does not appears in the content of the response.
-.. method:: TestCase.assertFormError(response, form, field, errors)
+.. method:: TestCase.assertFormError(response, form, field, errors, msg_prefix='')
Asserts that a field on a form raises the provided list of errors when
rendered on the form.
@@ -1135,19 +1144,19 @@ applications:
``errors`` is an error string, or a list of error strings, that are
expected as a result of form validation.
-.. method:: TestCase.assertTemplateUsed(response, template_name)
+.. method:: TestCase.assertTemplateUsed(response, template_name, msg_prefix='')
Asserts that the template with the given name was used in rendering the
response.
The name is a string such as ``'admin/index.html'``.
-.. method:: TestCase.assertTemplateNotUsed(response, template_name)
+.. method:: TestCase.assertTemplateNotUsed(response, template_name, msg_prefix='')
Asserts that the template with the given name was *not* used in rendering
the response.
-.. method:: TestCase.assertRedirects(response, expected_url, status_code=302, target_status_code=200)
+.. method:: TestCase.assertRedirects(response, expected_url, status_code=302, target_status_code=200, msg_prefix='')
Asserts that the response return a ``status_code`` redirect status, it
redirected to ``expected_url`` (including any GET data), and the final
Oops, something went wrong.

0 comments on commit 7ca9d93

Please sign in to comment.