Ticket #10314: patch_with_docs

File patch_with_docs, 22.0 KB (added by christian.oudard@…, 15 years ago)

Added documentation for msg_prefix

Line 
1diff --git a/django/test/testcases.py b/django/test/testcases.py
2index 8c73c63..9539ae9 100644
3--- a/django/test/testcases.py
4+++ b/django/test/testcases.py
5@@ -273,34 +273,41 @@ class TransactionTestCase(unittest.TestCase):
6 clear_url_caches()
7
8 def assertRedirects(self, response, expected_url, status_code=302,
9- target_status_code=200, host=None):
10+ target_status_code=200, host=None, msg_prefix=''):
11 """Asserts that a response redirected to a specific URL, and that the
12 redirect URL can be loaded.
13
14 Note that assertRedirects won't work for external links since it uses
15 TestClient to do a request.
16 """
17+ if msg_prefix:
18+ msg_prefix += ": "
19+
20 if hasattr(response, 'redirect_chain'):
21 # The request was a followed redirect
22 self.failUnless(len(response.redirect_chain) > 0,
23- ("Response didn't redirect as expected: Response code was %d"
24- " (expected %d)" % (response.status_code, status_code)))
25+ msg_prefix + "Response didn't redirect as expected: Response"
26+ " code was %d (expected %d)" %
27+ (response.status_code, status_code))
28
29 self.assertEqual(response.redirect_chain[0][1], status_code,
30- ("Initial response didn't redirect as expected: Response code was %d"
31- " (expected %d)" % (response.redirect_chain[0][1], status_code)))
32+ msg_prefix + "Initial response didn't redirect as expected:"
33+ " Response code was %d (expected %d)" %
34+ (response.redirect_chain[0][1], status_code))
35
36 url, status_code = response.redirect_chain[-1]
37
38 self.assertEqual(response.status_code, target_status_code,
39- ("Response didn't redirect as expected: Final Response code was %d"
40- " (expected %d)" % (response.status_code, target_status_code)))
41+ msg_prefix + "Response didn't redirect as expected: Final"
42+ " Response code was %d (expected %d)" %
43+ (response.status_code, target_status_code))
44
45 else:
46 # Not a followed redirect
47 self.assertEqual(response.status_code, status_code,
48- ("Response didn't redirect as expected: Response code was %d"
49- " (expected %d)" % (response.status_code, status_code)))
50+ msg_prefix + "Response didn't redirect as expected: Response"
51+ " code was %d (expected %d)" %
52+ (response.status_code, status_code))
53
54 url = response['Location']
55 scheme, netloc, path, query, fragment = urlsplit(url)
56@@ -310,9 +317,9 @@ class TransactionTestCase(unittest.TestCase):
57 # Get the redirection page, using the same client that was used
58 # to obtain the original response.
59 self.assertEqual(redirect_response.status_code, target_status_code,
60- ("Couldn't retrieve redirection page '%s': response code was %d"
61- " (expected %d)") %
62- (path, redirect_response.status_code, target_status_code))
63+ msg_prefix + "Couldn't retrieve redirection page '%s':"
64+ " response code was %d (expected %d)" %
65+ (path, redirect_response.status_code, target_status_code))
66
67 e_scheme, e_netloc, e_path, e_query, e_fragment = urlsplit(expected_url)
68 if not (e_scheme or e_netloc):
69@@ -320,10 +327,11 @@ class TransactionTestCase(unittest.TestCase):
70 e_query, e_fragment))
71
72 self.assertEqual(url, expected_url,
73- "Response redirected to '%s', expected '%s'" % (url, expected_url))
74-
75+ msg_prefix + "Response redirected to '%s', expected '%s'" %
76+ (url, expected_url))
77
78- def assertContains(self, response, text, count=None, status_code=200):
79+ def assertContains(self, response, text, count=None, status_code=200,
80+ msg_prefix=''):
81 """
82 Asserts that a response indicates that a page was retrieved
83 successfully, (i.e., the HTTP status code was as expected), and that
84@@ -331,42 +339,52 @@ class TransactionTestCase(unittest.TestCase):
85 If ``count`` is None, the count doesn't matter - the assertion is true
86 if the text occurs at least once in the response.
87 """
88+ if msg_prefix:
89+ msg_prefix += ": "
90+
91 self.assertEqual(response.status_code, status_code,
92- "Couldn't retrieve page: Response code was %d (expected %d)'" %
93- (response.status_code, status_code))
94+ msg_prefix + "Couldn't retrieve page: Response code was %d"
95+ " (expected %d)" % (response.status_code, status_code))
96 text = smart_str(text, response._charset)
97 real_count = response.content.count(text)
98 if count is not None:
99 self.assertEqual(real_count, count,
100- "Found %d instances of '%s' in response (expected %d)" %
101- (real_count, text, count))
102+ msg_prefix + "Found %d instances of '%s' in response"
103+ " (expected %d)" % (real_count, text, count))
104 else:
105 self.failUnless(real_count != 0,
106- "Couldn't find '%s' in response" % text)
107+ msg_prefix + "Couldn't find '%s' in response" % text)
108
109- def assertNotContains(self, response, text, status_code=200):
110+ def assertNotContains(self, response, text, status_code=200,
111+ msg_prefix=''):
112 """
113 Asserts that a response indicates that a page was retrieved
114 successfully, (i.e., the HTTP status code was as expected), and that
115 ``text`` doesn't occurs in the content of the response.
116 """
117+ if msg_prefix:
118+ msg_prefix += ": "
119+
120 self.assertEqual(response.status_code, status_code,
121- "Couldn't retrieve page: Response code was %d (expected %d)'" %
122- (response.status_code, status_code))
123+ msg_prefix + "Couldn't retrieve page: Response code was %d"
124+ " (expected %d)" % (response.status_code, status_code))
125 text = smart_str(text, response._charset)
126- self.assertEqual(response.content.count(text),
127- 0, "Response should not contain '%s'" % text)
128+ self.assertEqual(response.content.count(text), 0,
129+ msg_prefix + "Response should not contain '%s'" % text)
130
131- def assertFormError(self, response, form, field, errors):
132+ def assertFormError(self, response, form, field, errors, msg_prefix=''):
133 """
134 Asserts that a form used to render the response has a specific field
135 error.
136 """
137+ if msg_prefix:
138+ msg_prefix += ": "
139+
140 # Put context(s) into a list to simplify processing.
141 contexts = to_list(response.context)
142 if not contexts:
143- self.fail('Response did not use any contexts to render the'
144- ' response')
145+ self.fail(msg_prefix + "Response did not use any contexts to"
146+ "render the response")
147
148 # Put error(s) into a list to simplify processing.
149 errors = to_list(errors)
150@@ -382,50 +400,57 @@ class TransactionTestCase(unittest.TestCase):
151 if field in context[form].errors:
152 field_errors = context[form].errors[field]
153 self.failUnless(err in field_errors,
154- "The field '%s' on form '%s' in"
155- " context %d does not contain the"
156- " error '%s' (actual errors: %s)" %
157- (field, form, i, err,
158- repr(field_errors)))
159+ msg_prefix + "The field '%s' on form '%s' in"
160+ " context %d does not contain the error '%s'"
161+ " (actual errors: %s)" %
162+ (field, form, i, err, repr(field_errors)))
163 elif field in context[form].fields:
164- self.fail("The field '%s' on form '%s' in context %d"
165- " contains no errors" % (field, form, i))
166+ self.fail(msg_prefix + "The field '%s' on form '%s'"
167+ " in context %d contains no errors" %
168+ (field, form, i))
169 else:
170- self.fail("The form '%s' in context %d does not"
171- " contain the field '%s'" %
172+ self.fail(msg_prefix + "The form '%s' in context %d"
173+ " does not contain the field '%s'" %
174 (form, i, field))
175 else:
176 non_field_errors = context[form].non_field_errors()
177 self.failUnless(err in non_field_errors,
178- "The form '%s' in context %d does not contain the"
179- " non-field error '%s' (actual errors: %s)" %
180+ msg_prefix + "The form '%s' in context %d does not"
181+ " contain the non-field error '%s'"
182+ " (actual errors: %s)" %
183 (form, i, err, non_field_errors))
184 if not found_form:
185- self.fail("The form '%s' was not used to render the response" %
186- form)
187+ self.fail(msg_prefix + "The form '%s' was not used to render the"
188+ " response" % form)
189
190- def assertTemplateUsed(self, response, template_name):
191+ def assertTemplateUsed(self, response, template_name, msg_prefix=''):
192 """
193 Asserts that the template with the provided name was used in rendering
194 the response.
195 """
196+ if msg_prefix:
197+ msg_prefix += ": "
198+
199 template_names = [t.name for t in to_list(response.template)]
200 if not template_names:
201- self.fail('No templates used to render the response')
202+ self.fail(msg_prefix + "No templates used to render the response")
203 self.failUnless(template_name in template_names,
204- (u"Template '%s' was not a template used to render the response."
205- u" Actual template(s) used: %s") % (template_name,
206- u', '.join(template_names)))
207+ msg_prefix + "Template '%s' was not a template used to render"
208+ " the response. Actual template(s) used: %s" %
209+ (template_name, u', '.join(template_names)))
210
211- def assertTemplateNotUsed(self, response, template_name):
212+ def assertTemplateNotUsed(self, response, template_name, msg_prefix=''):
213 """
214 Asserts that the template with the provided name was NOT used in
215 rendering the response.
216 """
217+ if msg_prefix:
218+ msg_prefix += ": "
219+
220 template_names = [t.name for t in to_list(response.template)]
221 self.failIf(template_name in template_names,
222- (u"Template '%s' was used unexpectedly in rendering the"
223- u" response") % template_name)
224+ msg_prefix + "Template '%s' was used unexpectedly in rendering"
225+ " the response" % template_name)
226
227 class TestCase(TransactionTestCase):
228 """
229diff --git a/docs/topics/testing.txt b/docs/topics/testing.txt
230index 25d2f08..33cf166 100644
231--- a/docs/topics/testing.txt
232+++ b/docs/topics/testing.txt
233@@ -1049,23 +1049,31 @@ Assertions
234
235 .. versionadded:: 1.0
236
237+.. versionchanged:: 1.2
238+ Addded ``msg_prefix`` argument.
239+
240 As Python's normal ``unittest.TestCase`` class implements assertion methods
241 such as ``assertTrue`` and ``assertEquals``, Django's custom ``TestCase`` class
242 provides a number of custom assertion methods that are useful for testing Web
243 applications:
244
245-.. method:: TestCase.assertContains(response, text, count=None, status_code=200)
246+The failure messages given by the assertion methods can be customized with the
247+``msg_prefix`` argument. This string will be prefixed to the standard failure
248+message, so that you can give additional context information for easier
249+debugging.
250+
251+.. method:: TestCase.assertContains(response, text, count=None, status_code=200, msg_prefix='')
252
253 Asserts that a ``Response`` instance produced the given ``status_code`` and
254 that ``text`` appears in the content of the response. If ``count`` is
255 provided, ``text`` must occur exactly ``count`` times in the response.
256
257-.. method:: TestCase.assertNotContains(response, text, status_code=200)
258+.. method:: TestCase.assertNotContains(response, text, status_code=200, msg_prefix='')
259
260 Asserts that a ``Response`` instance produced the given ``status_code`` and
261 that ``text`` does not appears in the content of the response.
262
263-.. method:: TestCase.assertFormError(response, form, field, errors)
264+.. method:: TestCase.assertFormError(response, form, field, errors, msg_prefix='')
265
266 Asserts that a field on a form raises the provided list of errors when
267 rendered on the form.
268@@ -1080,19 +1088,19 @@ applications:
269 ``errors`` is an error string, or a list of error strings, that are
270 expected as a result of form validation.
271
272-.. method:: TestCase.assertTemplateUsed(response, template_name)
273+.. method:: TestCase.assertTemplateUsed(response, template_name, msg_prefix='')
274
275 Asserts that the template with the given name was used in rendering the
276 response.
277
278 The name is a string such as ``'admin/index.html'``.
279
280-.. method:: TestCase.assertTemplateNotUsed(response, template_name)
281+.. method:: TestCase.assertTemplateNotUsed(response, template_name, msg_prefix='')
282
283 Asserts that the template with the given name was *not* used in rendering
284 the response.
285
286-.. method:: TestCase.assertRedirects(response, expected_url, status_code=302, target_status_code=200)
287+.. method:: TestCase.assertRedirects(response, expected_url, status_code=302, target_status_code=200, msg_prefix='')
288
289 Asserts that the response return a ``status_code`` redirect status, it
290 redirected to ``expected_url`` (including any GET data), and the final
291diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py
292index be2cb8f..813087c 100644
293--- a/tests/regressiontests/test_client_regress/models.py
294+++ b/tests/regressiontests/test_client_regress/models.py
295@@ -31,39 +31,85 @@ class AssertContainsTests(TestCase):
296 self.assertContains(response, 'twice', 2)
297
298 try:
299+ self.assertContains(response, 'text', status_code=999)
300+ except AssertionError, e:
301+ self.assertEquals(str(e), "Couldn't retrieve page: Response code was 200 (expected 999)")
302+ try:
303+ self.assertContains(response, 'text', status_code=999, msg_prefix='abc')
304+ except AssertionError, e:
305+ self.assertEquals(str(e), "abc: Couldn't retrieve page: Response code was 200 (expected 999)")
306+
307+ try:
308+ self.assertNotContains(response, 'text', status_code=999)
309+ except AssertionError, e:
310+ self.assertEquals(str(e), "Couldn't retrieve page: Response code was 200 (expected 999)")
311+ try:
312+ self.assertNotContains(response, 'text', status_code=999, msg_prefix='abc')
313+ except AssertionError, e:
314+ self.assertEquals(str(e), "abc: Couldn't retrieve page: Response code was 200 (expected 999)")
315+
316+ try:
317 self.assertNotContains(response, 'once')
318 except AssertionError, e:
319 self.assertEquals(str(e), "Response should not contain 'once'")
320+ try:
321+ self.assertNotContains(response, 'once', msg_prefix='abc')
322+ except AssertionError, e:
323+ self.assertEquals(str(e), "abc: Response should not contain 'once'")
324
325 try:
326 self.assertContains(response, 'never', 1)
327 except AssertionError, e:
328 self.assertEquals(str(e), "Found 0 instances of 'never' in response (expected 1)")
329+ try:
330+ self.assertContains(response, 'never', 1, msg_prefix='abc')
331+ except AssertionError, e:
332+ self.assertEquals(str(e), "abc: Found 0 instances of 'never' in response (expected 1)")
333
334 try:
335 self.assertContains(response, 'once', 0)
336 except AssertionError, e:
337 self.assertEquals(str(e), "Found 1 instances of 'once' in response (expected 0)")
338+ try:
339+ self.assertContains(response, 'once', 0, msg_prefix='abc')
340+ except AssertionError, e:
341+ self.assertEquals(str(e), "abc: Found 1 instances of 'once' in response (expected 0)")
342
343 try:
344 self.assertContains(response, 'once', 2)
345 except AssertionError, e:
346 self.assertEquals(str(e), "Found 1 instances of 'once' in response (expected 2)")
347+ try:
348+ self.assertContains(response, 'once', 2, msg_prefix='abc')
349+ except AssertionError, e:
350+ self.assertEquals(str(e), "abc: Found 1 instances of 'once' in response (expected 2)")
351
352 try:
353 self.assertContains(response, 'twice', 1)
354 except AssertionError, e:
355 self.assertEquals(str(e), "Found 2 instances of 'twice' in response (expected 1)")
356+ try:
357+ self.assertContains(response, 'twice', 1, msg_prefix='abc')
358+ except AssertionError, e:
359+ self.assertEquals(str(e), "abc: Found 2 instances of 'twice' in response (expected 1)")
360
361 try:
362 self.assertContains(response, 'thrice')
363 except AssertionError, e:
364 self.assertEquals(str(e), "Couldn't find 'thrice' in response")
365+ try:
366+ self.assertContains(response, 'thrice', msg_prefix='abc')
367+ except AssertionError, e:
368+ self.assertEquals(str(e), "abc: Couldn't find 'thrice' in response")
369
370 try:
371 self.assertContains(response, 'thrice', 3)
372 except AssertionError, e:
373 self.assertEquals(str(e), "Found 0 instances of 'thrice' in response (expected 3)")
374+ try:
375+ self.assertContains(response, 'thrice', 3, msg_prefix='abc')
376+ except AssertionError, e:
377+ self.assertEquals(str(e), "abc: Found 0 instances of 'thrice' in response (expected 3)")
378
379 def test_unicode_contains(self):
380 "Unicode characters can be found in template context"
381@@ -79,6 +125,7 @@ class AssertContainsTests(TestCase):
382 self.assertNotContains(r, u'はたけ')
383 self.assertNotContains(r, '\xe3\x81\xaf\xe3\x81\x9f\xe3\x81\x91'.decode('utf-8'))
384
385+
386 class AssertTemplateUsedTests(TestCase):
387 fixtures = ['testdata.json']
388
389@@ -290,6 +337,10 @@ class AssertFormErrorTests(TestCase):
390 self.assertFormError(response, 'wrong_form', 'some_field', 'Some error.')
391 except AssertionError, e:
392 self.assertEqual(str(e), "The form 'wrong_form' was not used to render the response")
393+ try:
394+ self.assertFormError(response, 'wrong_form', 'some_field', 'Some error.', msg_prefix='abc')
395+ except AssertionError, e:
396+ self.assertEqual(str(e), "abc: The form 'wrong_form' was not used to render the response")
397
398 def test_unknown_field(self):
399 "An assertion is raised if the field name is unknown"
400@@ -308,6 +359,10 @@ class AssertFormErrorTests(TestCase):
401 self.assertFormError(response, 'form', 'some_field', 'Some error.')
402 except AssertionError, e:
403 self.assertEqual(str(e), "The form 'form' in context 0 does not contain the field 'some_field'")
404+ try:
405+ self.assertFormError(response, 'form', 'some_field', 'Some error.', msg_prefix='abc')
406+ except AssertionError, e:
407+ self.assertEqual(str(e), "abc: The form 'form' in context 0 does not contain the field 'some_field'")
408
409 def test_noerror_field(self):
410 "An assertion is raised if the field doesn't have any errors"
411@@ -326,6 +381,10 @@ class AssertFormErrorTests(TestCase):
412 self.assertFormError(response, 'form', 'value', 'Some error.')
413 except AssertionError, e:
414 self.assertEqual(str(e), "The field 'value' on form 'form' in context 0 contains no errors")
415+ try:
416+ self.assertFormError(response, 'form', 'value', 'Some error.', msg_prefix='abc')
417+ except AssertionError, e:
418+ self.assertEqual(str(e), "abc: The field 'value' on form 'form' in context 0 contains no errors")
419
420 def test_unknown_error(self):
421 "An assertion is raised if the field doesn't contain the provided error"
422@@ -344,6 +403,10 @@ class AssertFormErrorTests(TestCase):
423 self.assertFormError(response, 'form', 'email', 'Some error.')
424 except AssertionError, e:
425 self.assertEqual(str(e), "The field 'email' on form 'form' in context 0 does not contain the error 'Some error.' (actual errors: [u'Enter a valid e-mail address.'])")
426+ try:
427+ self.assertFormError(response, 'form', 'email', 'Some error.', msg_prefix='abc')
428+ except AssertionError, e:
429+ self.assertEqual(str(e), "abc: The field 'email' on form 'form' in context 0 does not contain the error 'Some error.' (actual errors: [u'Enter a valid e-mail address.'])")
430
431 def test_unknown_nonfield_error(self):
432 """
433@@ -365,6 +428,10 @@ class AssertFormErrorTests(TestCase):
434 self.assertFormError(response, 'form', None, 'Some error.')
435 except AssertionError, e:
436 self.assertEqual(str(e), "The form 'form' in context 0 does not contain the non-field error 'Some error.' (actual errors: )")
437+ try:
438+ self.assertFormError(response, 'form', None, 'Some error.', msg_prefix='abc')
439+ except AssertionError, e:
440+ self.assertEqual(str(e), "abc: The form 'form' in context 0 does not contain the non-field error 'Some error.' (actual errors: )")
441
442 class LoginTests(TestCase):
443 fixtures = ['testdata']
Back to Top