From faba5b702a9c5bb9452a543100928bcb5f66ebcf Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Tue, 8 Jun 2021 11:35:05 +0200 Subject: [PATCH] Refs #32355 -- Used addClassCleanup() in SimpleTestCase and SerializeMixin. --- django/test/testcases.py | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/django/test/testcases.py b/django/test/testcases.py index ec5363b4ea..b2720c50f3 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -179,10 +179,13 @@ class SimpleTestCase(unittest.TestCase): if cls._overridden_settings: cls._cls_overridden_context = override_settings(**cls._overridden_settings) cls._cls_overridden_context.enable() + cls.addClassCleanup(cls._cls_overridden_context.disable) if cls._modified_settings: cls._cls_modified_context = modify_settings(cls._modified_settings) cls._cls_modified_context.enable() + cls.addClassCleanup(cls._cls_modified_context.disable) cls._add_databases_failures() + cls.addClassCleanup(cls._remove_databases_failures) @classmethod def _validate_databases(cls): @@ -227,17 +230,6 @@ class SimpleTestCase(unittest.TestCase): method = getattr(connection, name) setattr(connection, name, method.wrapped) - @classmethod - def tearDownClass(cls): - cls._remove_databases_failures() - if hasattr(cls, '_cls_modified_context'): - cls._cls_modified_context.disable() - delattr(cls, '_cls_modified_context') - if hasattr(cls, '_cls_overridden_context'): - cls._cls_overridden_context.disable() - delattr(cls, '_cls_overridden_context') - super().tearDownClass() - def __call__(self, result=None): """ Wrapper around default __call__ method to perform common Django test @@ -1221,14 +1213,12 @@ class TestCase(TransactionTestCase): call_command('loaddata', *cls.fixtures, **{'verbosity': 0, 'database': db_name}) except Exception: cls._rollback_atomics(cls.cls_atomics) - cls._remove_databases_failures() raise pre_attrs = cls.__dict__.copy() try: cls.setUpTestData() except Exception: cls._rollback_atomics(cls.cls_atomics) - cls._remove_databases_failures() raise for name, value in cls.__dict__.items(): if value is not pre_attrs.get(name): @@ -1634,10 +1624,6 @@ class SerializeMixin: @classmethod def setUpClass(cls): cls._lockfile = open(cls.lockfile) + cls.addClassCleanup(cls._lockfile.close) locks.lock(cls._lockfile, locks.LOCK_EX) super().setUpClass() - - @classmethod - def tearDownClass(cls): - super().tearDownClass() - cls._lockfile.close()