Skip to content

Commit 5639422

Browse files
committed
Fixed #4901 -- Modified assertContains to provide a default check of 'any instances of text in content'. Thanks for the suggestion, nis@superlativ.dk.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5731 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent bdc5a3e commit 5639422

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

django/test/testcases.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,23 @@ def assertRedirects(self, response, expected_path, status_code=302, target_statu
7373
"Couldn't retrieve redirection page '%s': response code was %d (expected %d)" %
7474
(path, redirect_response.status_code, target_status_code))
7575

76-
def assertContains(self, response, text, count=1, status_code=200):
76+
def assertContains(self, response, text, count=None, status_code=200):
7777
"""Assert that a response indicates that a page was retreived successfully,
7878
(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.
8081
8182
"""
8283
self.assertEqual(response.status_code, status_code,
8384
"Couldn't retrieve page: Response code was %d (expected %d)'" %
8485
(response.status_code, status_code))
8586
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+
8993
def assertFormError(self, response, form, field, errors):
9094
"Assert that a form used to render the response has a specific field error"
9195
if not response.context:

docs/testing.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,10 +481,11 @@ Normal Python unit tests have a wide range of assertions, such as
481481
``django.TestCase`` adds to these, providing some assertions
482482
that can be useful in testing the behavior of web sites.
483483

484-
``assertContains(response, text, count=1, status_code=200)``
484+
``assertContains(response, text, count=None, status_code=200)``
485485
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.
488489

489490
``assertFormError(response, form, field, errors)``
490491
Assert that a field on a form raised the provided list of errors when

tests/regressiontests/test_client_regress/models.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,36 @@
66
from django.core import mail
77
import os
88

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+
939
class AssertTemplateUsedTests(TestCase):
1040
fixtures = ['testdata.json']
1141

tests/regressiontests/test_client_regress/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
def no_template_view(request):
66
"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.")
88

99
def file_upload_view(request):
1010
"""

0 commit comments

Comments
 (0)