mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Enabled SimpleTestCase to be decorated by override_settings
Refs #18417. Also fixed some test case classes which subclassed the wrong parent.
This commit is contained in:
@@ -241,6 +241,40 @@ class _AssertTemplateNotUsedContext(_AssertTemplateUsedContext):
|
||||
|
||||
|
||||
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 @@ class TransactionTestCase(SimpleTestCase):
|
||||
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 @@ class TransactionTestCase(SimpleTestCase):
|
||||
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:
|
||||
|
||||
|
||||
@@ -187,11 +187,11 @@ class override_settings(object):
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user