Django

Code

Changeset 5731

Show
Ignore:
Timestamp:
07/20/07 09:32:20 (1 year ago)
Author:
russellm
Message:

Fixed #4901 -- Modified assertContains to provide a default check of 'any instances of text in content'. Thanks for the suggestion, nis@superlativ.dk.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/test/testcases.py

    r5620 r5731  
    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        """ 
     
    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" 
  • django/trunk/docs/testing.txt

    r5730 r5731  
    482482that 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)`` 
  • django/trunk/tests/regressiontests/test_client_regress/models.py

    r5621 r5731  
    77import os 
    88 
     9class 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         
    939class AssertTemplateUsedTests(TestCase): 
    1040    fixtures = ['testdata.json'] 
  • django/trunk/tests/regressiontests/test_client_regress/views.py

    r5609 r5731  
    55def 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 
    99def file_upload_view(request):