Fixed #7165 -- Added an assertNotContains() method to the test client. Thanks for the suggestion and implementation, J. Pablo Fernandez <pupeno@pupeno.com>.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7578 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2008-06-06 13:50:02 +00:00
parent 46cd8bb5b6
commit 08401959d8
4 changed files with 29 additions and 6 deletions

View File

@ -142,6 +142,7 @@ answer newbie questions, and generally made Django that much better:
Bill Fenner <fenner@gmail.com> Bill Fenner <fenner@gmail.com>
Stefane Fermgier <sf@fermigier.com> Stefane Fermgier <sf@fermigier.com>
Afonso Fernández Nogueira <fonzzo.django@gmail.com> Afonso Fernández Nogueira <fonzzo.django@gmail.com>
J. Pablo Fernandez <pupeno@pupeno.com>
Matthew Flanagan <http://wadofstuff.blogspot.com> Matthew Flanagan <http://wadofstuff.blogspot.com>
Eric Floehr <eric@intellovations.com> Eric Floehr <eric@intellovations.com>
Vincent Foley <vfoleybourgon@yahoo.ca> Vincent Foley <vfoleybourgon@yahoo.ca>

View File

@ -128,6 +128,18 @@ class TestCase(unittest.TestCase):
self.failUnless(real_count != 0, self.failUnless(real_count != 0,
"Couldn't find '%s' in response" % text) "Couldn't find '%s' in response" % text)
def assertNotContains(self, response, text, status_code=200):
"""
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.
"""
self.assertEqual(response.status_code, status_code,
"Couldn't retrieve page: Response code was %d (expected %d)'" %
(response.status_code, status_code))
self.assertEqual(response.content.count(text), 0,
"Response should not contain '%s'" % text)
def assertFormError(self, response, form, field, errors): def assertFormError(self, response, form, field, errors):
""" """
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

View File

@ -822,6 +822,10 @@ useful for testing Web applications:
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.
``assertNotContains(response, text, status_code=200)``
Asserts that a ``Response`` instance produced the given ``status_code`` and
that ``text`` does not appears in the content of the response.
``assertFormError(response, form, field, errors)`` ``assertFormError(response, form, field, errors)``
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.
@ -837,6 +841,12 @@ useful for testing Web 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.
``assertTemplateUsed(response, template_name)``
Asserts that the template with the given name was used in rendering the
response.
The name is a string such as ``'admin/index.html'``.
``assertTemplateNotUsed(response, template_name)`` ``assertTemplateNotUsed(response, template_name)``
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.
@ -846,12 +856,6 @@ useful for testing Web applications:
it redirected to ``expected_url`` (including any GET data), and the subsequent it redirected to ``expected_url`` (including any GET data), and the subsequent
page was received with ``target_status_code``. page was received with ``target_status_code``.
``assertTemplateUsed(response, template_name)``
Asserts that the template with the given name was used in rendering the
response.
The name is a string such as ``'admin/index.html'``.
E-mail services E-mail services
--------------- ---------------

View File

@ -11,12 +11,18 @@ class AssertContainsTests(TestCase):
"Responses can be inspected for content, including counting repeated substrings" "Responses can be inspected for content, including counting repeated substrings"
response = self.client.get('/test_client_regress/no_template_view/') response = self.client.get('/test_client_regress/no_template_view/')
self.assertNotContains(response, 'never')
self.assertContains(response, 'never', 0) self.assertContains(response, 'never', 0)
self.assertContains(response, 'once') self.assertContains(response, 'once')
self.assertContains(response, 'once', 1) self.assertContains(response, 'once', 1)
self.assertContains(response, 'twice') self.assertContains(response, 'twice')
self.assertContains(response, 'twice', 2) self.assertContains(response, 'twice', 2)
try:
self.assertNotContains(response, 'once')
except AssertionError, e:
self.assertEquals(str(e), "Response should not contain 'once'")
try: try:
self.assertContains(response, 'never', 1) self.assertContains(response, 'never', 1)
except AssertionError, e: except AssertionError, e: