Refs -- Improved cleaning up and fixed isolation of staticfiles_tests tests.
@@ -29,14 +29,23 @@ class LiveServerBase(StaticLiveServerTestCase):
# Override settings
cls.settings_override = override_settings(**TEST_SETTINGS)
cls.settings_override.enable()
+ try:
super().setUpClass()
+ except Exception:
+ # Clean up since tearDownClass() isn't called on errors.
+ cls._tearDownLiveServerBase()
+ raise
@classmethod
- def tearDownClass(cls):
- super().tearDownClass()
+ def _tearDownLiveServerBase(cls):
# Restore original settings
cls.settings_override.disable()
+ @classmethod
+ def tearDownClass(cls):
+ super().tearDownClass()
+ cls._tearDownLiveServerBase()
+
class StaticLiveServerChecks(LiveServerBase):
@@ -46,7 +55,9 @@ class StaticLiveServerChecks(LiveServerBase):
# should bubble up to the main thread.
old_STATIC_URL = TEST_SETTINGS['STATIC_URL']
TEST_SETTINGS['STATIC_URL'] = None
+ try:
cls.raises_exception()
+ finally:
TEST_SETTINGS['STATIC_URL'] = old_STATIC_URL
@classmethod
@@ -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
