From fc0069bfa52f9d8bd62aa0f5749905d767ab42e9 Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Mon, 15 Feb 2021 10:15:06 +0100 Subject: [PATCH] Refs #32417 -- Improved cleaning up and fixed isolation of staticfiles_tests tests. --- tests/staticfiles_tests/test_liveserver.py | 29 ++++++++++++++-------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/tests/staticfiles_tests/test_liveserver.py b/tests/staticfiles_tests/test_liveserver.py index eb027f7341..970121f2da 100644 --- a/tests/staticfiles_tests/test_liveserver.py +++ b/tests/staticfiles_tests/test_liveserver.py @@ -29,13 +29,22 @@ class LiveServerBase(StaticLiveServerTestCase): # Override settings cls.settings_override = override_settings(**TEST_SETTINGS) cls.settings_override.enable() - super().setUpClass() + try: + super().setUpClass() + except Exception: + # Clean up since tearDownClass() isn't called on errors. + cls._tearDownLiveServerBase() + raise + + @classmethod + def _tearDownLiveServerBase(cls): + # Restore original settings + cls.settings_override.disable() @classmethod def tearDownClass(cls): super().tearDownClass() - # Restore original settings - cls.settings_override.disable() + cls._tearDownLiveServerBase() class StaticLiveServerChecks(LiveServerBase): @@ -46,8 +55,10 @@ class StaticLiveServerChecks(LiveServerBase): # should bubble up to the main thread. old_STATIC_URL = TEST_SETTINGS['STATIC_URL'] TEST_SETTINGS['STATIC_URL'] = None - cls.raises_exception() - TEST_SETTINGS['STATIC_URL'] = old_STATIC_URL + try: + cls.raises_exception() + finally: + TEST_SETTINGS['STATIC_URL'] = old_STATIC_URL @classmethod def tearDownClass(cls): @@ -58,16 +69,14 @@ class StaticLiveServerChecks(LiveServerBase): def raises_exception(cls): try: super().setUpClass() - raise Exception("The line above should have raised an exception") except ImproperlyConfigured: # This raises ImproperlyConfigured("You're using the staticfiles # app without having set the required STATIC_URL setting.") pass - finally: - # Use del to avoid decrementing the database thread sharing count a - # second time. - del cls.server_thread + else: + # super().setUpClass() cleans up after itself on a failure. super().tearDownClass() + raise Exception('setUpClass() should have raised an exception.') def test_test_test(self): # Intentionally empty method so that the test is picked up by the