Skip to content

Commit 1b46612

Browse files
pfalconpi-anl
authored andcommitted
unittest: Implement basic addCleanup()/doCleanup().
Signed-off-by: Paul Sokolovsky <[email protected]>
1 parent 2c0b508 commit 1b46612

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

python-stdlib/unittest/unittest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ class TestCase:
4141
def __init__(self):
4242
pass
4343

44+
def addCleanup(self, func, *args, **kwargs):
45+
if not hasattr(self, "_cleanups"):
46+
self._cleanups = []
47+
self._cleanups.append((func, args, kwargs))
48+
49+
def doCleanups(self):
50+
if hasattr(self, "_cleanups"):
51+
while self._cleanups:
52+
func, args, kwargs = self._cleanups.pop()
53+
func(*args, **kwargs)
54+
4455
def subTest(self, msg=None, **params):
4556
return NullContext()
4657

@@ -271,6 +282,7 @@ def run_suite(c, test_result):
271282
continue
272283
finally:
273284
tear_down()
285+
o.doCleanups()
274286
return exceptions
275287

276288

0 commit comments

Comments
 (0)