Skip to content

doctest: handle BdbQuit #5630

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

Merged
merged 3 commits into from
Oct 22, 2019
Merged
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/5630.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Quitting from debuggers is now properly handled in ``doctest`` items.
4 changes: 4 additions & 0 deletions src/_pytest/doctest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" discover and run doctests in modules and test files."""
import bdb
import inspect
import platform
import sys
Expand All @@ -7,6 +8,7 @@
from contextlib import contextmanager

import pytest
from _pytest import outcomes
from _pytest._code.code import ExceptionInfo
from _pytest._code.code import ReprFileLocation
from _pytest._code.code import TerminalRepr
Expand Down Expand Up @@ -155,6 +157,8 @@ def report_failure(self, out, test, example, got):
def report_unexpected_exception(self, out, test, example, exc_info):
if isinstance(exc_info[1], Skipped):
raise exc_info[1]
if isinstance(exc_info[1], bdb.BdbQuit):
outcomes.exit("Quitting debugger")
failure = doctest.UnexpectedException(test, example, exc_info)
if self.continue_on_failure:
out.append(failure)
Expand Down
24 changes: 23 additions & 1 deletion testing/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,6 @@ def test_2():
def test_pdb_interaction_doctest(self, testdir, monkeypatch):
p1 = testdir.makepyfile(
"""
import pytest
def function_1():
'''
>>> i = 0
Expand All @@ -485,9 +484,32 @@ def function_1():

child.sendeof()
rest = child.read().decode("utf8")
assert "! _pytest.outcomes.Exit: Quitting debugger !" in rest
assert "BdbQuit" not in rest
assert "1 failed" in rest
self.flush(child)

def test_doctest_set_trace_quit(self, testdir, monkeypatch):
p1 = testdir.makepyfile(
"""
def function_1():
'''
>>> __import__('pdb').set_trace()
'''
"""
)
# NOTE: does not use pytest.set_trace, but Python's patched pdb,
# therefore "-s" is required.
child = testdir.spawn_pytest("--doctest-modules --pdb -s %s" % p1)
child.expect("Pdb")
child.sendline("q")
rest = child.read().decode("utf8")

assert "! _pytest.outcomes.Exit: Quitting debugger !" in rest
assert "= no tests ran in" in rest
assert "BdbQuit" not in rest
assert "UNEXPECTED EXCEPTION" not in rest

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