Skip to content

Commit c7eeb05

Browse files
committed
unittest: do not use TestCase.debug() with --pdb
Fixes pytest-dev#5991.
1 parent c9524af commit c7eeb05

File tree

4 files changed

+20
-39
lines changed

4 files changed

+20
-39
lines changed

changelog/5991.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix interaction with ``--pdb`` and unittests: do not use unittest's ``TestCase.debug()``.

doc/en/unittest.rst

-11
Original file line numberDiff line numberDiff line change
@@ -238,17 +238,6 @@ was executed ahead of the ``test_method``.
238238

239239
.. _pdb-unittest-note:
240240

241-
.. note::
242-
243-
Running tests from ``unittest.TestCase`` subclasses with ``--pdb`` will
244-
disable tearDown and cleanup methods for the case that an Exception
245-
occurs. This allows proper post mortem debugging for all applications
246-
which have significant logic in their tearDown machinery. However,
247-
supporting this feature has the following side effect: If people
248-
overwrite ``unittest.TestCase`` ``__call__`` or ``run``, they need to
249-
to overwrite ``debug`` in the same way (this is also true for standard
250-
unittest).
251-
252241
.. note::
253242

254243
Due to architectural differences between the two frameworks, setup and

src/_pytest/unittest.py

+1-22
Original file line numberDiff line numberDiff line change
@@ -187,29 +187,8 @@ def addSuccess(self, testcase):
187187
def stopTest(self, testcase):
188188
pass
189189

190-
def _handle_skip(self):
191-
# implements the skipping machinery (see #2137)
192-
# analog to pythons Lib/unittest/case.py:run
193-
testMethod = getattr(self._testcase, self._testcase._testMethodName)
194-
if getattr(self._testcase.__class__, "__unittest_skip__", False) or getattr(
195-
testMethod, "__unittest_skip__", False
196-
):
197-
# If the class or method was skipped.
198-
skip_why = getattr(
199-
self._testcase.__class__, "__unittest_skip_why__", ""
200-
) or getattr(testMethod, "__unittest_skip_why__", "")
201-
self._testcase._addSkip(self, self._testcase, skip_why)
202-
return True
203-
return False
204-
205190
def runtest(self):
206-
if self.config.pluginmanager.get_plugin("pdbinvoke") is None:
207-
self._testcase(result=self)
208-
else:
209-
# disables tearDown and cleanups for post mortem debugging (see #1890)
210-
if self._handle_skip():
211-
return
212-
self._testcase.debug()
191+
self._testcase(result=self)
213192

214193
def _prunetraceback(self, excinfo):
215194
Function._prunetraceback(self, excinfo)

testing/test_pdb.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -164,20 +164,32 @@ def test_pdb_unittest_postmortem(self, testdir):
164164
p1 = testdir.makepyfile(
165165
"""
166166
import unittest
167+
168+
teardown_called = 0
169+
167170
class Blub(unittest.TestCase):
168171
def tearDown(self):
169-
self.filename = None
170-
def test_false(self):
172+
global teardown_called
173+
teardown_called += 1
174+
175+
def test_error(self):
176+
assert teardown_called == 0
171177
self.filename = 'debug' + '.me'
172178
assert 0
179+
180+
def test_check(self):
181+
assert teardown_called == 1
173182
"""
174183
)
175-
child = testdir.spawn_pytest("--pdb %s" % p1)
184+
child = testdir.spawn_pytest(
185+
"--pdb {p1}::Blub::test_error {p1}::Blub::test_check".format(p1=p1)
186+
)
176187
child.expect("Pdb")
177-
child.sendline("p self.filename")
178-
child.sendeof()
188+
child.sendline("p 'filename=' + self.filename")
189+
child.expect("'filename=debug.me'")
190+
child.sendline("c")
179191
rest = child.read().decode("utf8")
180-
assert "debug.me" in rest
192+
assert "= 1 failed, 1 passed in" in rest
181193
self.flush(child)
182194

183195
def test_pdb_unittest_skip(self, testdir):

0 commit comments

Comments
 (0)