mirror of https://github.com/django/django.git
Fixed #10314 -- Added a message prefix argument to Django's test assertions. 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
This commit is contained in:
parent
1b3dc8ad9a
commit
7ca9d9306c
1
AUTHORS
1
AUTHORS
|
@ -339,6 +339,7 @@ answer newbie questions, and generally made Django that much better:
|
||||||
Afonso Fernández Nogueira <fonzzo.django@gmail.com>
|
Afonso Fernández Nogueira <fonzzo.django@gmail.com>
|
||||||
Neal Norwitz <nnorwitz@google.com>
|
Neal Norwitz <nnorwitz@google.com>
|
||||||
Todd O'Bryan <toddobryan@mac.com>
|
Todd O'Bryan <toddobryan@mac.com>
|
||||||
|
Christian Oudard <christian.oudard@gmail.com>
|
||||||
oggie rob <oz.robharvey@gmail.com>
|
oggie rob <oz.robharvey@gmail.com>
|
||||||
oggy <ognjen.maric@gmail.com>
|
oggy <ognjen.maric@gmail.com>
|
||||||
Jay Parlar <parlar@gmail.com>
|
Jay Parlar <parlar@gmail.com>
|
||||||
|
|
|
@ -287,34 +287,41 @@ class TransactionTestCase(unittest.TestCase):
|
||||||
clear_url_caches()
|
clear_url_caches()
|
||||||
|
|
||||||
def assertRedirects(self, response, expected_url, status_code=302,
|
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
|
"""Asserts that a response redirected to a specific URL, and that the
|
||||||
redirect URL can be loaded.
|
redirect URL can be loaded.
|
||||||
|
|
||||||
Note that assertRedirects won't work for external links since it uses
|
Note that assertRedirects won't work for external links since it uses
|
||||||
TestClient to do a request.
|
TestClient to do a request.
|
||||||
"""
|
"""
|
||||||
|
if msg_prefix:
|
||||||
|
msg_prefix += ": "
|
||||||
|
|
||||||
if hasattr(response, 'redirect_chain'):
|
if hasattr(response, 'redirect_chain'):
|
||||||
# The request was a followed redirect
|
# The request was a followed redirect
|
||||||
self.failUnless(len(response.redirect_chain) > 0,
|
self.failUnless(len(response.redirect_chain) > 0,
|
||||||
("Response didn't redirect as expected: Response code was %d"
|
msg_prefix + "Response didn't redirect as expected: Response"
|
||||||
" (expected %d)" % (response.status_code, status_code)))
|
" code was %d (expected %d)" %
|
||||||
|
(response.status_code, status_code))
|
||||||
|
|
||||||
self.assertEqual(response.redirect_chain[0][1], status_code,
|
self.assertEqual(response.redirect_chain[0][1], status_code,
|
||||||
("Initial response didn't redirect as expected: Response code was %d"
|
msg_prefix + "Initial response didn't redirect as expected:"
|
||||||
" (expected %d)" % (response.redirect_chain[0][1], status_code)))
|
" Response code was %d (expected %d)" %
|
||||||
|
(response.redirect_chain[0][1], status_code))
|
||||||
|
|
||||||
url, status_code = response.redirect_chain[-1]
|
url, status_code = response.redirect_chain[-1]
|
||||||
|
|
||||||
self.assertEqual(response.status_code, target_status_code,
|
self.assertEqual(response.status_code, target_status_code,
|
||||||
("Response didn't redirect as expected: Final Response code was %d"
|
msg_prefix + "Response didn't redirect as expected: Final"
|
||||||
" (expected %d)" % (response.status_code, target_status_code)))
|
" Response code was %d (expected %d)" %
|
||||||
|
(response.status_code, target_status_code))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Not a followed redirect
|
# Not a followed redirect
|
||||||
self.assertEqual(response.status_code, status_code,
|
self.assertEqual(response.status_code, status_code,
|
||||||
("Response didn't redirect as expected: Response code was %d"
|
msg_prefix + "Response didn't redirect as expected: Response"
|
||||||
" (expected %d)" % (response.status_code, status_code)))
|
" code was %d (expected %d)" %
|
||||||
|
(response.status_code, status_code))
|
||||||
|
|
||||||
url = response['Location']
|
url = response['Location']
|
||||||
scheme, netloc, path, query, fragment = urlsplit(url)
|
scheme, netloc, path, query, fragment = urlsplit(url)
|
||||||
|
@ -324,9 +331,9 @@ class TransactionTestCase(unittest.TestCase):
|
||||||
# Get the redirection page, using the same client that was used
|
# Get the redirection page, using the same client that was used
|
||||||
# to obtain the original response.
|
# to obtain the original response.
|
||||||
self.assertEqual(redirect_response.status_code, target_status_code,
|
self.assertEqual(redirect_response.status_code, target_status_code,
|
||||||
("Couldn't retrieve redirection page '%s': response code was %d"
|
msg_prefix + "Couldn't retrieve redirection page '%s':"
|
||||||
" (expected %d)") %
|
" response code was %d (expected %d)" %
|
||||||
(path, redirect_response.status_code, target_status_code))
|
(path, redirect_response.status_code, target_status_code))
|
||||||
|
|
||||||
e_scheme, e_netloc, e_path, e_query, e_fragment = urlsplit(expected_url)
|
e_scheme, e_netloc, e_path, e_query, e_fragment = urlsplit(expected_url)
|
||||||
if not (e_scheme or e_netloc):
|
if not (e_scheme or e_netloc):
|
||||||
|
@ -334,10 +341,11 @@ class TransactionTestCase(unittest.TestCase):
|
||||||
e_query, e_fragment))
|
e_query, e_fragment))
|
||||||
|
|
||||||
self.assertEqual(url, expected_url,
|
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
|
Asserts that a response indicates that a page was retrieved
|
||||||
successfully, (i.e., the HTTP status code was as expected), and that
|
successfully, (i.e., the HTTP status code was as expected), and that
|
||||||
|
@ -345,42 +353,52 @@ class TransactionTestCase(unittest.TestCase):
|
||||||
If ``count`` is None, the count doesn't matter - the assertion is true
|
If ``count`` is None, the count doesn't matter - the assertion is true
|
||||||
if the text occurs at least once in the response.
|
if the text occurs at least once in the response.
|
||||||
"""
|
"""
|
||||||
|
if msg_prefix:
|
||||||
|
msg_prefix += ": "
|
||||||
|
|
||||||
self.assertEqual(response.status_code, status_code,
|
self.assertEqual(response.status_code, status_code,
|
||||||
"Couldn't retrieve page: Response code was %d (expected %d)'" %
|
msg_prefix + "Couldn't retrieve page: Response code was %d"
|
||||||
(response.status_code, status_code))
|
" (expected %d)" % (response.status_code, status_code))
|
||||||
text = smart_str(text, response._charset)
|
text = smart_str(text, response._charset)
|
||||||
real_count = response.content.count(text)
|
real_count = response.content.count(text)
|
||||||
if count is not None:
|
if count is not None:
|
||||||
self.assertEqual(real_count, count,
|
self.assertEqual(real_count, count,
|
||||||
"Found %d instances of '%s' in response (expected %d)" %
|
msg_prefix + "Found %d instances of '%s' in response"
|
||||||
(real_count, text, count))
|
" (expected %d)" % (real_count, text, count))
|
||||||
else:
|
else:
|
||||||
self.failUnless(real_count != 0,
|
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
|
Asserts that a response indicates that a page was retrieved
|
||||||
successfully, (i.e., the HTTP status code was as expected), and that
|
successfully, (i.e., the HTTP status code was as expected), and that
|
||||||
``text`` doesn't occurs in the content of the response.
|
``text`` doesn't occurs in the content of the response.
|
||||||
"""
|
"""
|
||||||
self.assertEqual(response.status_code, status_code,
|
if msg_prefix:
|
||||||
"Couldn't retrieve page: Response code was %d (expected %d)'" %
|
msg_prefix += ": "
|
||||||
(response.status_code, status_code))
|
|
||||||
text = smart_str(text, response._charset)
|
|
||||||
self.assertEqual(response.content.count(text),
|
|
||||||
0, "Response should not contain '%s'" % text)
|
|
||||||
|
|
||||||
def assertFormError(self, response, form, field, errors):
|
self.assertEqual(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,
|
||||||
|
msg_prefix + "Response should not contain '%s'" % text)
|
||||||
|
|
||||||
|
def assertFormError(self, response, form, field, errors, msg_prefix=''):
|
||||||
"""
|
"""
|
||||||
Asserts that a form used to render the response has a specific field
|
Asserts that a form used to render the response has a specific field
|
||||||
error.
|
error.
|
||||||
"""
|
"""
|
||||||
|
if msg_prefix:
|
||||||
|
msg_prefix += ": "
|
||||||
|
|
||||||
# Put context(s) into a list to simplify processing.
|
# Put context(s) into a list to simplify processing.
|
||||||
contexts = to_list(response.context)
|
contexts = to_list(response.context)
|
||||||
if not contexts:
|
if not contexts:
|
||||||
self.fail('Response did not use any contexts to render the'
|
self.fail(msg_prefix + "Response did not use any contexts to"
|
||||||
' response')
|
"render the response")
|
||||||
|
|
||||||
# Put error(s) into a list to simplify processing.
|
# Put error(s) into a list to simplify processing.
|
||||||
errors = to_list(errors)
|
errors = to_list(errors)
|
||||||
|
@ -396,50 +414,57 @@ class TransactionTestCase(unittest.TestCase):
|
||||||
if field in context[form].errors:
|
if field in context[form].errors:
|
||||||
field_errors = context[form].errors[field]
|
field_errors = context[form].errors[field]
|
||||||
self.failUnless(err in field_errors,
|
self.failUnless(err in field_errors,
|
||||||
"The field '%s' on form '%s' in"
|
msg_prefix + "The field '%s' on form '%s' in"
|
||||||
" context %d does not contain the"
|
" context %d does not contain the error '%s'"
|
||||||
" error '%s' (actual errors: %s)" %
|
" (actual errors: %s)" %
|
||||||
(field, form, i, err,
|
(field, form, i, err, repr(field_errors)))
|
||||||
repr(field_errors)))
|
|
||||||
elif field in context[form].fields:
|
elif field in context[form].fields:
|
||||||
self.fail("The field '%s' on form '%s' in context %d"
|
self.fail(msg_prefix + "The field '%s' on form '%s'"
|
||||||
" contains no errors" % (field, form, i))
|
" in context %d contains no errors" %
|
||||||
|
(field, form, i))
|
||||||
else:
|
else:
|
||||||
self.fail("The form '%s' in context %d does not"
|
self.fail(msg_prefix + "The form '%s' in context %d"
|
||||||
" contain the field '%s'" %
|
" does not contain the field '%s'" %
|
||||||
(form, i, field))
|
(form, i, field))
|
||||||
else:
|
else:
|
||||||
non_field_errors = context[form].non_field_errors()
|
non_field_errors = context[form].non_field_errors()
|
||||||
self.failUnless(err in non_field_errors,
|
self.failUnless(err in non_field_errors,
|
||||||
"The form '%s' in context %d does not contain the"
|
msg_prefix + "The form '%s' in context %d does not"
|
||||||
" non-field error '%s' (actual errors: %s)" %
|
" contain the non-field error '%s'"
|
||||||
|
" (actual errors: %s)" %
|
||||||
(form, i, err, non_field_errors))
|
(form, i, err, non_field_errors))
|
||||||
if not found_form:
|
if not found_form:
|
||||||
self.fail("The form '%s' was not used to render the response" %
|
self.fail(msg_prefix + "The form '%s' was not used to render the"
|
||||||
form)
|
" 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
|
Asserts that the template with the provided name was used in rendering
|
||||||
the response.
|
the response.
|
||||||
"""
|
"""
|
||||||
|
if msg_prefix:
|
||||||
|
msg_prefix += ": "
|
||||||
|
|
||||||
template_names = [t.name for t in to_list(response.template)]
|
template_names = [t.name for t in to_list(response.template)]
|
||||||
if not template_names:
|
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,
|
self.failUnless(template_name in template_names,
|
||||||
(u"Template '%s' was not a template used to render the response."
|
msg_prefix + "Template '%s' was not a template used to render"
|
||||||
u" Actual template(s) used: %s") % (template_name,
|
" the response. Actual template(s) used: %s" %
|
||||||
u', '.join(template_names)))
|
(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
|
Asserts that the template with the provided name was NOT used in
|
||||||
rendering the response.
|
rendering the response.
|
||||||
"""
|
"""
|
||||||
|
if msg_prefix:
|
||||||
|
msg_prefix += ": "
|
||||||
|
|
||||||
template_names = [t.name for t in to_list(response.template)]
|
template_names = [t.name for t in to_list(response.template)]
|
||||||
self.failIf(template_name in template_names,
|
self.failIf(template_name in template_names,
|
||||||
(u"Template '%s' was used unexpectedly in rendering the"
|
msg_prefix + "Template '%s' was used unexpectedly in rendering"
|
||||||
u" response") % template_name)
|
" the response" % template_name)
|
||||||
|
|
||||||
def connections_support_transactions():
|
def connections_support_transactions():
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -1104,23 +1104,32 @@ Assertions
|
||||||
|
|
||||||
.. versionadded:: 1.0
|
.. versionadded:: 1.0
|
||||||
|
|
||||||
|
.. versionchanged:: 1.2
|
||||||
|
Addded ``msg_prefix`` argument.
|
||||||
|
|
||||||
As Python's normal ``unittest.TestCase`` class implements assertion methods
|
As Python's normal ``unittest.TestCase`` class implements assertion methods
|
||||||
such as ``assertTrue`` and ``assertEquals``, Django's custom ``TestCase`` class
|
such as ``assertTrue`` and ``assertEquals``, Django's custom ``TestCase`` class
|
||||||
provides a number of custom assertion methods that are useful for testing Web
|
provides a number of custom assertion methods that are useful for testing Web
|
||||||
applications:
|
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
|
Asserts that a ``Response`` instance produced the given ``status_code`` and
|
||||||
that ``text`` appears in the content of the response. If ``count`` is
|
that ``text`` appears in the content of the response. If ``count`` is
|
||||||
provided, ``text`` must occur exactly ``count`` times in the response.
|
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
|
Asserts that a ``Response`` instance produced the given ``status_code`` and
|
||||||
that ``text`` does not appears in the content of the response.
|
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
|
Asserts that a field on a form raises the provided list of errors when
|
||||||
rendered on the form.
|
rendered on the form.
|
||||||
|
@ -1135,19 +1144,19 @@ applications:
|
||||||
``errors`` is an error string, or a list of error strings, that are
|
``errors`` is an error string, or a list of error strings, that are
|
||||||
expected as a result of form validation.
|
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
|
Asserts that the template with the given name was used in rendering the
|
||||||
response.
|
response.
|
||||||
|
|
||||||
The name is a string such as ``'admin/index.html'``.
|
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
|
Asserts that the template with the given name was *not* used in rendering
|
||||||
the response.
|
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
|
Asserts that the response return a ``status_code`` redirect status, it
|
||||||
redirected to ``expected_url`` (including any GET data), and the final
|
redirected to ``expected_url`` (including any GET data), and the final
|
||||||
|
|
|
@ -31,40 +31,86 @@ class AssertContainsTests(TestCase):
|
||||||
self.assertContains(response, 'twice')
|
self.assertContains(response, 'twice')
|
||||||
self.assertContains(response, 'twice', 2)
|
self.assertContains(response, 'twice', 2)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.assertContains(response, 'text', status_code=999)
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEquals(str(e), "Couldn't retrieve page: Response code was 200 (expected 999)")
|
||||||
|
try:
|
||||||
|
self.assertContains(response, 'text', status_code=999, msg_prefix='abc')
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEquals(str(e), "abc: Couldn't retrieve page: Response code was 200 (expected 999)")
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.assertNotContains(response, 'text', status_code=999)
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEquals(str(e), "Couldn't retrieve page: Response code was 200 (expected 999)")
|
||||||
|
try:
|
||||||
|
self.assertNotContains(response, 'text', status_code=999, msg_prefix='abc')
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEquals(str(e), "abc: Couldn't retrieve page: Response code was 200 (expected 999)")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.assertNotContains(response, 'once')
|
self.assertNotContains(response, 'once')
|
||||||
except AssertionError, e:
|
except AssertionError, e:
|
||||||
self.assertEquals(str(e), "Response should not contain 'once'")
|
self.assertEquals(str(e), "Response should not contain 'once'")
|
||||||
|
try:
|
||||||
|
self.assertNotContains(response, 'once', msg_prefix='abc')
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEquals(str(e), "abc: Response should not contain 'once'")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.assertContains(response, 'never', 1)
|
self.assertContains(response, 'never', 1)
|
||||||
except AssertionError, e:
|
except AssertionError, e:
|
||||||
self.assertEquals(str(e), "Found 0 instances of 'never' in response (expected 1)")
|
self.assertEquals(str(e), "Found 0 instances of 'never' in response (expected 1)")
|
||||||
|
try:
|
||||||
|
self.assertContains(response, 'never', 1, msg_prefix='abc')
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEquals(str(e), "abc: Found 0 instances of 'never' in response (expected 1)")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.assertContains(response, 'once', 0)
|
self.assertContains(response, 'once', 0)
|
||||||
except AssertionError, e:
|
except AssertionError, e:
|
||||||
self.assertEquals(str(e), "Found 1 instances of 'once' in response (expected 0)")
|
self.assertEquals(str(e), "Found 1 instances of 'once' in response (expected 0)")
|
||||||
|
try:
|
||||||
|
self.assertContains(response, 'once', 0, msg_prefix='abc')
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEquals(str(e), "abc: Found 1 instances of 'once' in response (expected 0)")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.assertContains(response, 'once', 2)
|
self.assertContains(response, 'once', 2)
|
||||||
except AssertionError, e:
|
except AssertionError, e:
|
||||||
self.assertEquals(str(e), "Found 1 instances of 'once' in response (expected 2)")
|
self.assertEquals(str(e), "Found 1 instances of 'once' in response (expected 2)")
|
||||||
|
try:
|
||||||
|
self.assertContains(response, 'once', 2, msg_prefix='abc')
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEquals(str(e), "abc: Found 1 instances of 'once' in response (expected 2)")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.assertContains(response, 'twice', 1)
|
self.assertContains(response, 'twice', 1)
|
||||||
except AssertionError, e:
|
except AssertionError, e:
|
||||||
self.assertEquals(str(e), "Found 2 instances of 'twice' in response (expected 1)")
|
self.assertEquals(str(e), "Found 2 instances of 'twice' in response (expected 1)")
|
||||||
|
try:
|
||||||
|
self.assertContains(response, 'twice', 1, msg_prefix='abc')
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEquals(str(e), "abc: Found 2 instances of 'twice' in response (expected 1)")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.assertContains(response, 'thrice')
|
self.assertContains(response, 'thrice')
|
||||||
except AssertionError, e:
|
except AssertionError, e:
|
||||||
self.assertEquals(str(e), "Couldn't find 'thrice' in response")
|
self.assertEquals(str(e), "Couldn't find 'thrice' in response")
|
||||||
|
try:
|
||||||
|
self.assertContains(response, 'thrice', msg_prefix='abc')
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEquals(str(e), "abc: Couldn't find 'thrice' in response")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.assertContains(response, 'thrice', 3)
|
self.assertContains(response, 'thrice', 3)
|
||||||
except AssertionError, e:
|
except AssertionError, e:
|
||||||
self.assertEquals(str(e), "Found 0 instances of 'thrice' in response (expected 3)")
|
self.assertEquals(str(e), "Found 0 instances of 'thrice' in response (expected 3)")
|
||||||
|
try:
|
||||||
|
self.assertContains(response, 'thrice', 3, msg_prefix='abc')
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEquals(str(e), "abc: Found 0 instances of 'thrice' in response (expected 3)")
|
||||||
|
|
||||||
def test_unicode_contains(self):
|
def test_unicode_contains(self):
|
||||||
"Unicode characters can be found in template context"
|
"Unicode characters can be found in template context"
|
||||||
|
@ -80,6 +126,7 @@ class AssertContainsTests(TestCase):
|
||||||
self.assertNotContains(r, u'はたけ')
|
self.assertNotContains(r, u'はたけ')
|
||||||
self.assertNotContains(r, '\xe3\x81\xaf\xe3\x81\x9f\xe3\x81\x91'.decode('utf-8'))
|
self.assertNotContains(r, '\xe3\x81\xaf\xe3\x81\x9f\xe3\x81\x91'.decode('utf-8'))
|
||||||
|
|
||||||
|
|
||||||
class AssertTemplateUsedTests(TestCase):
|
class AssertTemplateUsedTests(TestCase):
|
||||||
fixtures = ['testdata.json']
|
fixtures = ['testdata.json']
|
||||||
|
|
||||||
|
@ -95,21 +142,35 @@ class AssertTemplateUsedTests(TestCase):
|
||||||
except AssertionError, e:
|
except AssertionError, e:
|
||||||
self.assertEquals(str(e), "No templates used to render the response")
|
self.assertEquals(str(e), "No templates used to render the response")
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.assertTemplateUsed(response, 'GET Template', msg_prefix='abc')
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEquals(str(e), "abc: No templates used to render the response")
|
||||||
|
|
||||||
def test_single_context(self):
|
def test_single_context(self):
|
||||||
"Template assertions work when there is a single context"
|
"Template assertions work when there is a single context"
|
||||||
response = self.client.get('/test_client/post_view/', {})
|
response = self.client.get('/test_client/post_view/', {})
|
||||||
|
|
||||||
#
|
|
||||||
try:
|
try:
|
||||||
self.assertTemplateNotUsed(response, 'Empty GET Template')
|
self.assertTemplateNotUsed(response, 'Empty GET Template')
|
||||||
except AssertionError, e:
|
except AssertionError, e:
|
||||||
self.assertEquals(str(e), "Template 'Empty GET Template' was used unexpectedly in rendering the response")
|
self.assertEquals(str(e), "Template 'Empty GET Template' was used unexpectedly in rendering the response")
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.assertTemplateNotUsed(response, 'Empty GET Template', msg_prefix='abc')
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEquals(str(e), "abc: Template 'Empty GET Template' was used unexpectedly in rendering the response")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.assertTemplateUsed(response, 'Empty POST Template')
|
self.assertTemplateUsed(response, 'Empty POST Template')
|
||||||
except AssertionError, e:
|
except AssertionError, e:
|
||||||
self.assertEquals(str(e), "Template 'Empty POST Template' was not a template used to render the response. Actual template(s) used: Empty GET Template")
|
self.assertEquals(str(e), "Template 'Empty POST Template' was not a template used to render the response. Actual template(s) used: Empty GET Template")
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.assertTemplateUsed(response, 'Empty POST Template', msg_prefix='abc')
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEquals(str(e), "abc: Template 'Empty POST Template' was not a template used to render the response. Actual template(s) used: Empty GET Template")
|
||||||
|
|
||||||
def test_multiple_context(self):
|
def test_multiple_context(self):
|
||||||
"Template assertions work when there are multiple contexts"
|
"Template assertions work when there are multiple contexts"
|
||||||
post_data = {
|
post_data = {
|
||||||
|
@ -146,6 +207,11 @@ class AssertRedirectsTests(TestCase):
|
||||||
except AssertionError, e:
|
except AssertionError, e:
|
||||||
self.assertEquals(str(e), "Response didn't redirect as expected: Response code was 301 (expected 302)")
|
self.assertEquals(str(e), "Response didn't redirect as expected: Response code was 301 (expected 302)")
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.assertRedirects(response, '/test_client/get_view/', msg_prefix='abc')
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEquals(str(e), "abc: Response didn't redirect as expected: Response code was 301 (expected 302)")
|
||||||
|
|
||||||
def test_lost_query(self):
|
def test_lost_query(self):
|
||||||
"An assertion is raised if the redirect location doesn't preserve GET parameters"
|
"An assertion is raised if the redirect location doesn't preserve GET parameters"
|
||||||
response = self.client.get('/test_client/redirect_view/', {'var': 'value'})
|
response = self.client.get('/test_client/redirect_view/', {'var': 'value'})
|
||||||
|
@ -154,6 +220,11 @@ class AssertRedirectsTests(TestCase):
|
||||||
except AssertionError, e:
|
except AssertionError, e:
|
||||||
self.assertEquals(str(e), "Response redirected to 'http://testserver/test_client/get_view/?var=value', expected 'http://testserver/test_client/get_view/'")
|
self.assertEquals(str(e), "Response redirected to 'http://testserver/test_client/get_view/?var=value', expected 'http://testserver/test_client/get_view/'")
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.assertRedirects(response, '/test_client/get_view/', msg_prefix='abc')
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEquals(str(e), "abc: Response redirected to 'http://testserver/test_client/get_view/?var=value', expected 'http://testserver/test_client/get_view/'")
|
||||||
|
|
||||||
def test_incorrect_target(self):
|
def test_incorrect_target(self):
|
||||||
"An assertion is raised if the response redirects to another target"
|
"An assertion is raised if the response redirects to another target"
|
||||||
response = self.client.get('/test_client/permanent_redirect_view/')
|
response = self.client.get('/test_client/permanent_redirect_view/')
|
||||||
|
@ -172,6 +243,12 @@ class AssertRedirectsTests(TestCase):
|
||||||
except AssertionError, e:
|
except AssertionError, e:
|
||||||
self.assertEquals(str(e), "Couldn't retrieve redirection page '/test_client/permanent_redirect_view/': response code was 301 (expected 200)")
|
self.assertEquals(str(e), "Couldn't retrieve redirection page '/test_client/permanent_redirect_view/': response code was 301 (expected 200)")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# The redirect target responds with a 301 code, not 200
|
||||||
|
self.assertRedirects(response, 'http://testserver/test_client/permanent_redirect_view/', msg_prefix='abc')
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEquals(str(e), "abc: Couldn't retrieve redirection page '/test_client/permanent_redirect_view/': response code was 301 (expected 200)")
|
||||||
|
|
||||||
def test_redirect_chain(self):
|
def test_redirect_chain(self):
|
||||||
"You can follow a redirect chain of multiple redirects"
|
"You can follow a redirect chain of multiple redirects"
|
||||||
response = self.client.get('/test_client_regress/redirects/further/more/', {}, follow=True)
|
response = self.client.get('/test_client_regress/redirects/further/more/', {}, follow=True)
|
||||||
|
@ -263,6 +340,11 @@ class AssertRedirectsTests(TestCase):
|
||||||
except AssertionError, e:
|
except AssertionError, e:
|
||||||
self.assertEquals(str(e), "Response didn't redirect as expected: Response code was 200 (expected 302)")
|
self.assertEquals(str(e), "Response didn't redirect as expected: Response code was 200 (expected 302)")
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.assertRedirects(response, '/test_client/get_view/', msg_prefix='abc')
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEquals(str(e), "abc: Response didn't redirect as expected: Response code was 200 (expected 302)")
|
||||||
|
|
||||||
def test_redirect_on_non_redirect_page(self):
|
def test_redirect_on_non_redirect_page(self):
|
||||||
"An assertion is raised if the original page couldn't be retrieved as expected"
|
"An assertion is raised if the original page couldn't be retrieved as expected"
|
||||||
# This page will redirect with code 301, not 302
|
# This page will redirect with code 301, not 302
|
||||||
|
@ -272,6 +354,11 @@ class AssertRedirectsTests(TestCase):
|
||||||
except AssertionError, e:
|
except AssertionError, e:
|
||||||
self.assertEquals(str(e), "Response didn't redirect as expected: Response code was 200 (expected 302)")
|
self.assertEquals(str(e), "Response didn't redirect as expected: Response code was 200 (expected 302)")
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.assertRedirects(response, '/test_client/get_view/', msg_prefix='abc')
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEquals(str(e), "abc: Response didn't redirect as expected: Response code was 200 (expected 302)")
|
||||||
|
|
||||||
|
|
||||||
class AssertFormErrorTests(TestCase):
|
class AssertFormErrorTests(TestCase):
|
||||||
def test_unknown_form(self):
|
def test_unknown_form(self):
|
||||||
|
@ -291,6 +378,10 @@ class AssertFormErrorTests(TestCase):
|
||||||
self.assertFormError(response, 'wrong_form', 'some_field', 'Some error.')
|
self.assertFormError(response, 'wrong_form', 'some_field', 'Some error.')
|
||||||
except AssertionError, e:
|
except AssertionError, e:
|
||||||
self.assertEqual(str(e), "The form 'wrong_form' was not used to render the response")
|
self.assertEqual(str(e), "The form 'wrong_form' was not used to render the response")
|
||||||
|
try:
|
||||||
|
self.assertFormError(response, 'wrong_form', 'some_field', 'Some error.', msg_prefix='abc')
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEqual(str(e), "abc: The form 'wrong_form' was not used to render the response")
|
||||||
|
|
||||||
def test_unknown_field(self):
|
def test_unknown_field(self):
|
||||||
"An assertion is raised if the field name is unknown"
|
"An assertion is raised if the field name is unknown"
|
||||||
|
@ -309,6 +400,10 @@ class AssertFormErrorTests(TestCase):
|
||||||
self.assertFormError(response, 'form', 'some_field', 'Some error.')
|
self.assertFormError(response, 'form', 'some_field', 'Some error.')
|
||||||
except AssertionError, e:
|
except AssertionError, e:
|
||||||
self.assertEqual(str(e), "The form 'form' in context 0 does not contain the field 'some_field'")
|
self.assertEqual(str(e), "The form 'form' in context 0 does not contain the field 'some_field'")
|
||||||
|
try:
|
||||||
|
self.assertFormError(response, 'form', 'some_field', 'Some error.', msg_prefix='abc')
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEqual(str(e), "abc: The form 'form' in context 0 does not contain the field 'some_field'")
|
||||||
|
|
||||||
def test_noerror_field(self):
|
def test_noerror_field(self):
|
||||||
"An assertion is raised if the field doesn't have any errors"
|
"An assertion is raised if the field doesn't have any errors"
|
||||||
|
@ -327,6 +422,10 @@ class AssertFormErrorTests(TestCase):
|
||||||
self.assertFormError(response, 'form', 'value', 'Some error.')
|
self.assertFormError(response, 'form', 'value', 'Some error.')
|
||||||
except AssertionError, e:
|
except AssertionError, e:
|
||||||
self.assertEqual(str(e), "The field 'value' on form 'form' in context 0 contains no errors")
|
self.assertEqual(str(e), "The field 'value' on form 'form' in context 0 contains no errors")
|
||||||
|
try:
|
||||||
|
self.assertFormError(response, 'form', 'value', 'Some error.', msg_prefix='abc')
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEqual(str(e), "abc: The field 'value' on form 'form' in context 0 contains no errors")
|
||||||
|
|
||||||
def test_unknown_error(self):
|
def test_unknown_error(self):
|
||||||
"An assertion is raised if the field doesn't contain the provided error"
|
"An assertion is raised if the field doesn't contain the provided error"
|
||||||
|
@ -345,6 +444,10 @@ class AssertFormErrorTests(TestCase):
|
||||||
self.assertFormError(response, 'form', 'email', 'Some error.')
|
self.assertFormError(response, 'form', 'email', 'Some error.')
|
||||||
except AssertionError, e:
|
except AssertionError, e:
|
||||||
self.assertEqual(str(e), "The field 'email' on form 'form' in context 0 does not contain the error 'Some error.' (actual errors: [u'Enter a valid e-mail address.'])")
|
self.assertEqual(str(e), "The field 'email' on form 'form' in context 0 does not contain the error 'Some error.' (actual errors: [u'Enter a valid e-mail address.'])")
|
||||||
|
try:
|
||||||
|
self.assertFormError(response, 'form', 'email', 'Some error.', msg_prefix='abc')
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEqual(str(e), "abc: The field 'email' on form 'form' in context 0 does not contain the error 'Some error.' (actual errors: [u'Enter a valid e-mail address.'])")
|
||||||
|
|
||||||
def test_unknown_nonfield_error(self):
|
def test_unknown_nonfield_error(self):
|
||||||
"""
|
"""
|
||||||
|
@ -366,6 +469,10 @@ class AssertFormErrorTests(TestCase):
|
||||||
self.assertFormError(response, 'form', None, 'Some error.')
|
self.assertFormError(response, 'form', None, 'Some error.')
|
||||||
except AssertionError, e:
|
except AssertionError, e:
|
||||||
self.assertEqual(str(e), "The form 'form' in context 0 does not contain the non-field error 'Some error.' (actual errors: )")
|
self.assertEqual(str(e), "The form 'form' in context 0 does not contain the non-field error 'Some error.' (actual errors: )")
|
||||||
|
try:
|
||||||
|
self.assertFormError(response, 'form', None, 'Some error.', msg_prefix='abc')
|
||||||
|
except AssertionError, e:
|
||||||
|
self.assertEqual(str(e), "abc: The form 'form' in context 0 does not contain the non-field error 'Some error.' (actual errors: )")
|
||||||
|
|
||||||
class LoginTests(TestCase):
|
class LoginTests(TestCase):
|
||||||
fixtures = ['testdata']
|
fixtures = ['testdata']
|
||||||
|
|
Loading…
Reference in New Issue