Changeset 5731
- Timestamp:
- 07/20/07 09:32:20 (1 year ago)
- Files:
-
- django/trunk/django/test/testcases.py (modified) (2 diffs)
- django/trunk/docs/testing.txt (modified) (1 diff)
- django/trunk/tests/regressiontests/test_client_regress/models.py (modified) (1 diff)
- django/trunk/tests/regressiontests/test_client_regress/views.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/test/testcases.py
r5620 r5731 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 78 (i.e., the HTTP status code was as expected), and that ``text`` occurs ``count`` 79 times in the content of the response. 79 times in the content of the response. If ``count`` is None, the count doesn't 80 matter - the assertion is true if the text occurs at least once in the response. 80 81 81 82 """ … … 84 85 (response.status_code, status_code)) 85 86 real_count = response.content.count(text) 86 self.assertEqual(real_count, count, 87 "Found %d instances of '%s' in response (expected %d)" % (real_count, text, count)) 88 87 if count: 88 self.assertEqual(real_count, count, 89 "Found %d instances of '%s' in response (expected %d)" % (real_count, text, count)) 90 else: 91 self.assertTrue(real_count != 0, "Couldn't find '%s' in response" % text) 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" django/trunk/docs/testing.txt
r5730 r5731 482 482 that can be useful in testing the behavior of web sites. 483 483 484 ``assertContains(response, text, count= 1, status_code=200)``484 ``assertContains(response, text, count=None, status_code=200)`` 485 485 Assert that a response indicates that a page could be retrieved and 486 produced the nominated status code, and that ``text`` occurs ``count`` 487 times in the content of the response. 486 produced the nominated status code, and that ``text`` in the content 487 of the response. If ``count`` is provided, ``text`` must occur exactly 488 ``count`` times in the response. 488 489 489 490 ``assertFormError(response, form, field, errors)`` django/trunk/tests/regressiontests/test_client_regress/models.py
r5621 r5731 7 7 import os 8 8 9 class AssertContainsTests(TestCase): 10 def test_contains(self): 11 "Reponses can be inspected for content, including counting repeated substrings" 12 response = self.client.get('/test_client_regress/no_template_view/') 13 14 self.assertContains(response, 'once') 15 self.assertContains(response, 'once', 1) 16 self.assertContains(response, 'twice') 17 self.assertContains(response, 'twice', 2) 18 19 try: 20 self.assertContains(response, 'once', 2) 21 except AssertionError, e: 22 self.assertEquals(str(e), "Found 1 instances of 'once' in response (expected 2)") 23 24 try: 25 self.assertContains(response, 'twice', 1) 26 except AssertionError, e: 27 self.assertEquals(str(e), "Found 2 instances of 'twice' in response (expected 1)") 28 29 try: 30 self.assertContains(response, 'thrice') 31 except AssertionError, e: 32 self.assertEquals(str(e), "Couldn't find 'thrice' in response") 33 34 try: 35 self.assertContains(response, 'thrice', 3) 36 except AssertionError, e: 37 self.assertEquals(str(e), "Found 0 instances of 'thrice' in response (expected 3)") 38 9 39 class AssertTemplateUsedTests(TestCase): 10 40 fixtures = ['testdata.json'] django/trunk/tests/regressiontests/test_client_regress/views.py
r5609 r5731 5 5 def no_template_view(request): 6 6 "A simple view that expects a GET request, and returns a rendered template" 7 return HttpResponse("No template used ")7 return HttpResponse("No template used. Sample content: twice once twice. Content ends.") 8 8 9 9 def file_upload_view(request):
