Refs #32417 -- Improved cleaning up and fixed isolation of staticfiles_tests tests.

This commit is contained in:
Chris Jerdonek 2021-02-15 10:15:06 +01:00 committed by Mariusz Felisiak
parent 3fa1ed53be
commit fc0069bfa5
1 changed files with 19 additions and 10 deletions

View File

@ -29,13 +29,22 @@ class LiveServerBase(StaticLiveServerTestCase):
# Override settings # Override settings
cls.settings_override = override_settings(**TEST_SETTINGS) cls.settings_override = override_settings(**TEST_SETTINGS)
cls.settings_override.enable() cls.settings_override.enable()
try:
super().setUpClass() 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 @classmethod
def tearDownClass(cls): def tearDownClass(cls):
super().tearDownClass() super().tearDownClass()
# Restore original settings cls._tearDownLiveServerBase()
cls.settings_override.disable()
class StaticLiveServerChecks(LiveServerBase): class StaticLiveServerChecks(LiveServerBase):
@ -46,7 +55,9 @@ class StaticLiveServerChecks(LiveServerBase):
# should bubble up to the main thread. # should bubble up to the main thread.
old_STATIC_URL = TEST_SETTINGS['STATIC_URL'] old_STATIC_URL = TEST_SETTINGS['STATIC_URL']
TEST_SETTINGS['STATIC_URL'] = None TEST_SETTINGS['STATIC_URL'] = None
try:
cls.raises_exception() cls.raises_exception()
finally:
TEST_SETTINGS['STATIC_URL'] = old_STATIC_URL TEST_SETTINGS['STATIC_URL'] = old_STATIC_URL
@classmethod @classmethod
@ -58,16 +69,14 @@ class StaticLiveServerChecks(LiveServerBase):
def raises_exception(cls): def raises_exception(cls):
try: try:
super().setUpClass() super().setUpClass()
raise Exception("The line above should have raised an exception")
except ImproperlyConfigured: except ImproperlyConfigured:
# This raises ImproperlyConfigured("You're using the staticfiles # This raises ImproperlyConfigured("You're using the staticfiles
# app without having set the required STATIC_URL setting.") # app without having set the required STATIC_URL setting.")
pass pass
finally: else:
# Use del to avoid decrementing the database thread sharing count a # super().setUpClass() cleans up after itself on a failure.
# second time.
del cls.server_thread
super().tearDownClass() super().tearDownClass()
raise Exception('setUpClass() should have raised an exception.')
def test_test_test(self): def test_test_test(self):
# Intentionally empty method so that the test is picked up by the # Intentionally empty method so that the test is picked up by the