Fixed #16224 -- Fixed override_settings test utility to correctly work with TestCase classes.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16377 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2011-06-11 20:40:02 +00:00
parent e096d56406
commit a0791b2759
2 changed files with 28 additions and 7 deletions

View File

@ -197,11 +197,21 @@ class override_settings(object):
def __exit__(self, exc_type, exc_value, traceback): def __exit__(self, exc_type, exc_value, traceback):
self.disable() self.disable()
def __call__(self, func): def __call__(self, test_func):
@wraps(func) from django.test import TestCase
def inner(*args, **kwargs): if isinstance(test_func, type) and issubclass(test_func, TestCase):
with self: class inner(test_func):
return func(*args, **kwargs) def _pre_setup(innerself):
self.enable()
super(inner, innerself)._pre_setup()
def _post_teardown(innerself):
super(inner, innerself)._post_teardown()
self.disable()
else:
@wraps(test_func)
def inner(*args, **kwargs):
with self:
return test_func(*args, **kwargs)
return inner return inner
def enable(self): def enable(self):

View File

@ -1,11 +1,22 @@
from __future__ import with_statement from __future__ import with_statement
import os, sys import os
from django.conf import settings, global_settings from django.conf import settings, global_settings
from django.test import TestCase, signals from django.test import TestCase, signals
from django.test.utils import override_settings from django.test.utils import override_settings
from django.utils.unittest import skipIf
# @override_settings(TEST='override')
class FullyDecoratedTestCase(TestCase):
def test_override(self):
self.assertEqual(settings.TEST, 'override')
@override_settings(TEST='override2')
def test_method_override(self):
self.assertEqual(settings.TEST, 'override2')
FullyDecoratedTestCase = override_settings(TEST='override')(FullyDecoratedTestCase)
class SettingGetter(object): class SettingGetter(object):
def __init__(self): def __init__(self):
self.test = getattr(settings, 'TEST', 'undefined') self.test = getattr(settings, 'TEST', 'undefined')