Ticket #4901: django_ticket4901.patch

File django_ticket4901.patch, 2.7 KB (added by nis@…, 8 years ago)
  • django/test/testcases.py

     
    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,
    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.
    8181        """
    8282        self.assertEqual(response.status_code, status_code,
    8383            "Couldn't retrieve page: Response code was %d (expected %d)'" %
    8484                (response.status_code, status_code))
    8585        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,
    8791            "Found %d instances of '%s' in response (expected %d)" % (real_count, text, count))
    88 
     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

     
    472472``django.TestCase`` adds to these, providing some assertions
    473473that can be useful in testing the behavior of web sites.
    474474
    475 ``assertContains(response, text, count=1, status_code=200)``
     475``assertContains(response, text, count=None, status_code=200)``
    476476    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.
    479479
    480480``assertFormError(response, form, field, errors)``
    481481    Assert that a field on a form raised the provided list of errors when
Back to Top