Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
@claudep claudep authored
View
64 django/test/testcases.py
@@ -241,6 +241,40 @@ def message(self):
class SimpleTestCase(ut2.TestCase):
+ def __call__(self, result=None):
+ """
+ Wrapper around default __call__ method to perform common Django test
+ set up. This means that user-defined Test Cases aren't required to
+ include a call to super().setUp().
+ """
+ testMethod = getattr(self, self._testMethodName)
+ skipped = (getattr(self.__class__, "__unittest_skip__", False) or
+ getattr(testMethod, "__unittest_skip__", False))
+
+ if not skipped:
+ try:
+ self._pre_setup()
+ except (KeyboardInterrupt, SystemExit):
+ raise
+ except Exception:
+ result.addError(self, sys.exc_info())
+ return
+ super(SimpleTestCase, self).__call__(result)
+ if not skipped:
+ try:
+ self._post_teardown()
+ except (KeyboardInterrupt, SystemExit):
+ raise
+ except Exception:
+ result.addError(self, sys.exc_info())
+ return
+
+ def _pre_setup(self):
+ pass
+
+ def _post_teardown(self):
+ pass
+
def save_warnings_state(self):
"""
Saves the state of the warnings module
@@ -412,6 +446,7 @@ def _pre_setup(self):
ROOT_URLCONF with it.
* Clearing the mail test outbox.
"""
+ self.client = self.client_class()
self._fixture_setup()
self._urlconf_setup()
mail.outbox = []
@@ -459,35 +494,6 @@ def _urlconf_setup(self):
settings.ROOT_URLCONF = self.urls
clear_url_caches()
- def __call__(self, result=None):
- """
- Wrapper around default __call__ method to perform common Django test
- set up. This means that user-defined Test Cases aren't required to
- include a call to super().setUp().
- """
- testMethod = getattr(self, self._testMethodName)
- skipped = (getattr(self.__class__, "__unittest_skip__", False) or
- getattr(testMethod, "__unittest_skip__", False))
-
- if not skipped:
- self.client = self.client_class()
- try:
- self._pre_setup()
- except (KeyboardInterrupt, SystemExit):
- raise
- except Exception:
- result.addError(self, sys.exc_info())
- return
- super(TransactionTestCase, self).__call__(result)
- if not skipped:
- try:
- self._post_teardown()
- except (KeyboardInterrupt, SystemExit):
- raise
- except Exception:
- result.addError(self, sys.exc_info())
- return
-
def _post_teardown(self):
""" Performs any post-test things. This includes:
View
6 django/test/utils.py
@@ -187,11 +187,11 @@ def __exit__(self, exc_type, exc_value, traceback):
self.disable()
def __call__(self, test_func):
- from django.test import TransactionTestCase
+ from django.test import SimpleTestCase
if isinstance(test_func, type):
- if not issubclass(test_func, TransactionTestCase):
+ if not issubclass(test_func, SimpleTestCase):
raise Exception(
- "Only subclasses of Django TransactionTestCase can be decorated "
+ "Only subclasses of Django SimpleTestCase can be decorated "
"with override_settings")
original_pre_setup = test_func._pre_setup
original_post_teardown = test_func._post_teardown
View
20 tests/regressiontests/forms/tests/input_formats.py
@@ -3,11 +3,11 @@
from django import forms
from django.test.utils import override_settings
from django.utils.translation import activate, deactivate
-from django.utils.unittest import TestCase
+from django.test import SimpleTestCase
@override_settings(TIME_INPUT_FORMATS=["%I:%M:%S %p", "%I:%M %p"], USE_L10N=True)
-class LocalizedTimeTests(TestCase):
+class LocalizedTimeTests(SimpleTestCase):
def setUp(self):
activate('de')
@@ -106,7 +106,7 @@ def test_localized_timeField_with_inputformat(self):
@override_settings(TIME_INPUT_FORMATS=["%I:%M:%S %p", "%I:%M %p"])
-class CustomTimeInputFormatsTests(TestCase):
+class CustomTimeInputFormatsTests(SimpleTestCase):
def test_timeField(self):
"TimeFields can parse dates in the default format"
f = forms.TimeField()
@@ -198,7 +198,7 @@ def test_localized_timeField_with_inputformat(self):
self.assertEqual(text, "01:30:00 PM")
-class SimpleTimeFormatTests(TestCase):
+class SimpleTimeFormatTests(SimpleTestCase):
def test_timeField(self):
"TimeFields can parse dates in the default format"
f = forms.TimeField()
@@ -289,7 +289,7 @@ def test_localized_timeField_with_inputformat(self):
@override_settings(DATE_INPUT_FORMATS=["%d/%m/%Y", "%d-%m-%Y"], USE_L10N=True)
-class LocalizedDateTests(TestCase):
+class LocalizedDateTests(SimpleTestCase):
def setUp(self):
activate('de')
@@ -390,7 +390,7 @@ def test_localized_dateField_with_inputformat(self):
@override_settings(DATE_INPUT_FORMATS=["%d.%m.%Y", "%d-%m-%Y"])
-class CustomDateInputFormatsTests(TestCase):
+class CustomDateInputFormatsTests(SimpleTestCase):
def test_dateField(self):
"DateFields can parse dates in the default format"
f = forms.DateField()
@@ -481,7 +481,7 @@ def test_localized_dateField_with_inputformat(self):
text = f.widget._format_value(result)
self.assertEqual(text, "21.12.2010")
-class SimpleDateFormatTests(TestCase):
+class SimpleDateFormatTests(SimpleTestCase):
def test_dateField(self):
"DateFields can parse dates in the default format"
f = forms.DateField()
@@ -572,7 +572,7 @@ def test_localized_dateField_with_inputformat(self):
@override_settings(DATETIME_INPUT_FORMATS=["%I:%M:%S %p %d/%m/%Y", "%I:%M %p %d-%m-%Y"], USE_L10N=True)
-class LocalizedDateTimeTests(TestCase):
+class LocalizedDateTimeTests(SimpleTestCase):
def setUp(self):
activate('de')
@@ -673,7 +673,7 @@ def test_localized_dateTimeField_with_inputformat(self):
@override_settings(DATETIME_INPUT_FORMATS=["%I:%M:%S %p %d/%m/%Y", "%I:%M %p %d-%m-%Y"])
-class CustomDateTimeInputFormatsTests(TestCase):
+class CustomDateTimeInputFormatsTests(SimpleTestCase):
def test_dateTimeField(self):
"DateTimeFields can parse dates in the default format"
f = forms.DateTimeField()
@@ -764,7 +764,7 @@ def test_localized_dateTimeField_with_inputformat(self):
text = f.widget._format_value(result)
self.assertEqual(text, "01:30:00 PM 21/12/2010")
-class SimpleDateTimeFormatTests(TestCase):
+class SimpleDateTimeFormatTests(SimpleTestCase):
def test_dateTimeField(self):
"DateTimeFields can parse dates in the default format"
f = forms.DateTimeField()
View
12 tests/regressiontests/settings_tests/tests.py
@@ -4,7 +4,7 @@
from django.conf import settings, global_settings
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpRequest
-from django.test import TransactionTestCase, TestCase, signals
+from django.test import SimpleTestCase, TransactionTestCase, TestCase, signals
from django.test.utils import override_settings
from django.utils import unittest, six
@@ -118,19 +118,19 @@ def test_context_manager(self):
self.assertRaises(AttributeError, getattr, settings, 'TEST')
def test_class_decorator(self):
- # TransactionTestCase can be decorated by override_settings, but not ut.TestCase
- class TransactionTestCaseSubclass(TransactionTestCase):
+ # SimpleTestCase can be decorated by override_settings, but not ut.TestCase
+ class SimpleTestCaseSubclass(SimpleTestCase):
pass
class UnittestTestCaseSubclass(unittest.TestCase):
pass
- decorated = override_settings(TEST='override')(TransactionTestCaseSubclass)
+ decorated = override_settings(TEST='override')(SimpleTestCaseSubclass)
self.assertIsInstance(decorated, type)
- self.assertTrue(issubclass(decorated, TransactionTestCase))
+ self.assertTrue(issubclass(decorated, SimpleTestCase))
with six.assertRaisesRegex(self, Exception,
- "Only subclasses of Django TransactionTestCase*"):
+ "Only subclasses of Django SimpleTestCase*"):
decorated = override_settings(TEST='override')(UnittestTestCaseSubclass)
def test_signal_callback_context_manager(self):
Please sign in to comment.
Something went wrong with that request. Please try again.