Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unittest: do not use TestCase.debug with --pdb #6014

Closed
wants to merge 1 commit into from
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
1 change: 1 addition & 0 deletions changelog/5991.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do not use ``TestCase.debug()`` for unittests with ``--pdb``.
23 changes: 1 addition & 22 deletions src/_pytest/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,29 +187,8 @@ def addSuccess(self, testcase):
def stopTest(self, testcase):
pass

def _handle_skip(self):
# implements the skipping machinery (see #2137)
# analog to pythons Lib/unittest/case.py:run
testMethod = getattr(self._testcase, self._testcase._testMethodName)
if getattr(self._testcase.__class__, "__unittest_skip__", False) or getattr(
testMethod, "__unittest_skip__", False
):
# If the class or method was skipped.
skip_why = getattr(
self._testcase.__class__, "__unittest_skip_why__", ""
) or getattr(testMethod, "__unittest_skip_why__", "")
self._testcase._addSkip(self, self._testcase, skip_why)
return True
return False

def runtest(self):
if self.config.pluginmanager.get_plugin("pdbinvoke") is None:
self._testcase(result=self)
else:
# disables tearDown and cleanups for post mortem debugging (see #1890)
if self._handle_skip():
return
self._testcase.debug()
self._testcase(result=self)

def _prunetraceback(self, excinfo):
Function._prunetraceback(self, excinfo)
Expand Down
29 changes: 23 additions & 6 deletions testing/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def flush(child):
child.wait()
assert not child.isalive()

@pytest.mark.xfail(reason="running .debug() all the time is bad (#5991)")
def test_pdb_unittest_postmortem(self, testdir):
p1 = testdir.makepyfile(
"""
Expand All @@ -181,21 +182,37 @@ def test_false(self):
self.flush(child)

def test_pdb_unittest_skip(self, testdir):
"""Test for issue #2137"""
"""Test for issues #2137 and #5991"""
p1 = testdir.makepyfile(
"""
import unittest

@unittest.skipIf(True, 'Skipping also with pdb active')
class MyTestCase(unittest.TestCase):
def test_one(self):
assert 0

class MyOtherTestCase(unittest.TestCase):
def setUp(self):
print("\\nsetUp_called\\n")

def tearDown(self):
print("\\ntearDown_called\\n")

def test_two(self):
self.skipTest("skip_two")
"""
)
child = testdir.spawn_pytest("-rs --pdb %s" % p1)
child.expect("Skipping also with pdb active")
child.expect("1 skipped in")
child.sendeof()
self.flush(child)
result = testdir.runpytest("-s", "-rs", "--pdb", str(p1))
result.stdout.fnmatch_lines(
["setUp_called", "tearDown_called", "*= 2 skipped in *"]
)
result.stdout.fnmatch_lines_random(
[
"SKIPPED [1] test_pdb_unittest_skip.py:5: Skipping also with pdb active",
"SKIPPED [1] test_pdb_unittest_skip.py:15: skip_two",
]
)

def test_pdb_print_captured_stdout_and_stderr(self, testdir):
p1 = testdir.makepyfile(
Expand Down