Skip to content

Commit a094fb3

Browse files
authored
Merge pull request #1890 from mbyt/disable_tearDown_and_cleanups_for_post_mortem_debugging
unittest runner: avoid tearDown and cleanup to ease post mortem debugging
2 parents 67ba8aa + e43d117 commit a094fb3

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Martin Prusse
9191
Matt Bachmann
9292
Matt Williams
9393
Matthias Hafner
94+
mbyt
9495
Michael Aquilina
9596
Michael Birtwell
9697
Michael Droettboom

CHANGELOG.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,23 @@
1818
if a test suite uses ``pytest_plugins`` to load internal plugins (`#1888`_).
1919
Thanks `@jaraco`_ for the report and `@nicoddemus`_ for the PR (`#1891`_).
2020

21+
* Do not call tearDown and cleanups when running tests from
22+
``unittest.TestCase`` subclasses with ``--pdb``
23+
enabled. This allows proper post mortem debugging for all applications
24+
which have significant logic in their tearDown machinery (`#1890`_). Thanks
25+
`@mbyt`_ for the PR.
26+
2127
*
2228

2329
.. _@joguSD: https://github.com/joguSD
2430
.. _@AiOO: https://github.com/AiOO
31+
.. _@mbyt: https://github.com/mbyt
2532

2633
.. _#1857: https://github.com/pytest-dev/pytest/issues/1857
2734
.. _#1864: https://github.com/pytest-dev/pytest/issues/1864
2835
.. _#1888: https://github.com/pytest-dev/pytest/issues/1888
2936
.. _#1891: https://github.com/pytest-dev/pytest/pull/1891
37+
.. _#1890: https://github.com/pytest-dev/pytest/issues/1890
3038

3139

3240
3.0.1

_pytest/unittest.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,12 @@ def stopTest(self, testcase):
150150
pass
151151

152152
def runtest(self):
153-
self._testcase(result=self)
153+
if self.config.pluginmanager.get_plugin("pdbinvoke") is None:
154+
self._testcase(result=self)
155+
else:
156+
# disables tearDown and cleanups for post mortem debugging (see #1890)
157+
self._testcase.debug()
158+
154159

155160
def _prunetraceback(self, excinfo):
156161
pytest.Function._prunetraceback(self, excinfo)

doc/en/unittest.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ distributing tests to multiple CPUs via the ``-nNUM`` option if you
3333
installed the ``pytest-xdist`` plugin. Please refer to
3434
the general ``pytest`` documentation for many more examples.
3535

36+
.. note::
37+
38+
Running tests from ``unittest.TestCase`` subclasses with ``--pdb`` will
39+
disable tearDown and cleanup methods for the case that an Exception
40+
occurs. This allows proper post mortem debugging for all applications
41+
which have significant logic in their tearDown machinery.
42+
3643
Mixing pytest fixtures into unittest.TestCase style tests
3744
-----------------------------------------------------------
3845

testing/test_pdb.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@ def test_1():
7979
if child.isalive():
8080
child.wait()
8181

82+
def test_pdb_unittest_postmortem(self, testdir):
83+
p1 = testdir.makepyfile("""
84+
import unittest
85+
class Blub(unittest.TestCase):
86+
def tearDown(self):
87+
self.filename = None
88+
def test_false(self):
89+
self.filename = 'debug' + '.me'
90+
assert 0
91+
""")
92+
child = testdir.spawn_pytest("--pdb %s" % p1)
93+
child.expect('(Pdb)')
94+
child.sendline('p self.filename')
95+
child.sendeof()
96+
rest = child.read().decode("utf8")
97+
assert 'debug.me' in rest
98+
if child.isalive():
99+
child.wait()
100+
82101
def test_pdb_interaction_capture(self, testdir):
83102
p1 = testdir.makepyfile("""
84103
def test_1():

0 commit comments

Comments
 (0)