Ticket #10314: patch_better_unittests

File patch_better_unittests, 13.5 KB (added by christian.oudard@…, 15 years ago)

better unittests vs. patch2

Line 
1diff --git a/django/test/testcases.py b/django/test/testcases.py
2index 8c73c63..b353ccd 100644
3--- a/django/test/testcases.py
4+++ b/django/test/testcases.py
5@@ -273,7 +273,7 @@ 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=None):
11 """Asserts that a response redirected to a specific URL, and that the
12 redirect URL can be loaded.
13
14@@ -283,23 +283,23 @@ class TransactionTestCase(unittest.TestCase):
15 if hasattr(response, 'redirect_chain'):
16 # The request was a followed redirect
17 self.failUnless(len(response.redirect_chain) > 0,
18- ("Response didn't redirect as expected: Response code was %d"
19+ msg or ("Response didn't redirect as expected: Response code was %d"
20 " (expected %d)" % (response.status_code, status_code)))
21
22 self.assertEqual(response.redirect_chain[0][1], status_code,
23- ("Initial response didn't redirect as expected: Response code was %d"
24+ msg or ("Initial response didn't redirect as expected: Response code was %d"
25 " (expected %d)" % (response.redirect_chain[0][1], status_code)))
26
27 url, status_code = response.redirect_chain[-1]
28
29 self.assertEqual(response.status_code, target_status_code,
30- ("Response didn't redirect as expected: Final Response code was %d"
31+ msg or ("Response didn't redirect as expected: Final Response code was %d"
32 " (expected %d)" % (response.status_code, target_status_code)))
33
34 else:
35 # Not a followed redirect
36 self.assertEqual(response.status_code, status_code,
37- ("Response didn't redirect as expected: Response code was %d"
38+ msg or ("Response didn't redirect as expected: Response code was %d"
39 " (expected %d)" % (response.status_code, status_code)))
40
41 url = response['Location']
42@@ -310,7 +310,7 @@ class TransactionTestCase(unittest.TestCase):
43 # Get the redirection page, using the same client that was used
44 # to obtain the original response.
45 self.assertEqual(redirect_response.status_code, target_status_code,
46- ("Couldn't retrieve redirection page '%s': response code was %d"
47+ msg or ("Couldn't retrieve redirection page '%s': response code was %d"
48 " (expected %d)") %
49 (path, redirect_response.status_code, target_status_code))
50
51@@ -320,10 +320,10 @@ class TransactionTestCase(unittest.TestCase):
52 e_query, e_fragment))
53
54 self.assertEqual(url, expected_url,
55- "Response redirected to '%s', expected '%s'" % (url, expected_url))
56+ msg or "Response redirected to '%s', expected '%s'" % (url, expected_url))
57
58
59- def assertContains(self, response, text, count=None, status_code=200):
60+ def assertContains(self, response, text, count=None, status_code=200, msg=None):
61 """
62 Asserts that a response indicates that a page was retrieved
63 successfully, (i.e., the HTTP status code was as expected), and that
64@@ -332,32 +332,32 @@ class TransactionTestCase(unittest.TestCase):
65 if the text occurs at least once in the response.
66 """
67 self.assertEqual(response.status_code, status_code,
68- "Couldn't retrieve page: Response code was %d (expected %d)'" %
69+ msg or "Couldn't retrieve page: Response code was %d (expected %d)'" %
70 (response.status_code, status_code))
71 text = smart_str(text, response._charset)
72 real_count = response.content.count(text)
73 if count is not None:
74 self.assertEqual(real_count, count,
75- "Found %d instances of '%s' in response (expected %d)" %
76+ msg or "Found %d instances of '%s' in response (expected %d)" %
77 (real_count, text, count))
78 else:
79 self.failUnless(real_count != 0,
80- "Couldn't find '%s' in response" % text)
81+ msg or "Couldn't find '%s' in response" % text)
82
83- def assertNotContains(self, response, text, status_code=200):
84+ def assertNotContains(self, response, text, status_code=200, msg=None):
85 """
86 Asserts that a response indicates that a page was retrieved
87 successfully, (i.e., the HTTP status code was as expected), and that
88 ``text`` doesn't occurs in the content of the response.
89 """
90 self.assertEqual(response.status_code, status_code,
91- "Couldn't retrieve page: Response code was %d (expected %d)'" %
92+ msg or "Couldn't retrieve page: Response code was %d (expected %d)'" %
93 (response.status_code, status_code))
94 text = smart_str(text, response._charset)
95 self.assertEqual(response.content.count(text),
96- 0, "Response should not contain '%s'" % text)
97+ 0, msg or "Response should not contain '%s'" % text)
98
99- def assertFormError(self, response, form, field, errors):
100+ def assertFormError(self, response, form, field, errors, msg=None):
101 """
102 Asserts that a form used to render the response has a specific field
103 error.
104@@ -365,7 +365,7 @@ class TransactionTestCase(unittest.TestCase):
105 # Put context(s) into a list to simplify processing.
106 contexts = to_list(response.context)
107 if not contexts:
108- self.fail('Response did not use any contexts to render the'
109+ self.fail(msg or 'Response did not use any contexts to render the'
110 ' response')
111
112 # Put error(s) into a list to simplify processing.
113@@ -382,49 +382,49 @@ class TransactionTestCase(unittest.TestCase):
114 if field in context[form].errors:
115 field_errors = context[form].errors[field]
116 self.failUnless(err in field_errors,
117- "The field '%s' on form '%s' in"
118+ msg or "The field '%s' on form '%s' in"
119 " context %d does not contain the"
120 " error '%s' (actual errors: %s)" %
121 (field, form, i, err,
122 repr(field_errors)))
123 elif field in context[form].fields:
124- self.fail("The field '%s' on form '%s' in context %d"
125+ self.fail(msg or "The field '%s' on form '%s' in context %d"
126 " contains no errors" % (field, form, i))
127 else:
128- self.fail("The form '%s' in context %d does not"
129+ msg or self.fail(msg or "The form '%s' in context %d does not"
130 " contain the field '%s'" %
131 (form, i, field))
132 else:
133 non_field_errors = context[form].non_field_errors()
134 self.failUnless(err in non_field_errors,
135- "The form '%s' in context %d does not contain the"
136+ msg or "The form '%s' in context %d does not contain the"
137 " non-field error '%s' (actual errors: %s)" %
138 (form, i, err, non_field_errors))
139 if not found_form:
140- self.fail("The form '%s' was not used to render the response" %
141- form)
142+ self.fail(msg or "The form '%s' was not used to render the response" %
143+ form)
144
145- def assertTemplateUsed(self, response, template_name):
146+ def assertTemplateUsed(self, response, template_name, msg=None):
147 """
148 Asserts that the template with the provided name was used in rendering
149 the response.
150 """
151 template_names = [t.name for t in to_list(response.template)]
152 if not template_names:
153- self.fail('No templates used to render the response')
154+ self.fail(msg or 'No templates used to render the response')
155 self.failUnless(template_name in template_names,
156- (u"Template '%s' was not a template used to render the response."
157+ msg or (u"Template '%s' was not a template used to render the response."
158 u" Actual template(s) used: %s") % (template_name,
159 u', '.join(template_names)))
160
161- def assertTemplateNotUsed(self, response, template_name):
162+ def assertTemplateNotUsed(self, response, template_name, msg=None):
163 """
164 Asserts that the template with the provided name was NOT used in
165 rendering the response.
166 """
167 template_names = [t.name for t in to_list(response.template)]
168 self.failIf(template_name in template_names,
169- (u"Template '%s' was used unexpectedly in rendering the"
170+ msg or (u"Template '%s' was used unexpectedly in rendering the"
171 u" response") % template_name)
172
173 class TestCase(TransactionTestCase):
174diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py
175index be2cb8f..6c295c6 100644
176--- a/tests/regressiontests/test_client_regress/models.py
177+++ b/tests/regressiontests/test_client_regress/models.py
178@@ -79,6 +79,34 @@ class AssertContainsTests(TestCase):
179 self.assertNotContains(r, u'はたけ')
180 self.assertNotContains(r, '\xe3\x81\xaf\xe3\x81\x9f\xe3\x81\x91'.decode('utf-8'))
181
182+ def test_custom_message(self):
183+ "Custom message argument is used correctly."
184+ url = '/test_client_regress/no_template_view/'
185+ response = self.client.get(url)
186+ m = 'custom message'
187+
188+ try:
189+ self.assertContains(response, 'never', msg=m)
190+ except AssertionError, e:
191+ self.assertEquals(str(e), m)
192+ try:
193+ self.assertContains(response, 'never', 1, msg=m)
194+ except AssertionError, e:
195+ self.assertEquals(str(e), m)
196+ try:
197+ self.assertContains(response, 'text', status_code=999, msg=m)
198+ except AssertionError, e:
199+ self.assertEquals(str(e), m)
200+
201+ try:
202+ self.assertNotContains(response, 'once', msg=m)
203+ except AssertionError, e:
204+ self.assertEquals(str(e), m)
205+ try:
206+ self.assertNotContains(response, 'once', status_code=999, msg=m)
207+ except AssertionError, e:
208+ self.assertEquals(str(e), m)
209+
210 class AssertTemplateUsedTests(TestCase):
211 fixtures = ['testdata.json']
212
213@@ -291,6 +319,12 @@ class AssertFormErrorTests(TestCase):
214 except AssertionError, e:
215 self.assertEqual(str(e), "The form 'wrong_form' was not used to render the response")
216
217+ # Test message argument
218+ try:
219+ self.assertFormError(response, 'wrong_form', 'some_field', 'Some error.', msg='abc')
220+ except AssertionError, e:
221+ self.assertEqual(str(e), "abc")
222+
223 def test_unknown_field(self):
224 "An assertion is raised if the field name is unknown"
225 post_data = {
226@@ -309,6 +343,12 @@ class AssertFormErrorTests(TestCase):
227 except AssertionError, e:
228 self.assertEqual(str(e), "The form 'form' in context 0 does not contain the field 'some_field'")
229
230+ # Test message argument
231+ try:
232+ self.assertFormError(response, 'form', 'some_field', 'Some error.', msg='abc')
233+ except AssertionError, e:
234+ self.assertEqual(str(e), "abc")
235+
236 def test_noerror_field(self):
237 "An assertion is raised if the field doesn't have any errors"
238 post_data = {
239@@ -327,6 +367,12 @@ class AssertFormErrorTests(TestCase):
240 except AssertionError, e:
241 self.assertEqual(str(e), "The field 'value' on form 'form' in context 0 contains no errors")
242
243+ # Test message argument
244+ try:
245+ self.assertFormError(response, 'form', 'value', 'Some error.', msg='abc')
246+ except AssertionError, e:
247+ self.assertEqual(str(e), "abc")
248+
249 def test_unknown_error(self):
250 "An assertion is raised if the field doesn't contain the provided error"
251 post_data = {
252@@ -345,6 +391,12 @@ class AssertFormErrorTests(TestCase):
253 except AssertionError, e:
254 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.'])")
255
256+ # Test message argument
257+ try:
258+ self.assertFormError(response, 'form', 'email', 'Some error.', msg='abc')
259+ except AssertionError, e:
260+ self.assertEqual(str(e), "abc")
261+
262 def test_unknown_nonfield_error(self):
263 """
264 Checks that an assertion is raised if the form's non field errors
265@@ -366,6 +418,12 @@ class AssertFormErrorTests(TestCase):
266 except AssertionError, e:
267 self.assertEqual(str(e), "The form 'form' in context 0 does not contain the non-field error 'Some error.' (actual errors: )")
268
269+ # Test message argument
270+ try:
271+ self.assertFormError(response, 'form', None, 'Some error.', msg='abc')
272+ except AssertionError, e:
273+ self.assertEqual(str(e), "abc")
274+
275 class LoginTests(TestCase):
276 fixtures = ['testdata']
277
Back to Top