Skip to content

Commit 98fc937

Browse files
authored
Merge pull request #5630 from blueyed/pdb-doctest-bdbquit
doctest: handle BdbQuit
2 parents 8683293 + a51bb3e commit 98fc937

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

changelog/5630.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Quitting from debuggers is now properly handled in ``doctest`` items.

src/_pytest/doctest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
""" discover and run doctests in modules and test files."""
2+
import bdb
23
import inspect
34
import platform
45
import sys
@@ -7,6 +8,7 @@
78
from contextlib import contextmanager
89

910
import pytest
11+
from _pytest import outcomes
1012
from _pytest._code.code import ExceptionInfo
1113
from _pytest._code.code import ReprFileLocation
1214
from _pytest._code.code import TerminalRepr
@@ -155,6 +157,8 @@ def report_failure(self, out, test, example, got):
155157
def report_unexpected_exception(self, out, test, example, exc_info):
156158
if isinstance(exc_info[1], Skipped):
157159
raise exc_info[1]
160+
if isinstance(exc_info[1], bdb.BdbQuit):
161+
outcomes.exit("Quitting debugger")
158162
failure = doctest.UnexpectedException(test, example, exc_info)
159163
if self.continue_on_failure:
160164
out.append(failure)

testing/test_pdb.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,6 @@ def test_2():
466466
def test_pdb_interaction_doctest(self, testdir, monkeypatch):
467467
p1 = testdir.makepyfile(
468468
"""
469-
import pytest
470469
def function_1():
471470
'''
472471
>>> i = 0
@@ -485,9 +484,32 @@ def function_1():
485484

486485
child.sendeof()
487486
rest = child.read().decode("utf8")
487+
assert "! _pytest.outcomes.Exit: Quitting debugger !" in rest
488+
assert "BdbQuit" not in rest
488489
assert "1 failed" in rest
489490
self.flush(child)
490491

492+
def test_doctest_set_trace_quit(self, testdir, monkeypatch):
493+
p1 = testdir.makepyfile(
494+
"""
495+
def function_1():
496+
'''
497+
>>> __import__('pdb').set_trace()
498+
'''
499+
"""
500+
)
501+
# NOTE: does not use pytest.set_trace, but Python's patched pdb,
502+
# therefore "-s" is required.
503+
child = testdir.spawn_pytest("--doctest-modules --pdb -s %s" % p1)
504+
child.expect("Pdb")
505+
child.sendline("q")
506+
rest = child.read().decode("utf8")
507+
508+
assert "! _pytest.outcomes.Exit: Quitting debugger !" in rest
509+
assert "= no tests ran in" in rest
510+
assert "BdbQuit" not in rest
511+
assert "UNEXPECTED EXCEPTION" not in rest
512+
491513
def test_pdb_interaction_capturing_twice(self, testdir):
492514
p1 = testdir.makepyfile(
493515
"""

0 commit comments

Comments
 (0)