Ticket #18417: 18417-2.diff

File 18417-2.diff, 6.0 KB (added by Claude Paroz, 12 years ago)

Raise if decorated class is not subclass of SimpleTestCase

  • django/test/testcases.py

    diff --git a/django/test/testcases.py b/django/test/testcases.py
    index 1239275..f4c1569 100644
    a b class _AssertTemplateNotUsedContext(_AssertTemplateUsedContext):  
    241241
    242242
    243243class 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
    244278    def save_warnings_state(self):
    245279        """
    246280        Saves the state of the warnings module
    class TransactionTestCase(SimpleTestCase):  
    412446              ROOT_URLCONF with it.
    413447            * Clearing the mail test outbox.
    414448        """
     449        self.client = self.client_class()
    415450        self._fixture_setup()
    416451        self._urlconf_setup()
    417452        mail.outbox = []
    class TransactionTestCase(SimpleTestCase):  
    453488            settings.ROOT_URLCONF = self.urls
    454489            clear_url_caches()
    455490
    456     def __call__(self, result=None):
    457         """
    458         Wrapper around default __call__ method to perform common Django test
    459         set up. This means that user-defined Test Cases aren't required to
    460         include a call to super().setUp().
    461         """
    462         testMethod = getattr(self, self._testMethodName)
    463         skipped = (getattr(self.__class__, "__unittest_skip__", False) or
    464             getattr(testMethod, "__unittest_skip__", False))
    465 
    466         if not skipped:
    467             self.client = self.client_class()
    468             try:
    469                 self._pre_setup()
    470             except (KeyboardInterrupt, SystemExit):
    471                 raise
    472             except Exception:
    473                 result.addError(self, sys.exc_info())
    474                 return
    475         super(TransactionTestCase, self).__call__(result)
    476         if not skipped:
    477             try:
    478                 self._post_teardown()
    479             except (KeyboardInterrupt, SystemExit):
    480                 raise
    481             except Exception:
    482                 result.addError(self, sys.exc_info())
    483                 return
    484 
    485491    def _post_teardown(self):
    486492        """ Performs any post-test things. This includes:
    487493
  • django/test/utils.py

    diff --git a/django/test/utils.py b/django/test/utils.py
    index f10d388..8114ae0 100644
    a b class override_settings(object):  
    187187        self.disable()
    188188
    189189    def __call__(self, test_func):
    190         from django.test import TransactionTestCase
    191         if isinstance(test_func, type) and issubclass(test_func, TransactionTestCase):
     190        from django.test import SimpleTestCase
     191        if isinstance(test_func, type):
     192            if not issubclass(test_func, SimpleTestCase):
     193                raise Exception(
     194                    "Only subclasses of Django SimpleTestCase can be decorated "
     195                    "with override_settings")
    192196            original_pre_setup = test_func._pre_setup
    193197            original_post_teardown = test_func._post_teardown
    194198
  • tests/regressiontests/settings_tests/tests.py

    diff --git a/tests/regressiontests/settings_tests/tests.py b/tests/regressiontests/settings_tests/tests.py
    index aaf8bcf..68f620b 100644
    a b import warnings  
    44from django.conf import settings, global_settings
    55from django.core.exceptions import ImproperlyConfigured
    66from django.http import HttpRequest
    7 from django.test import TransactionTestCase, TestCase, signals
     7from django.test import SimpleTestCase, TransactionTestCase, TestCase, signals
    88from django.test.utils import override_settings
     9from django.utils import unittest, six
    910
    1011
    1112@override_settings(TEST='override')
    class ClassDecoratedTestCase(ClassDecoratedTestCaseSuper):  
    6768            self.fail()
    6869
    6970
    70 class SettingGetter(object):
    71     def __init__(self):
    72         self.test = getattr(settings, 'TEST', 'undefined')
    73 
    74 
    7571class SettingsTests(TestCase):
    7672    def setUp(self):
    7773        self.testvalue = None
    class SettingsTests(TestCase):  
    122118        self.assertRaises(AttributeError, getattr, settings, 'TEST')
    123119
    124120    def test_class_decorator(self):
    125         self.assertEqual(SettingGetter().test, 'undefined')
    126         DecoratedSettingGetter = override_settings(TEST='override')(SettingGetter)
    127         self.assertEqual(DecoratedSettingGetter().test, 'override')
    128         self.assertRaises(AttributeError, getattr, settings, 'TEST')
     121        # SimpleTestCase can be decorated by override_settings, but not ut.TestCase
     122        class SimpleTestCaseSubclass(SimpleTestCase):
     123            pass
     124
     125        class UnittestTestCaseSubclass(unittest.TestCase):
     126            pass
     127
     128        decorated = override_settings(TEST='override')(SimpleTestCaseSubclass)
     129        self.assertIsInstance(decorated, type)
     130        self.assertTrue(issubclass(decorated, SimpleTestCase))
     131
     132        with six.assertRaisesRegex(self, Exception,
     133                "Only subclasses of Django SimpleTestCase*"):
     134            decorated = override_settings(TEST='override')(UnittestTestCaseSubclass)
    129135
    130136    def test_signal_callback_context_manager(self):
    131137        self.assertRaises(AttributeError, getattr, settings, 'TEST')
Back to Top