Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions tests/test_ensure_do_cleanups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from unittesting import DeferrableTestCase


class TestExplicitDoCleanups(DeferrableTestCase):

def test_manually_calling_do_cleanups_works(self):
messages = []

def work(message):
messages.append(message)

self.addCleanup(work, 1)
yield from self.doCleanups()

self.assertEqual(messages, [1])


cleanup_called = []


class TestImplicitDoCleanupsOnTeardown(DeferrableTestCase):
def test_a_prepare(self):
self.addCleanup(lambda: cleanup_called.append(1))

def test_b_assert(self):
self.assertEqual(cleanup_called, [1])
29 changes: 19 additions & 10 deletions unittesting/core/st3/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,12 @@ def run(self, result=None):
outcome = _Outcome()
self._outcomeForDoCleanups = outcome

deferred = self._executeTestPart(self.setUp, outcome)
if isiterable(deferred):
yield from deferred
yield from self._executeTestPart(self.setUp, outcome)
if outcome.success:
deferred = self._executeTestPart(testMethod, outcome, isTest=True)
if isiterable(deferred):
yield from deferred
deferred = self._executeTestPart(self.tearDown, outcome)
if isiterable(deferred):
yield from deferred
yield from self._executeTestPart(testMethod, outcome, isTest=True)
yield from self._executeTestPart(self.tearDown, outcome)

self.doCleanups()
yield from self.doCleanups()
if outcome.success:
result.addSuccess(self)
else:
Expand Down Expand Up @@ -110,3 +104,18 @@ def run(self, result=None):
stopTestRun = getattr(result, 'stopTestRun', None)
if stopTestRun is not None:
stopTestRun()

def doCleanups(self):
"""Execute all cleanup functions.

Normally called for you after tearDown.
"""
outcome = self._outcomeForDoCleanups or _Outcome()
while self._cleanups:
function, args, kwargs = self._cleanups.pop()
part = lambda: function(*args, **kwargs) # noqa: E731
yield from self._executeTestPart(part, outcome)

# return this for backwards compatibility
# even though we no longer us it internally
return outcome.success