Fixed #31944 -- Used addCleanup() to register TestContextDecorator cleanups.

Cleanups from addCleanup() are scheduled to happen in reverse order to
the order they are added (LIFO). Ensures each cleanup is executed from
the innermost to the outermost.
This commit is contained in:
François Freitag 2020-09-06 14:36:20 +02:00 committed by Mariusz Felisiak
parent 11ebc6479f
commit 57dadfac3c
3 changed files with 30 additions and 11 deletions

View file

@ -1477,4 +1477,27 @@ class TestContextDecoratorTests(SimpleTestCase):
self.assertFalse(mock_disable.called)
with self.assertRaisesMessage(NotImplementedError, 'reraised'):
decorated_test_class.setUp()
decorated_test_class.doCleanups()
self.assertTrue(mock_disable.called)
def test_cleanups_run_after_tearDown(self):
calls = []
class SaveCallsDecorator(TestContextDecorator):
def enable(self):
calls.append('enable')
def disable(self):
calls.append('disable')
class AddCleanupInSetUp(unittest.TestCase):
def setUp(self):
calls.append('setUp')
self.addCleanup(lambda: calls.append('cleanup'))
decorator = SaveCallsDecorator()
decorated_test_class = decorator.__call__(AddCleanupInSetUp)()
decorated_test_class.setUp()
decorated_test_class.tearDown()
decorated_test_class.doCleanups()
self.assertEqual(calls, ['enable', 'setUp', 'cleanup', 'disable'])