Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7165 -- Added an assertNotContains() method to the test client…

…. Thanks for the suggestion and implementation, J. Pablo Fernandez <pupeno@pupeno.com>.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7578 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 08401959d86a48413f05b55c2af4b37fb7bf3279 1 parent 46cd8bb
Russell Keith-Magee authored June 06, 2008
1  AUTHORS
@@ -142,6 +142,7 @@ answer newbie questions, and generally made Django that much better:
142 142
     Bill Fenner <fenner@gmail.com>
143 143
     Stefane Fermgier <sf@fermigier.com>
144 144
     Afonso Fernández Nogueira <fonzzo.django@gmail.com>
  145
+	J. Pablo Fernandez <pupeno@pupeno.com>
145 146
     Matthew Flanagan <http://wadofstuff.blogspot.com>
146 147
     Eric Floehr <eric@intellovations.com>
147 148
     Vincent Foley <vfoleybourgon@yahoo.ca>
12  django/test/testcases.py
@@ -128,6 +128,18 @@ def assertContains(self, response, text, count=None, status_code=200):
128 128
             self.failUnless(real_count != 0,
129 129
                             "Couldn't find '%s' in response" % text)
130 130
 
  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 not contain '%s'" % text)
  142
+
131 143
     def assertFormError(self, response, form, field, errors):
132 144
         """
133 145
         Asserts that a form used to render the response has a specific field
16  docs/testing.txt
@@ -822,6 +822,10 @@ useful for testing Web applications:
822 822
     that ``text`` appears in the content of the response. If ``count`` is
823 823
     provided, ``text`` must occur exactly ``count`` times in the response.
824 824
 
  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
+
825 829
 ``assertFormError(response, form, field, errors)``
826 830
     Asserts that a field on a form raises the provided list of errors when
827 831
     rendered on the form.
@@ -837,6 +841,12 @@ useful for testing Web applications:
837 841
     ``errors`` is an error string, or a list of error strings, that are
838 842
     expected as a result of form validation.
839 843
 
  844
+``assertTemplateUsed(response, template_name)``
  845
+    Asserts that the template with the given name was used in rendering the
  846
+    response.
  847
+
  848
+    The name is a string such as ``'admin/index.html'``.
  849
+
840 850
 ``assertTemplateNotUsed(response, template_name)``
841 851
     Asserts that the template with the given name was *not* used in rendering
842 852
     the response.
@@ -846,12 +856,6 @@ useful for testing Web applications:
846 856
     it redirected to ``expected_url`` (including any GET data), and the subsequent
847 857
     page was received with ``target_status_code``.
848 858
 
849  
-``assertTemplateUsed(response, template_name)``
850  
-    Asserts that the template with the given name was used in rendering the
851  
-    response.
852  
-
853  
-    The name is a string such as ``'admin/index.html'``.
854  
-
855 859
 E-mail services
856 860
 ---------------
857 861
 
6  tests/regressiontests/test_client_regress/models.py
@@ -11,6 +11,7 @@ def test_contains(self):
11 11
         "Responses can be inspected for content, including counting repeated substrings"
12 12
         response = self.client.get('/test_client_regress/no_template_view/')
13 13
 
  14
+        self.assertNotContains(response, 'never')
14 15
         self.assertContains(response, 'never', 0)
15 16
         self.assertContains(response, 'once')
16 17
         self.assertContains(response, 'once', 1)
@@ -18,6 +19,11 @@ def test_contains(self):
18 19
         self.assertContains(response, 'twice', 2)
19 20
 
20 21
         try:
  22
+            self.assertNotContains(response, 'once')
  23
+        except AssertionError, e:
  24
+            self.assertEquals(str(e), "Response should not contain 'once'")
  25
+            
  26
+        try:
21 27
             self.assertContains(response, 'never', 1)
22 28
         except AssertionError, e:
23 29
             self.assertEquals(str(e), "Found 0 instances of 'never' in response (expected 1)")

0 notes on commit 0840195

Please sign in to comment.
Something went wrong with that request. Please try again.