From 6945f60c2b2406ba06d889020d50aaba80e7d317 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 24 Nov 2012 23:47:41 +0100 Subject: [PATCH] [1.5.x] Enabled SimpleTestCase to be decorated by override_settings Refs #18417. Also fixed some test case classes which subclassed the wrong parent. Backport of a5d47415f from master. --- django/test/testcases.py | 64 ++++++++++--------- django/test/utils.py | 6 +- .../forms/tests/input_formats.py | 20 +++--- tests/regressiontests/settings_tests/tests.py | 12 ++-- 4 files changed, 54 insertions(+), 48 deletions(-) diff --git a/django/test/testcases.py b/django/test/testcases.py index 6b4885f93f..f673a10d08 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -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: diff --git a/django/test/utils.py b/django/test/utils.py index 99ef6b9fa2..8114ae0e6a 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -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 diff --git a/tests/regressiontests/forms/tests/input_formats.py b/tests/regressiontests/forms/tests/input_formats.py index d591804b9d..d34125fa29 100644 --- a/tests/regressiontests/forms/tests/input_formats.py +++ b/tests/regressiontests/forms/tests/input_formats.py @@ -3,11 +3,11 @@ from datetime import time, date, datetime from django import forms from django.test.utils import override_settings from django.utils.translation import activate, deactivate -from django.utils.unittest import TestCase +from django.test import SimpleTestCase @override_settings(TIME_INPUT_FORMATS=["%I:%M:%S %p", "%I:%M %p"], USE_L10N=True) -class LocalizedTimeTests(TestCase): +class LocalizedTimeTests(SimpleTestCase): def setUp(self): activate('de') @@ -106,7 +106,7 @@ class LocalizedTimeTests(TestCase): @override_settings(TIME_INPUT_FORMATS=["%I:%M:%S %p", "%I:%M %p"]) -class CustomTimeInputFormatsTests(TestCase): +class CustomTimeInputFormatsTests(SimpleTestCase): def test_timeField(self): "TimeFields can parse dates in the default format" f = forms.TimeField() @@ -198,7 +198,7 @@ class CustomTimeInputFormatsTests(TestCase): self.assertEqual(text, "01:30:00 PM") -class SimpleTimeFormatTests(TestCase): +class SimpleTimeFormatTests(SimpleTestCase): def test_timeField(self): "TimeFields can parse dates in the default format" f = forms.TimeField() @@ -289,7 +289,7 @@ class SimpleTimeFormatTests(TestCase): @override_settings(DATE_INPUT_FORMATS=["%d/%m/%Y", "%d-%m-%Y"], USE_L10N=True) -class LocalizedDateTests(TestCase): +class LocalizedDateTests(SimpleTestCase): def setUp(self): activate('de') @@ -390,7 +390,7 @@ class LocalizedDateTests(TestCase): @override_settings(DATE_INPUT_FORMATS=["%d.%m.%Y", "%d-%m-%Y"]) -class CustomDateInputFormatsTests(TestCase): +class CustomDateInputFormatsTests(SimpleTestCase): def test_dateField(self): "DateFields can parse dates in the default format" f = forms.DateField() @@ -481,7 +481,7 @@ class CustomDateInputFormatsTests(TestCase): text = f.widget._format_value(result) self.assertEqual(text, "21.12.2010") -class SimpleDateFormatTests(TestCase): +class SimpleDateFormatTests(SimpleTestCase): def test_dateField(self): "DateFields can parse dates in the default format" f = forms.DateField() @@ -572,7 +572,7 @@ class SimpleDateFormatTests(TestCase): @override_settings(DATETIME_INPUT_FORMATS=["%I:%M:%S %p %d/%m/%Y", "%I:%M %p %d-%m-%Y"], USE_L10N=True) -class LocalizedDateTimeTests(TestCase): +class LocalizedDateTimeTests(SimpleTestCase): def setUp(self): activate('de') @@ -673,7 +673,7 @@ class LocalizedDateTimeTests(TestCase): @override_settings(DATETIME_INPUT_FORMATS=["%I:%M:%S %p %d/%m/%Y", "%I:%M %p %d-%m-%Y"]) -class CustomDateTimeInputFormatsTests(TestCase): +class CustomDateTimeInputFormatsTests(SimpleTestCase): def test_dateTimeField(self): "DateTimeFields can parse dates in the default format" f = forms.DateTimeField() @@ -764,7 +764,7 @@ class CustomDateTimeInputFormatsTests(TestCase): text = f.widget._format_value(result) self.assertEqual(text, "01:30:00 PM 21/12/2010") -class SimpleDateTimeFormatTests(TestCase): +class SimpleDateTimeFormatTests(SimpleTestCase): def test_dateTimeField(self): "DateTimeFields can parse dates in the default format" f = forms.DateTimeField() diff --git a/tests/regressiontests/settings_tests/tests.py b/tests/regressiontests/settings_tests/tests.py index 45190daa23..68f620b5c9 100644 --- a/tests/regressiontests/settings_tests/tests.py +++ b/tests/regressiontests/settings_tests/tests.py @@ -4,7 +4,7 @@ import warnings from django.conf import settings, global_settings from django.core.exceptions import ImproperlyConfigured from django.http import HttpRequest -from django.test import TransactionTestCase, TestCase, signals +from django.test import SimpleTestCase, TransactionTestCase, TestCase, signals from django.test.utils import override_settings from django.utils import unittest, six @@ -118,19 +118,19 @@ class SettingsTests(TestCase): self.assertRaises(AttributeError, getattr, settings, 'TEST') def test_class_decorator(self): - # TransactionTestCase can be decorated by override_settings, but not ut.TestCase - class TransactionTestCaseSubclass(TransactionTestCase): + # SimpleTestCase can be decorated by override_settings, but not ut.TestCase + class SimpleTestCaseSubclass(SimpleTestCase): pass class UnittestTestCaseSubclass(unittest.TestCase): pass - decorated = override_settings(TEST='override')(TransactionTestCaseSubclass) + decorated = override_settings(TEST='override')(SimpleTestCaseSubclass) self.assertIsInstance(decorated, type) - self.assertTrue(issubclass(decorated, TransactionTestCase)) + self.assertTrue(issubclass(decorated, SimpleTestCase)) with six.assertRaisesRegex(self, Exception, - "Only subclasses of Django TransactionTestCase*"): + "Only subclasses of Django SimpleTestCase*"): decorated = override_settings(TEST='override')(UnittestTestCaseSubclass) def test_signal_callback_context_manager(self):