Skip to content

Commit 63d5176

Browse files
committed
doctest: handle BdbQuit
Map `BdbQuit` exception to `outcomes.Exit`. This is necessary since we are not wrapping `pdb.set_trace` there, and therefore our `do_quit` is not called.
1 parent faf222f commit 63d5176

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

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
@@ -458,7 +458,6 @@ def test_2():
458458
def test_pdb_interaction_doctest(self, testdir, monkeypatch):
459459
p1 = testdir.makepyfile(
460460
"""
461-
import pytest
462461
def function_1():
463462
'''
464463
>>> i = 0
@@ -477,9 +476,32 @@ def function_1():
477476

478477
child.sendeof()
479478
rest = child.read().decode("utf8")
479+
assert "! _pytest.outcomes.Exit: Quitting debugger !" in rest
480+
assert "BdbQuit" not in rest
480481
assert "1 failed" in rest
481482
self.flush(child)
482483

484+
def test_doctest_set_trace_quit(self, testdir, monkeypatch):
485+
p1 = testdir.makepyfile(
486+
"""
487+
def function_1():
488+
'''
489+
>>> __import__('pdb').set_trace()
490+
'''
491+
"""
492+
)
493+
# NOTE: does not use pytest.set_trace, but Python's patched pdb,
494+
# therefore "-s" is required.
495+
child = testdir.spawn_pytest("--doctest-modules --pdb -s %s" % p1)
496+
child.expect("Pdb")
497+
child.sendline("q")
498+
rest = child.read().decode("utf8")
499+
500+
assert "! _pytest.outcomes.Exit: Quitting debugger !" in rest
501+
assert "= no tests ran in" in rest
502+
assert "BdbQuit" not in rest
503+
assert "UNEXPECTED EXCEPTION" not in rest
504+
483505
def test_pdb_interaction_capturing_twice(self, testdir):
484506
p1 = testdir.makepyfile(
485507
"""

0 commit comments

Comments
 (0)