Skip to content

Commit

Permalink
Call cleanup functions if explicit teardown is needed
Browse files Browse the repository at this point in the history
  • Loading branch information
scorphus committed Apr 8, 2020
1 parent eb00182 commit 43e17de
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -211,6 +211,7 @@ Omar Kohl
Omer Hadari
Ondřej Súkup
Oscar Benjamin
Pablo Aguiar
Patrick Hayes
Pauli Virtanen
Paweł Adamczak
Expand Down
1 change: 1 addition & 0 deletions changelog/6947.bugfix.rst
@@ -0,0 +1 @@
Call cleanup functions on test failure.
1 change: 1 addition & 0 deletions src/_pytest/unittest.py
Expand Up @@ -122,6 +122,7 @@ def setup(self):
def teardown(self):
if self._needs_explicit_tearDown:
self._testcase.tearDown()
self._testcase.doCleanups()
self._testcase = None
self._obj = None

Expand Down
18 changes: 18 additions & 0 deletions testing/test_unittest.py
Expand Up @@ -876,6 +876,24 @@ def test_notTornDown():
reprec.assertoutcome(passed=1, failed=1)


def test_call_cleanup_functions_on_test_failure(testdir):
testdir.makepyfile(
"""
import unittest
class TC(unittest.TestCase):
def setUp(self):
self.addCleanup(lambda: print("someCleanup()"))
def test_method(self):
assert False
"""
)
result = testdir.runpytest("-s")
assert result.ret == 1
result.stdout.fnmatch_lines(
["*someCleanup()*", "*test_method*", "*assert False*", "*1 failed*"]
)


def test_issue333_result_clearing(testdir):
testdir.makeconftest(
"""
Expand Down

0 comments on commit 43e17de

Please sign in to comment.