mirror of
https://github.com/django/django.git
synced 2025-08-03 18:38:50 +00:00
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:
parent
11ebc6479f
commit
57dadfac3c
3 changed files with 30 additions and 11 deletions
|
@ -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'])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue