From 0f767f9a993f5834f7a2004bf4c440a126a73d06 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Tue, 23 Aug 2011 00:32:35 +0000 Subject: [PATCH] Fixed #16672 -- Ensure that test classes decorated with @override_setings gets the right name when running the tests. Thanks to Julien Phalip for the report and initial patch git-svn-id: http://code.djangoproject.com/svn/django/trunk@16650 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/test/utils.py | 23 +++++++++++++------ tests/regressiontests/settings_tests/tests.py | 3 +++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/django/test/utils.py b/django/test/utils.py index 88da9d54c5..9673a26b98 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -196,13 +196,22 @@ class override_settings(object): def __call__(self, test_func): from django.test import TransactionTestCase if isinstance(test_func, type) and issubclass(test_func, TransactionTestCase): - class inner(test_func): - def _pre_setup(innerself): - self.enable() - super(inner, innerself)._pre_setup() - def _post_teardown(innerself): - super(inner, innerself)._post_teardown() - self.disable() + # When decorating a class, we need to construct a new class + # with the same name so that the test discovery tools can + # get a useful name. + def _pre_setup(innerself): + self.enable() + test_func._pre_setup(innerself) + def _post_teardown(innerself): + test_func._post_teardown(innerself) + self.disable() + inner = type( + test_func.__name__, + (test_func,), + { + '_pre_setup': _pre_setup, + '_post_teardown': _post_teardown, + }) else: @wraps(test_func) def inner(*args, **kwargs): diff --git a/tests/regressiontests/settings_tests/tests.py b/tests/regressiontests/settings_tests/tests.py index 9715046bd4..6af2ed307b 100644 --- a/tests/regressiontests/settings_tests/tests.py +++ b/tests/regressiontests/settings_tests/tests.py @@ -15,6 +15,9 @@ class FullyDecoratedTranTestCase(TransactionTestCase): def test_method_override(self): self.assertEqual(settings.TEST, 'override2') + def test_decorated_testcase_name(self): + self.assertEquals(FullyDecoratedTranTestCase.__name__, 'FullyDecoratedTranTestCase') + FullyDecoratedTranTestCase = override_settings(TEST='override')(FullyDecoratedTranTestCase) # @override_settings(TEST='override')