Ticket #7165: assert_not_contains.2.diff

File assert_not_contains.2.diff, 2.4 KB (added by J. Pablo Fernandez <pupeno@…>, 17 years ago)

Assertion with test and documentation.

  • django/test/testcases.py

    diff --git a/django/test/testcases.py b/django/test/testcases.py
    index 5589443..8f76d6e 100644
    a b class TestCase(unittest.TestCase):  
    128128            self.failUnless(real_count != 0,
    129129                            "Couldn't find '%s' in response" % text)
    130130
     131    def assertNotContains(self, response, text, status_code=200):
     132        """
     133        Asserts that a response indicates that a page was retrieved
     134        successfully, (i.e., the HTTP status code was as expected), and that
     135        ``text`` doesn't occurs in the content of the response.
     136        """
     137        self.assertEqual(response.status_code, status_code,
     138            "Couldn't retrieve page: Response code was %d (expected %d)'" %
     139                (response.status_code, status_code))
     140        self.assertEqual(response.content.count(text), 0,
     141                         "Response should't have had '%s'" % text)
     142
    131143    def assertFormError(self, response, form, field, errors):
    132144        """
    133145        Asserts that a form used to render the response has a specific field
  • docs/testing.txt

    diff --git a/docs/testing.txt b/docs/testing.txt
    index 0ff3cce..97f5ae4 100644
    a b useful for testing Web applications:  
    822822    that ``text`` appears in the content of the response. If ``count`` is
    823823    provided, ``text`` must occur exactly ``count`` times in the response.
    824824
     825``assertNotContains(response, text, status_code=200)``
     826    Asserts that a ``Response`` instance produced the given ``status_code`` and
     827    that ``text`` does not appears in the content of the response.
     828
    825829``assertFormError(response, form, field, errors)``
    826830    Asserts that a field on a form raises the provided list of errors when
    827831    rendered on the form.
  • tests/modeltests/test_client/models.py

    diff --git a/tests/modeltests/test_client/models.py b/tests/modeltests/test_client/models.py
    index f5b1988..bc664dd 100644
    a b class ClientTest(TestCase):  
    406406        self.assertEqual(mail.outbox[1].to[0], 'second@example.com')
    407407        self.assertEqual(mail.outbox[1].to[1], 'third@example.com')
    408408
     409    def test_not_contains(self):
     410        response = self.client.get('/test_client/get_view/')
     411        self.assertNotContains(response, "surely not there")
     412        self.assertRaises(AssertionError,
     413                          self.assertNotContains, response, "This is a test")
     414
Back to Top