Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Enabled SimpleTestCase to be decorated by override_settings

Refs #18417. Also fixed some test case classes which subclassed
the wrong parent.
  • Loading branch information...
commit a5d47415f47025b014e580af1533e9023dc32945 1 parent 9f7cefd
Claude Paroz authored November 24, 2012
64  django/test/testcases.py
@@ -241,6 +241,40 @@ def message(self):
241 241
 
242 242
 
243 243
 class SimpleTestCase(ut2.TestCase):
  244
+    def __call__(self, result=None):
  245
+        """
  246
+        Wrapper around default __call__ method to perform common Django test
  247
+        set up. This means that user-defined Test Cases aren't required to
  248
+        include a call to super().setUp().
  249
+        """
  250
+        testMethod = getattr(self, self._testMethodName)
  251
+        skipped = (getattr(self.__class__, "__unittest_skip__", False) or
  252
+            getattr(testMethod, "__unittest_skip__", False))
  253
+
  254
+        if not skipped:
  255
+            try:
  256
+                self._pre_setup()
  257
+            except (KeyboardInterrupt, SystemExit):
  258
+                raise
  259
+            except Exception:
  260
+                result.addError(self, sys.exc_info())
  261
+                return
  262
+        super(SimpleTestCase, self).__call__(result)
  263
+        if not skipped:
  264
+            try:
  265
+                self._post_teardown()
  266
+            except (KeyboardInterrupt, SystemExit):
  267
+                raise
  268
+            except Exception:
  269
+                result.addError(self, sys.exc_info())
  270
+                return
  271
+
  272
+    def _pre_setup(self):
  273
+        pass
  274
+
  275
+    def _post_teardown(self):
  276
+        pass
  277
+
244 278
     def save_warnings_state(self):
245 279
         """
246 280
         Saves the state of the warnings module
@@ -412,6 +446,7 @@ def _pre_setup(self):
412 446
               ROOT_URLCONF with it.
413 447
             * Clearing the mail test outbox.
414 448
         """
  449
+        self.client = self.client_class()
415 450
         self._fixture_setup()
416 451
         self._urlconf_setup()
417 452
         mail.outbox = []
@@ -459,35 +494,6 @@ def _urlconf_setup(self):
459 494
             settings.ROOT_URLCONF = self.urls
460 495
             clear_url_caches()
461 496
 
462  
-    def __call__(self, result=None):
463  
-        """
464  
-        Wrapper around default __call__ method to perform common Django test
465  
-        set up. This means that user-defined Test Cases aren't required to
466  
-        include a call to super().setUp().
467  
-        """
468  
-        testMethod = getattr(self, self._testMethodName)
469  
-        skipped = (getattr(self.__class__, "__unittest_skip__", False) or
470  
-            getattr(testMethod, "__unittest_skip__", False))
471  
-
472  
-        if not skipped:
473  
-            self.client = self.client_class()
474  
-            try:
475  
-                self._pre_setup()
476  
-            except (KeyboardInterrupt, SystemExit):
477  
-                raise
478  
-            except Exception:
479  
-                result.addError(self, sys.exc_info())
480  
-                return
481  
-        super(TransactionTestCase, self).__call__(result)
482  
-        if not skipped:
483  
-            try:
484  
-                self._post_teardown()
485  
-            except (KeyboardInterrupt, SystemExit):
486  
-                raise
487  
-            except Exception:
488  
-                result.addError(self, sys.exc_info())
489  
-                return
490  
-
491 497
     def _post_teardown(self):
492 498
         """ Performs any post-test things. This includes:
493 499
 
6  django/test/utils.py
@@ -187,11 +187,11 @@ def __exit__(self, exc_type, exc_value, traceback):
187 187
         self.disable()
188 188
 
189 189
     def __call__(self, test_func):
190  
-        from django.test import TransactionTestCase
  190
+        from django.test import SimpleTestCase
191 191
         if isinstance(test_func, type):
192  
-            if not issubclass(test_func, TransactionTestCase):
  192
+            if not issubclass(test_func, SimpleTestCase):
193 193
                 raise Exception(
194  
-                    "Only subclasses of Django TransactionTestCase can be decorated "
  194
+                    "Only subclasses of Django SimpleTestCase can be decorated "
195 195
                     "with override_settings")
196 196
             original_pre_setup = test_func._pre_setup
197 197
             original_post_teardown = test_func._post_teardown
20  tests/regressiontests/forms/tests/input_formats.py
@@ -3,11 +3,11 @@
3 3
 from django import forms
4 4
 from django.test.utils import override_settings
5 5
 from django.utils.translation import activate, deactivate
6  
-from django.utils.unittest import TestCase
  6
+from django.test import SimpleTestCase
7 7
 
8 8
 
9 9
 @override_settings(TIME_INPUT_FORMATS=["%I:%M:%S %p", "%I:%M %p"], USE_L10N=True)
10  
-class LocalizedTimeTests(TestCase):
  10
+class LocalizedTimeTests(SimpleTestCase):
11 11
     def setUp(self):
12 12
         activate('de')
13 13
 
@@ -106,7 +106,7 @@ def test_localized_timeField_with_inputformat(self):
106 106
 
107 107
 
108 108
 @override_settings(TIME_INPUT_FORMATS=["%I:%M:%S %p", "%I:%M %p"])
109  
-class CustomTimeInputFormatsTests(TestCase):
  109
+class CustomTimeInputFormatsTests(SimpleTestCase):
110 110
     def test_timeField(self):
111 111
         "TimeFields can parse dates in the default format"
112 112
         f = forms.TimeField()
@@ -198,7 +198,7 @@ def test_localized_timeField_with_inputformat(self):
198 198
         self.assertEqual(text, "01:30:00 PM")
199 199
 
200 200
 
201  
-class SimpleTimeFormatTests(TestCase):
  201
+class SimpleTimeFormatTests(SimpleTestCase):
202 202
     def test_timeField(self):
203 203
         "TimeFields can parse dates in the default format"
204 204
         f = forms.TimeField()
@@ -289,7 +289,7 @@ def test_localized_timeField_with_inputformat(self):
289 289
 
290 290
 
291 291
 @override_settings(DATE_INPUT_FORMATS=["%d/%m/%Y", "%d-%m-%Y"], USE_L10N=True)
292  
-class LocalizedDateTests(TestCase):
  292
+class LocalizedDateTests(SimpleTestCase):
293 293
     def setUp(self):
294 294
         activate('de')
295 295
 
@@ -390,7 +390,7 @@ def test_localized_dateField_with_inputformat(self):
390 390
 
391 391
 
392 392
 @override_settings(DATE_INPUT_FORMATS=["%d.%m.%Y", "%d-%m-%Y"])
393  
-class CustomDateInputFormatsTests(TestCase):
  393
+class CustomDateInputFormatsTests(SimpleTestCase):
394 394
     def test_dateField(self):
395 395
         "DateFields can parse dates in the default format"
396 396
         f = forms.DateField()
@@ -481,7 +481,7 @@ def test_localized_dateField_with_inputformat(self):
481 481
         text = f.widget._format_value(result)
482 482
         self.assertEqual(text, "21.12.2010")
483 483
 
484  
-class SimpleDateFormatTests(TestCase):
  484
+class SimpleDateFormatTests(SimpleTestCase):
485 485
     def test_dateField(self):
486 486
         "DateFields can parse dates in the default format"
487 487
         f = forms.DateField()
@@ -572,7 +572,7 @@ def test_localized_dateField_with_inputformat(self):
572 572
 
573 573
 
574 574
 @override_settings(DATETIME_INPUT_FORMATS=["%I:%M:%S %p %d/%m/%Y", "%I:%M %p %d-%m-%Y"], USE_L10N=True)
575  
-class LocalizedDateTimeTests(TestCase):
  575
+class LocalizedDateTimeTests(SimpleTestCase):
576 576
     def setUp(self):
577 577
         activate('de')
578 578
 
@@ -673,7 +673,7 @@ def test_localized_dateTimeField_with_inputformat(self):
673 673
 
674 674
 
675 675
 @override_settings(DATETIME_INPUT_FORMATS=["%I:%M:%S %p %d/%m/%Y", "%I:%M %p %d-%m-%Y"])
676  
-class CustomDateTimeInputFormatsTests(TestCase):
  676
+class CustomDateTimeInputFormatsTests(SimpleTestCase):
677 677
     def test_dateTimeField(self):
678 678
         "DateTimeFields can parse dates in the default format"
679 679
         f = forms.DateTimeField()
@@ -764,7 +764,7 @@ def test_localized_dateTimeField_with_inputformat(self):
764 764
         text = f.widget._format_value(result)
765 765
         self.assertEqual(text, "01:30:00 PM 21/12/2010")
766 766
 
767  
-class SimpleDateTimeFormatTests(TestCase):
  767
+class SimpleDateTimeFormatTests(SimpleTestCase):
768 768
     def test_dateTimeField(self):
769 769
         "DateTimeFields can parse dates in the default format"
770 770
         f = forms.DateTimeField()
12  tests/regressiontests/settings_tests/tests.py
@@ -4,7 +4,7 @@
4 4
 from django.conf import settings, global_settings
5 5
 from django.core.exceptions import ImproperlyConfigured
6 6
 from django.http import HttpRequest
7  
-from django.test import TransactionTestCase, TestCase, signals
  7
+from django.test import SimpleTestCase, TransactionTestCase, TestCase, signals
8 8
 from django.test.utils import override_settings
9 9
 from django.utils import unittest, six
10 10
 
@@ -118,19 +118,19 @@ def test_context_manager(self):
118 118
         self.assertRaises(AttributeError, getattr, settings, 'TEST')
119 119
 
120 120
     def test_class_decorator(self):
121  
-        # TransactionTestCase can be decorated by override_settings, but not ut.TestCase
122  
-        class TransactionTestCaseSubclass(TransactionTestCase):
  121
+        # SimpleTestCase can be decorated by override_settings, but not ut.TestCase
  122
+        class SimpleTestCaseSubclass(SimpleTestCase):
123 123
             pass
124 124
 
125 125
         class UnittestTestCaseSubclass(unittest.TestCase):
126 126
             pass
127 127
 
128  
-        decorated = override_settings(TEST='override')(TransactionTestCaseSubclass)
  128
+        decorated = override_settings(TEST='override')(SimpleTestCaseSubclass)
129 129
         self.assertIsInstance(decorated, type)
130  
-        self.assertTrue(issubclass(decorated, TransactionTestCase))
  130
+        self.assertTrue(issubclass(decorated, SimpleTestCase))
131 131
 
132 132
         with six.assertRaisesRegex(self, Exception,
133  
-                "Only subclasses of Django TransactionTestCase*"):
  133
+                "Only subclasses of Django SimpleTestCase*"):
134 134
             decorated = override_settings(TEST='override')(UnittestTestCaseSubclass)
135 135
 
136 136
     def test_signal_callback_context_manager(self):

0 notes on commit a5d4741

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