diff options
| author | Thomas Chaumeny <t.chaumeny@gmail.com> | 2014-10-18 20:03:10 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2014-11-03 14:14:39 -0500 |
| commit | d89f56dc4d03f6bf6602536b8b62602ec0d46d2f (patch) | |
| tree | ee089210ebb576d286abbe2bee51db042ee6ee48 /django | |
| parent | 8b77b64f1cf878b100d1d2001c8a73938ea3e1ed (diff) | |
Fixed #21281 -- Made override_settings act at class level when used as a TestCase decorator.
Diffstat (limited to 'django')
| -rw-r--r-- | django/test/testcases.py | 33 |
1 files changed, 21 insertions, 12 deletions
diff --git a/django/test/testcases.py b/django/test/testcases.py index 566c9395fb..9b25a0b937 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -161,6 +161,24 @@ class SimpleTestCase(unittest.TestCase): _overridden_settings = None _modified_settings = None + @classmethod + def setUpClass(cls): + if cls._overridden_settings: + cls._cls_overridden_context = override_settings(**cls._overridden_settings) + cls._cls_overridden_context.enable() + if cls._modified_settings: + cls._cls_modified_context = modify_settings(cls._modified_settings) + cls._cls_modified_context.enable() + + @classmethod + def tearDownClass(cls): + if hasattr(cls, '_cls_modified_context'): + cls._cls_modified_context.disable() + delattr(cls, '_cls_modified_context') + if hasattr(cls, '_cls_overridden_context'): + cls._cls_overridden_context.disable() + delattr(cls, '_cls_overridden_context') + def __call__(self, result=None): """ Wrapper around default __call__ method to perform common Django test @@ -192,24 +210,18 @@ class SimpleTestCase(unittest.TestCase): * If the class has a 'urls' attribute, replace ROOT_URLCONF with it. * Clearing the mail test outbox. """ - if self._overridden_settings: - self._overridden_context = override_settings(**self._overridden_settings) - self._overridden_context.enable() - if self._modified_settings: - self._modified_context = modify_settings(self._modified_settings) - self._modified_context.enable() self.client = self.client_class() self._urlconf_setup() mail.outbox = [] def _urlconf_setup(self): - set_urlconf(None) if hasattr(self, 'urls'): warnings.warn( "SimpleTestCase.urls is deprecated and will be removed in " "Django 2.0. Use @override_settings(ROOT_URLCONF=...) " "in %s instead." % self.__class__.__name__, RemovedInDjango20Warning, stacklevel=2) + set_urlconf(None) self._old_root_urlconf = settings.ROOT_URLCONF settings.ROOT_URLCONF = self.urls clear_url_caches() @@ -220,14 +232,10 @@ class SimpleTestCase(unittest.TestCase): * Putting back the original ROOT_URLCONF if it was changed. """ self._urlconf_teardown() - if self._modified_settings: - self._modified_context.disable() - if self._overridden_settings: - self._overridden_context.disable() def _urlconf_teardown(self): - set_urlconf(None) if hasattr(self, '_old_root_urlconf'): + set_urlconf(None) settings.ROOT_URLCONF = self._old_root_urlconf clear_url_caches() @@ -1169,6 +1177,7 @@ class LiveServerTestCase(TransactionTestCase): @classmethod def setUpClass(cls): + super(LiveServerTestCase, cls).setUpClass() connections_override = {} for conn in connections.all(): # If using in-memory sqlite databases, pass the connections to |
