Skip to content

Commit 1548081

Browse files
committed
[soc2010/test-refactor] Provisional unittest2 support. Currently requires unittest2 to be in the pythonpath. The test_client_regress fix cleans up error checking to be less brittle (and pass with unittest2).
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/test-refactor@13406 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 2ca92d4 commit 1548081

File tree

2 files changed

+49
-49
lines changed

2 files changed

+49
-49
lines changed

django/test/testcases.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import re
2-
import unittest
2+
import unittest2 as unittest
33
from urlparse import urlsplit, urlunsplit
44
from xml.dom.minidom import parseString, Node
55

tests/regressiontests/test_client_regress/models.py

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -34,83 +34,83 @@ def test_contains(self):
3434
try:
3535
self.assertContains(response, 'text', status_code=999)
3636
except AssertionError, e:
37-
self.assertEquals(str(e), "Couldn't retrieve page: Response code was 200 (expected 999)")
37+
self.assertTrue("Couldn't retrieve page: Response code was 200 (expected 999)" in str(e))
3838
try:
3939
self.assertContains(response, 'text', status_code=999, msg_prefix='abc')
4040
except AssertionError, e:
41-
self.assertEquals(str(e), "abc: Couldn't retrieve page: Response code was 200 (expected 999)")
41+
self.assertTrue("abc: Couldn't retrieve page: Response code was 200 (expected 999)" in str(e))
4242

4343
try:
4444
self.assertNotContains(response, 'text', status_code=999)
4545
except AssertionError, e:
46-
self.assertEquals(str(e), "Couldn't retrieve page: Response code was 200 (expected 999)")
46+
self.assertTrue("Couldn't retrieve page: Response code was 200 (expected 999)" in str(e))
4747
try:
4848
self.assertNotContains(response, 'text', status_code=999, msg_prefix='abc')
4949
except AssertionError, e:
50-
self.assertEquals(str(e), "abc: Couldn't retrieve page: Response code was 200 (expected 999)")
50+
self.assertTrue("abc: Couldn't retrieve page: Response code was 200 (expected 999)" in str(e))
5151

5252
try:
5353
self.assertNotContains(response, 'once')
5454
except AssertionError, e:
55-
self.assertEquals(str(e), "Response should not contain 'once'")
55+
self.assertTrue("Response should not contain 'once'" in str(e))
5656
try:
5757
self.assertNotContains(response, 'once', msg_prefix='abc')
5858
except AssertionError, e:
59-
self.assertEquals(str(e), "abc: Response should not contain 'once'")
59+
self.assertTrue("abc: Response should not contain 'once'" in str(e))
6060

6161
try:
6262
self.assertContains(response, 'never', 1)
6363
except AssertionError, e:
64-
self.assertEquals(str(e), "Found 0 instances of 'never' in response (expected 1)")
64+
self.assertTrue("Found 0 instances of 'never' in response (expected 1)" in str(e))
6565
try:
6666
self.assertContains(response, 'never', 1, msg_prefix='abc')
6767
except AssertionError, e:
68-
self.assertEquals(str(e), "abc: Found 0 instances of 'never' in response (expected 1)")
68+
self.assertTrue("abc: Found 0 instances of 'never' in response (expected 1)" in str(e))
6969

7070
try:
7171
self.assertContains(response, 'once', 0)
7272
except AssertionError, e:
73-
self.assertEquals(str(e), "Found 1 instances of 'once' in response (expected 0)")
73+
self.assertTrue("Found 1 instances of 'once' in response (expected 0)" in str(e))
7474
try:
7575
self.assertContains(response, 'once', 0, msg_prefix='abc')
7676
except AssertionError, e:
77-
self.assertEquals(str(e), "abc: Found 1 instances of 'once' in response (expected 0)")
77+
self.assertTrue("abc: Found 1 instances of 'once' in response (expected 0)" in str(e))
7878

7979
try:
8080
self.assertContains(response, 'once', 2)
8181
except AssertionError, e:
82-
self.assertEquals(str(e), "Found 1 instances of 'once' in response (expected 2)")
82+
self.assertTrue("Found 1 instances of 'once' in response (expected 2)" in str(e))
8383
try:
8484
self.assertContains(response, 'once', 2, msg_prefix='abc')
8585
except AssertionError, e:
86-
self.assertEquals(str(e), "abc: Found 1 instances of 'once' in response (expected 2)")
86+
self.assertTrue("abc: Found 1 instances of 'once' in response (expected 2)" in str(e))
8787

8888
try:
8989
self.assertContains(response, 'twice', 1)
9090
except AssertionError, e:
91-
self.assertEquals(str(e), "Found 2 instances of 'twice' in response (expected 1)")
91+
self.assertTrue("Found 2 instances of 'twice' in response (expected 1)" in str(e))
9292
try:
9393
self.assertContains(response, 'twice', 1, msg_prefix='abc')
9494
except AssertionError, e:
95-
self.assertEquals(str(e), "abc: Found 2 instances of 'twice' in response (expected 1)")
95+
self.assertTrue("abc: Found 2 instances of 'twice' in response (expected 1)" in str(e))
9696

9797
try:
9898
self.assertContains(response, 'thrice')
9999
except AssertionError, e:
100-
self.assertEquals(str(e), "Couldn't find 'thrice' in response")
100+
self.assertTrue("Couldn't find 'thrice' in response" in str(e))
101101
try:
102102
self.assertContains(response, 'thrice', msg_prefix='abc')
103103
except AssertionError, e:
104-
self.assertEquals(str(e), "abc: Couldn't find 'thrice' in response")
104+
self.assertTrue("abc: Couldn't find 'thrice' in response" in str(e))
105105

106106
try:
107107
self.assertContains(response, 'thrice', 3)
108108
except AssertionError, e:
109-
self.assertEquals(str(e), "Found 0 instances of 'thrice' in response (expected 3)")
109+
self.assertTrue("Found 0 instances of 'thrice' in response (expected 3)" in str(e))
110110
try:
111111
self.assertContains(response, 'thrice', 3, msg_prefix='abc')
112112
except AssertionError, e:
113-
self.assertEquals(str(e), "abc: Found 0 instances of 'thrice' in response (expected 3)")
113+
self.assertTrue("abc: Found 0 instances of 'thrice' in response (expected 3)" in str(e))
114114

115115
def test_unicode_contains(self):
116116
"Unicode characters can be found in template context"
@@ -140,12 +140,12 @@ def test_no_context(self):
140140
try:
141141
self.assertTemplateUsed(response, 'GET Template')
142142
except AssertionError, e:
143-
self.assertEquals(str(e), "No templates used to render the response")
143+
self.assertTrue("No templates used to render the response" in str(e))
144144

145145
try:
146146
self.assertTemplateUsed(response, 'GET Template', msg_prefix='abc')
147147
except AssertionError, e:
148-
self.assertEquals(str(e), "abc: No templates used to render the response")
148+
self.assertTrue("abc: No templates used to render the response" in str(e))
149149

150150
def test_single_context(self):
151151
"Template assertions work when there is a single context"
@@ -154,22 +154,22 @@ def test_single_context(self):
154154
try:
155155
self.assertTemplateNotUsed(response, 'Empty GET Template')
156156
except AssertionError, e:
157-
self.assertEquals(str(e), "Template 'Empty GET Template' was used unexpectedly in rendering the response")
157+
self.assertTrue("Template 'Empty GET Template' was used unexpectedly in rendering the response" in str(e))
158158

159159
try:
160160
self.assertTemplateNotUsed(response, 'Empty GET Template', msg_prefix='abc')
161161
except AssertionError, e:
162-
self.assertEquals(str(e), "abc: Template 'Empty GET Template' was used unexpectedly in rendering the response")
162+
self.assertTrue("abc: Template 'Empty GET Template' was used unexpectedly in rendering the response" in str(e))
163163

164164
try:
165165
self.assertTemplateUsed(response, 'Empty POST Template')
166166
except AssertionError, e:
167-
self.assertEquals(str(e), "Template 'Empty POST Template' was not a template used to render the response. Actual template(s) used: Empty GET Template")
167+
self.assertTrue("Template 'Empty POST Template' was not a template used to render the response. Actual template(s) used: Empty GET Template" in str(e))
168168

169169
try:
170170
self.assertTemplateUsed(response, 'Empty POST Template', msg_prefix='abc')
171171
except AssertionError, e:
172-
self.assertEquals(str(e), "abc: Template 'Empty POST Template' was not a template used to render the response. Actual template(s) used: Empty GET Template")
172+
self.assertTrue("abc: Template 'Empty POST Template' was not a template used to render the response. Actual template(s) used: Empty GET Template" in str(e))
173173

174174
def test_multiple_context(self):
175175
"Template assertions work when there are multiple contexts"
@@ -185,17 +185,17 @@ def test_multiple_context(self):
185185
try:
186186
self.assertTemplateNotUsed(response, "form_view.html")
187187
except AssertionError, e:
188-
self.assertEquals(str(e), "Template 'form_view.html' was used unexpectedly in rendering the response")
188+
self.assertTrue("Template 'form_view.html' was used unexpectedly in rendering the response" in str(e))
189189

190190
try:
191191
self.assertTemplateNotUsed(response, 'base.html')
192192
except AssertionError, e:
193-
self.assertEquals(str(e), "Template 'base.html' was used unexpectedly in rendering the response")
193+
self.assertTrue("Template 'base.html' was used unexpectedly in rendering the response" in str(e))
194194

195195
try:
196196
self.assertTemplateUsed(response, "Valid POST Template")
197197
except AssertionError, e:
198-
self.assertEquals(str(e), "Template 'Valid POST Template' was not a template used to render the response. Actual template(s) used: form_view.html, base.html")
198+
self.assertTrue("Template 'Valid POST Template' was not a template used to render the response. Actual template(s) used: form_view.html, base.html" in str(e))
199199

200200
class AssertRedirectsTests(TestCase):
201201
def test_redirect_page(self):
@@ -205,25 +205,25 @@ def test_redirect_page(self):
205205
try:
206206
self.assertRedirects(response, '/test_client/get_view/')
207207
except AssertionError, e:
208-
self.assertEquals(str(e), "Response didn't redirect as expected: Response code was 301 (expected 302)")
208+
self.assertTrue("Response didn't redirect as expected: Response code was 301 (expected 302)" in str(e))
209209

210210
try:
211211
self.assertRedirects(response, '/test_client/get_view/', msg_prefix='abc')
212212
except AssertionError, e:
213-
self.assertEquals(str(e), "abc: Response didn't redirect as expected: Response code was 301 (expected 302)")
213+
self.assertTrue("abc: Response didn't redirect as expected: Response code was 301 (expected 302)" in str(e))
214214

215215
def test_lost_query(self):
216216
"An assertion is raised if the redirect location doesn't preserve GET parameters"
217217
response = self.client.get('/test_client/redirect_view/', {'var': 'value'})
218218
try:
219219
self.assertRedirects(response, '/test_client/get_view/')
220220
except AssertionError, e:
221-
self.assertEquals(str(e), "Response redirected to 'http://testserver/test_client/get_view/?var=value', expected 'http://testserver/test_client/get_view/'")
221+
self.assertTrue("Response redirected to 'http://testserver/test_client/get_view/?var=value', expected 'http://testserver/test_client/get_view/'" in str(e))
222222

223223
try:
224224
self.assertRedirects(response, '/test_client/get_view/', msg_prefix='abc')
225225
except AssertionError, e:
226-
self.assertEquals(str(e), "abc: Response redirected to 'http://testserver/test_client/get_view/?var=value', expected 'http://testserver/test_client/get_view/'")
226+
self.assertTrue("abc: Response redirected to 'http://testserver/test_client/get_view/?var=value', expected 'http://testserver/test_client/get_view/'" in str(e))
227227

228228
def test_incorrect_target(self):
229229
"An assertion is raised if the response redirects to another target"
@@ -232,7 +232,7 @@ def test_incorrect_target(self):
232232
# Should redirect to get_view
233233
self.assertRedirects(response, '/test_client/some_view/')
234234
except AssertionError, e:
235-
self.assertEquals(str(e), "Response didn't redirect as expected: Response code was 301 (expected 302)")
235+
self.assertTrue("Response didn't redirect as expected: Response code was 301 (expected 302)" in str(e))
236236

237237
def test_target_page(self):
238238
"An assertion is raised if the response redirect target cannot be retrieved as expected"
@@ -241,13 +241,13 @@ def test_target_page(self):
241241
# The redirect target responds with a 301 code, not 200
242242
self.assertRedirects(response, 'http://testserver/test_client/permanent_redirect_view/')
243243
except AssertionError, e:
244-
self.assertEquals(str(e), "Couldn't retrieve redirection page '/test_client/permanent_redirect_view/': response code was 301 (expected 200)")
244+
self.assertTrue("Couldn't retrieve redirection page '/test_client/permanent_redirect_view/': response code was 301 (expected 200)" in str(e))
245245

246246
try:
247247
# The redirect target responds with a 301 code, not 200
248248
self.assertRedirects(response, 'http://testserver/test_client/permanent_redirect_view/', msg_prefix='abc')
249249
except AssertionError, e:
250-
self.assertEquals(str(e), "abc: Couldn't retrieve redirection page '/test_client/permanent_redirect_view/': response code was 301 (expected 200)")
250+
self.assertTrue("abc: Couldn't retrieve redirection page '/test_client/permanent_redirect_view/': response code was 301 (expected 200)" in str(e))
251251

252252
def test_redirect_chain(self):
253253
"You can follow a redirect chain of multiple redirects"
@@ -338,12 +338,12 @@ def test_redirect_chain_on_non_redirect_page(self):
338338
try:
339339
self.assertRedirects(response, '/test_client/get_view/')
340340
except AssertionError, e:
341-
self.assertEquals(str(e), "Response didn't redirect as expected: Response code was 200 (expected 302)")
341+
self.assertTrue("Response didn't redirect as expected: Response code was 200 (expected 302)" in str(e))
342342

343343
try:
344344
self.assertRedirects(response, '/test_client/get_view/', msg_prefix='abc')
345345
except AssertionError, e:
346-
self.assertEquals(str(e), "abc: Response didn't redirect as expected: Response code was 200 (expected 302)")
346+
self.assertTrue("abc: Response didn't redirect as expected: Response code was 200 (expected 302)" in str(e))
347347

348348
def test_redirect_on_non_redirect_page(self):
349349
"An assertion is raised if the original page couldn't be retrieved as expected"
@@ -352,12 +352,12 @@ def test_redirect_on_non_redirect_page(self):
352352
try:
353353
self.assertRedirects(response, '/test_client/get_view/')
354354
except AssertionError, e:
355-
self.assertEquals(str(e), "Response didn't redirect as expected: Response code was 200 (expected 302)")
355+
self.assertTrue("Response didn't redirect as expected: Response code was 200 (expected 302)" in str(e))
356356

357357
try:
358358
self.assertRedirects(response, '/test_client/get_view/', msg_prefix='abc')
359359
except AssertionError, e:
360-
self.assertEquals(str(e), "abc: Response didn't redirect as expected: Response code was 200 (expected 302)")
360+
self.assertTrue("abc: Response didn't redirect as expected: Response code was 200 (expected 302)" in str(e))
361361

362362

363363
class AssertFormErrorTests(TestCase):
@@ -377,11 +377,11 @@ def test_unknown_form(self):
377377
try:
378378
self.assertFormError(response, 'wrong_form', 'some_field', 'Some error.')
379379
except AssertionError, e:
380-
self.assertEqual(str(e), "The form 'wrong_form' was not used to render the response")
380+
self.assertTrue("The form 'wrong_form' was not used to render the response" in str(e))
381381
try:
382382
self.assertFormError(response, 'wrong_form', 'some_field', 'Some error.', msg_prefix='abc')
383383
except AssertionError, e:
384-
self.assertEqual(str(e), "abc: The form 'wrong_form' was not used to render the response")
384+
self.assertTrue("abc: The form 'wrong_form' was not used to render the response" in str(e))
385385

386386
def test_unknown_field(self):
387387
"An assertion is raised if the field name is unknown"
@@ -399,11 +399,11 @@ def test_unknown_field(self):
399399
try:
400400
self.assertFormError(response, 'form', 'some_field', 'Some error.')
401401
except AssertionError, e:
402-
self.assertEqual(str(e), "The form 'form' in context 0 does not contain the field 'some_field'")
402+
self.assertTrue("The form 'form' in context 0 does not contain the field 'some_field'" in str(e))
403403
try:
404404
self.assertFormError(response, 'form', 'some_field', 'Some error.', msg_prefix='abc')
405405
except AssertionError, e:
406-
self.assertEqual(str(e), "abc: The form 'form' in context 0 does not contain the field 'some_field'")
406+
self.assertTrue("abc: The form 'form' in context 0 does not contain the field 'some_field'" in str(e))
407407

408408
def test_noerror_field(self):
409409
"An assertion is raised if the field doesn't have any errors"
@@ -421,11 +421,11 @@ def test_noerror_field(self):
421421
try:
422422
self.assertFormError(response, 'form', 'value', 'Some error.')
423423
except AssertionError, e:
424-
self.assertEqual(str(e), "The field 'value' on form 'form' in context 0 contains no errors")
424+
self.assertTrue("The field 'value' on form 'form' in context 0 contains no errors" in str(e))
425425
try:
426426
self.assertFormError(response, 'form', 'value', 'Some error.', msg_prefix='abc')
427427
except AssertionError, e:
428-
self.assertEqual(str(e), "abc: The field 'value' on form 'form' in context 0 contains no errors")
428+
self.assertTrue("abc: The field 'value' on form 'form' in context 0 contains no errors" in str(e))
429429

430430
def test_unknown_error(self):
431431
"An assertion is raised if the field doesn't contain the provided error"
@@ -443,11 +443,11 @@ def test_unknown_error(self):
443443
try:
444444
self.assertFormError(response, 'form', 'email', 'Some error.')
445445
except AssertionError, e:
446-
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.'])")
446+
self.assertTrue("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.'])" in str(e))
447447
try:
448448
self.assertFormError(response, 'form', 'email', 'Some error.', msg_prefix='abc')
449449
except AssertionError, e:
450-
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.'])")
450+
self.assertTrue("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.'])" in str(e))
451451

452452
def test_unknown_nonfield_error(self):
453453
"""
@@ -468,11 +468,11 @@ def test_unknown_nonfield_error(self):
468468
try:
469469
self.assertFormError(response, 'form', None, 'Some error.')
470470
except AssertionError, e:
471-
self.assertEqual(str(e), "The form 'form' in context 0 does not contain the non-field error 'Some error.' (actual errors: )")
471+
self.assertTrue("The form 'form' in context 0 does not contain the non-field error 'Some error.' (actual errors: )" in str(e))
472472
try:
473473
self.assertFormError(response, 'form', None, 'Some error.', msg_prefix='abc')
474474
except AssertionError, e:
475-
self.assertEqual(str(e), "abc: The form 'form' in context 0 does not contain the non-field error 'Some error.' (actual errors: )")
475+
self.assertTrue("abc: The form 'form' in context 0 does not contain the non-field error 'Some error.' (actual errors: )" in str(e))
476476

477477
class LoginTests(TestCase):
478478
fixtures = ['testdata']

0 commit comments

Comments
 (0)