Author: russellm
Date: 2007-07-20 09:32:20 -0500 (Fri, 20 Jul 2007)
New Revision: 5731
Modified:
django/trunk/django/test/testcases.py
django/trunk/docs/testing.txt
django/trunk/tests/regressiontests/test_client_regress/models.py
django/trunk/tests/regressiontests/test_client_regress/views.py
Log:
Fixed #4901 -- Modified assertContains to provide a default check of 'any
instances of text in content'. Thanks for the suggestion, [EMAIL PROTECTED]
Modified: django/trunk/django/test/testcases.py
===================================================================
--- django/trunk/django/test/testcases.py 2007-07-20 14:07:54 UTC (rev
5730)
+++ django/trunk/django/test/testcases.py 2007-07-20 14:32:20 UTC (rev
5731)
@@ -73,19 +73,23 @@
"Couldn't retrieve redirection page '%s': response code was %d
(expected %d)" %
(path, redirect_response.status_code, target_status_code))
- def assertContains(self, response, text, count=1, status_code=200):
+ def assertContains(self, response, text, count=None, status_code=200):
"""Assert that a response indicates that a page was retreived
successfully,
(i.e., the HTTP status code was as expected), and that ``text`` occurs
``count``
- times in the content of the response.
+ times in the content of the response. If ``count`` is None, the count
doesn't
+ matter - the assertion is true if the text occurs at least once in the
response.
"""
self.assertEqual(response.status_code, status_code,
"Couldn't retrieve page: Response code was %d (expected %d)'" %
(response.status_code, status_code))
real_count = response.content.count(text)
- self.assertEqual(real_count, count,
- "Found %d instances of '%s' in response (expected %d)" %
(real_count, text, count))
-
+ if count:
+ self.assertEqual(real_count, count,
+ "Found %d instances of '%s' in response (expected %d)" %
(real_count, text, count))
+ else:
+ self.assertTrue(real_count != 0, "Couldn't find '%s' in response"
% text)
+
def assertFormError(self, response, form, field, errors):
"Assert that a form used to render the response has a specific field
error"
if not response.context:
Modified: django/trunk/docs/testing.txt
===================================================================
--- django/trunk/docs/testing.txt 2007-07-20 14:07:54 UTC (rev 5730)
+++ django/trunk/docs/testing.txt 2007-07-20 14:32:20 UTC (rev 5731)
@@ -481,10 +481,11 @@
``django.TestCase`` adds to these, providing some assertions
that can be useful in testing the behavior of web sites.
-``assertContains(response, text, count=1, status_code=200)``
+``assertContains(response, text, count=None, status_code=200)``
Assert that a response indicates that a page could be retrieved and
- produced the nominated status code, and that ``text`` occurs ``count``
- times in the content of the response.
+ produced the nominated status code, and that ``text`` in the content
+ of the response. If ``count`` is provided, ``text`` must occur exactly
+ ``count`` times in the response.
``assertFormError(response, form, field, errors)``
Assert that a field on a form raised the provided list of errors when
Modified: django/trunk/tests/regressiontests/test_client_regress/models.py
===================================================================
--- django/trunk/tests/regressiontests/test_client_regress/models.py
2007-07-20 14:07:54 UTC (rev 5730)
+++ django/trunk/tests/regressiontests/test_client_regress/models.py
2007-07-20 14:32:20 UTC (rev 5731)
@@ -6,6 +6,36 @@
from django.core import mail
import os
+class AssertContainsTests(TestCase):
+ def test_contains(self):
+ "Reponses can be inspected for content, including counting repeated
substrings"
+ response = self.client.get('/test_client_regress/no_template_view/')
+
+ self.assertContains(response, 'once')
+ self.assertContains(response, 'once', 1)
+ self.assertContains(response, 'twice')
+ self.assertContains(response, 'twice', 2)
+
+ try:
+ self.assertContains(response, 'once', 2)
+ except AssertionError, e:
+ self.assertEquals(str(e), "Found 1 instances of 'once' in response
(expected 2)")
+
+ try:
+ self.assertContains(response, 'twice', 1)
+ except AssertionError, e:
+ self.assertEquals(str(e), "Found 2 instances of 'twice' in
response (expected 1)")
+
+ try:
+ self.assertContains(response, 'thrice')
+ except AssertionError, e:
+ self.assertEquals(str(e), "Couldn't find 'thrice' in response")
+
+ try:
+ self.assertContains(response, 'thrice', 3)
+ except AssertionError, e:
+ self.assertEquals(str(e), "Found 0 instances of 'thrice' in
response (expected 3)")
+
class AssertTemplateUsedTests(TestCase):
fixtures = ['testdata.json']
Modified: django/trunk/tests/regressiontests/test_client_regress/views.py
===================================================================
--- django/trunk/tests/regressiontests/test_client_regress/views.py
2007-07-20 14:07:54 UTC (rev 5730)
+++ django/trunk/tests/regressiontests/test_client_regress/views.py
2007-07-20 14:32:20 UTC (rev 5731)
@@ -4,7 +4,7 @@
def no_template_view(request):
"A simple view that expects a GET request, and returns a rendered template"
- return HttpResponse("No template used")
+ return HttpResponse("No template used. Sample content: twice once twice.
Content ends.")
def file_upload_view(request):
"""
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---