diff --git a/django/test/utils.py b/django/test/utils.py index 825c6b6d3b..234f831cfe 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -347,7 +347,11 @@ class TestContextDecorator: context = self.enable() if self.attr_name: setattr(inner_self, self.attr_name, context) - decorated_setUp(inner_self) + try: + decorated_setUp(inner_self) + except Exception: + self.disable() + raise def tearDown(inner_self): decorated_tearDown(inner_self) diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py index d908b52a8a..bfed8c81b2 100644 --- a/tests/test_utils/tests.py +++ b/tests/test_utils/tests.py @@ -19,8 +19,8 @@ from django.test import ( ) from django.test.html import HTMLParseError, parse_html from django.test.utils import ( - CaptureQueriesContext, isolate_apps, override_settings, - setup_test_environment, + CaptureQueriesContext, TestContextDecorator, isolate_apps, + override_settings, setup_test_environment, ) from django.urls import NoReverseMatch, reverse @@ -1221,3 +1221,28 @@ class IsolatedAppsTests(SimpleTestCase): self.assertEqual(MethodDecoration._meta.apps, method_apps) self.assertEqual(ContextManager._meta.apps, context_apps) self.assertEqual(NestedContextManager._meta.apps, nested_context_apps) + + +class DoNothingDecorator(TestContextDecorator): + def enable(self): + pass + + def disable(self): + pass + + +class TestContextDecoratorTests(SimpleTestCase): + + @mock.patch.object(DoNothingDecorator, 'disable') + def test_exception_in_setup(self, mock_disable): + """An exception is setUp() is reraised after disable() is called.""" + class ExceptionInSetUp(unittest.TestCase): + def setUp(self): + raise NotImplementedError('reraised') + + decorator = DoNothingDecorator() + decorated_test_class = decorator.__call__(ExceptionInSetUp)() + self.assertFalse(mock_disable.called) + with self.assertRaisesMessage(NotImplementedError, 'reraised'): + decorated_test_class.setUp() + self.assertTrue(mock_disable.called)