Django

Code

Ticket #7165: assert_not_contains.2.diff

File assert_not_contains.2.diff, 2.4 kB (added by J. Pablo Fernandez <pupeno@pupeno.com>, 7 months ago)

Assertion with test and documentation.

  • a/django/test/testcases.py

    old new  
    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 
  • a/docs/testing.txt

    old new  
    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. 
  • a/tests/modeltests/test_client/models.py

    old new  
    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