Ticket #4901: django_ticket4901.patch
File django_ticket4901.patch, 2.7 KB (added by , 17 years ago) |
---|
-
django/test/testcases.py
73 73 "Couldn't retrieve redirection page '%s': response code was %d (expected %d)" % 74 74 (path, redirect_response.status_code, target_status_code)) 75 75 76 def assertContains(self, response, text, count= 1, status_code=200):76 def assertContains(self, response, text, count=None, status_code=200): 77 77 """Assert that a response indicates that a page was retreived successfully, 78 (i.e., the HTTP status code was as expected), and that ``text`` occurs ``count``79 times in the content of the response.80 78 (i.e., the HTTP status code was as expected), and that ``text`` occurs in the 79 content of the response. If ``count`` is given, ``text`` must occur exactly ``count`` 80 times. 81 81 """ 82 82 self.assertEqual(response.status_code, status_code, 83 83 "Couldn't retrieve page: Response code was %d (expected %d)'" % 84 84 (response.status_code, status_code)) 85 85 real_count = response.content.count(text) 86 self.assertEqual(real_count, count, 86 if count is None: 87 self.assertNotEqual(real_count, 0, 88 "Found no instances of '%s' in response (expected at least one)" % (text)) 89 else: 90 self.assertEqual(real_count, count, 87 91 "Found %d instances of '%s' in response (expected %d)" % (real_count, text, count)) 88 92 89 93 def assertFormError(self, response, form, field, errors): 90 94 "Assert that a form used to render the response has a specific field error" 91 95 if not response.context: -
docs/testing.txt
472 472 ``django.TestCase`` adds to these, providing some assertions 473 473 that can be useful in testing the behavior of web sites. 474 474 475 ``assertContains(response, text, count= 1, status_code=200)``475 ``assertContains(response, text, count=None, status_code=200)`` 476 476 Assert that a response indicates that a page could be retrieved and 477 produced the nominated status code, and that ``text`` occurs ``count``478 times in the content of the response.477 produced the nominated status code, and that ``text`` in the content of the 478 response. If ``count`` is given, ``text`` must occur exactly ``count`` times. 479 479 480 480 ``assertFormError(response, form, field, errors)`` 481 481 Assert that a field on a form raised the provided list of errors when