Merge pull request #5630 from blueyed/pdb-doctest-bdbquit

doctest: handle BdbQuit
This commit is contained in:
Daniel Hahler 2019-10-23 01:10:19 +02:00 committed by GitHub
commit 98fc9377d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 1 deletions

View File

@ -0,0 +1 @@
Quitting from debuggers is now properly handled in ``doctest`` items.

View File

@ -1,4 +1,5 @@
""" discover and run doctests in modules and test files.""" """ discover and run doctests in modules and test files."""
import bdb
import inspect import inspect
import platform import platform
import sys import sys
@ -7,6 +8,7 @@ import warnings
from contextlib import contextmanager from contextlib import contextmanager
import pytest import pytest
from _pytest import outcomes
from _pytest._code.code import ExceptionInfo from _pytest._code.code import ExceptionInfo
from _pytest._code.code import ReprFileLocation from _pytest._code.code import ReprFileLocation
from _pytest._code.code import TerminalRepr from _pytest._code.code import TerminalRepr
@ -155,6 +157,8 @@ def _init_runner_class():
def report_unexpected_exception(self, out, test, example, exc_info): def report_unexpected_exception(self, out, test, example, exc_info):
if isinstance(exc_info[1], Skipped): if isinstance(exc_info[1], Skipped):
raise exc_info[1] raise exc_info[1]
if isinstance(exc_info[1], bdb.BdbQuit):
outcomes.exit("Quitting debugger")
failure = doctest.UnexpectedException(test, example, exc_info) failure = doctest.UnexpectedException(test, example, exc_info)
if self.continue_on_failure: if self.continue_on_failure:
out.append(failure) out.append(failure)

View File

@ -466,7 +466,6 @@ class TestPDB:
def test_pdb_interaction_doctest(self, testdir, monkeypatch): def test_pdb_interaction_doctest(self, testdir, monkeypatch):
p1 = testdir.makepyfile( p1 = testdir.makepyfile(
""" """
import pytest
def function_1(): def function_1():
''' '''
>>> i = 0 >>> i = 0
@ -485,9 +484,32 @@ class TestPDB:
child.sendeof() child.sendeof()
rest = child.read().decode("utf8") rest = child.read().decode("utf8")
assert "! _pytest.outcomes.Exit: Quitting debugger !" in rest
assert "BdbQuit" not in rest
assert "1 failed" in rest assert "1 failed" in rest
self.flush(child) 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): def test_pdb_interaction_capturing_twice(self, testdir):
p1 = testdir.makepyfile( p1 = testdir.makepyfile(
""" """