Ticket #10314: assert_msg_patch

File assert_msg_patch, 4.4 KB (added by christian.oudard@…, 15 years ago)
Line 
1diff --git a/django/test/testcases.py b/django/test/testcases.py
2index 8c73c63..dc9aa81 100644
3--- a/django/test/testcases.py
4+++ b/django/test/testcases.py
5@@ -203,6 +203,24 @@ class DocTestRunner(doctest.DocTestRunner):
6 # side effects on other tests.
7 transaction.rollback_unless_managed()
8
9+
10+import sys
11+from django.utils.decorators import auto_adapt_to_methods, wraps
12+def custom_assert_message(assert_method):
13+ """
14+ Decorator for assert methods which adds a msg parameter. This parameter
15+ will replace the message in any AssertionErrors raised.
16+ """
17+ def _wrapped_method(*args, **kwargs):
18+ msg = kwargs.pop('msg', None)
19+ try:
20+ return assert_method(*args, **kwargs)
21+ except AssertionError, e:
22+ raise e.__class__(msg or str(e)), None, sys.exc_info()[2]
23+ return wraps(assert_method)(_wrapped_method)
24+custom_assert_message = auto_adapt_to_methods(custom_assert_message)
25+
26+
27 class TransactionTestCase(unittest.TestCase):
28 def _pre_setup(self):
29 """Performs any pre-test setup. This includes:
30@@ -343,6 +361,7 @@ class TransactionTestCase(unittest.TestCase):
31 else:
32 self.failUnless(real_count != 0,
33 "Couldn't find '%s' in response" % text)
34+ assertContains = custom_assert_message(assertContains)
35
36 def assertNotContains(self, response, text, status_code=200):
37 """
38@@ -356,6 +375,7 @@ class TransactionTestCase(unittest.TestCase):
39 text = smart_str(text, response._charset)
40 self.assertEqual(response.content.count(text),
41 0, "Response should not contain '%s'" % text)
42+ assertNotContains = custom_assert_message(assertNotContains)
43
44 def assertFormError(self, response, form, field, errors):
45 """
46@@ -403,6 +423,7 @@ class TransactionTestCase(unittest.TestCase):
47 if not found_form:
48 self.fail("The form '%s' was not used to render the response" %
49 form)
50+ assertFormError = custom_assert_message(assertFormError)
51
52 def assertTemplateUsed(self, response, template_name):
53 """
54@@ -416,6 +437,7 @@ class TransactionTestCase(unittest.TestCase):
55 (u"Template '%s' was not a template used to render the response."
56 u" Actual template(s) used: %s") % (template_name,
57 u', '.join(template_names)))
58+ assertTemplateUsed = custom_assert_message(assertTemplateUsed)
59
60 def assertTemplateNotUsed(self, response, template_name):
61 """
62@@ -426,6 +448,7 @@ class TransactionTestCase(unittest.TestCase):
63 self.failIf(template_name in template_names,
64 (u"Template '%s' was used unexpectedly in rendering the"
65 u" response") % template_name)
66+ assertTemplateNotUsed = custom_assert_message(assertTemplateNotUsed)
67
68 class TestCase(TransactionTestCase):
69 """
70diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py
71index be2cb8f..f26b0e9 100644
72--- a/tests/regressiontests/test_client_regress/models.py
73+++ b/tests/regressiontests/test_client_regress/models.py
74@@ -79,6 +79,34 @@ class AssertContainsTests(TestCase):
75 self.assertNotContains(r, u'はたけ')
76 self.assertNotContains(r, '\xe3\x81\xaf\xe3\x81\x9f\xe3\x81\x91'.decode('utf-8'))
77
78+ def test_custom_message(self):
79+ "Custom message argument is used correctly."
80+ url = '/test_client_regress/no_template_view/'
81+ response = self.client.get(url)
82+ m = 'custom message'
83+
84+ try:
85+ self.assertContains(response, 'never', msg=m)
86+ except AssertionError, e:
87+ self.assertEquals(str(e), m)
88+ try:
89+ self.assertContains(response, 'never', 1, msg=m)
90+ except AssertionError, e:
91+ self.assertEquals(str(e), m)
92+ try:
93+ self.assertContains(response, 'text', status_code=999, msg=m)
94+ except AssertionError, e:
95+ self.assertEquals(str(e), m)
96+
97+ try:
98+ self.assertNotContains(response, 'once', msg=m)
99+ except AssertionError, e:
100+ self.assertEquals(str(e), m)
101+ try:
102+ self.assertNotContains(response, 'once', status_code=999, msg=m)
103+ except AssertionError, e:
104+ self.assertEquals(str(e), m)
105+
106 class AssertTemplateUsedTests(TestCase):
107 fixtures = ['testdata.json']
108
Back to Top