From 63d517645ce0ceb36ab36a778d4f33c93a2c77a8 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 19 Jul 2019 02:54:54 +0200 Subject: [PATCH 1/2] 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. --- src/_pytest/doctest.py | 4 ++++ testing/test_pdb.py | 24 +++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/_pytest/doctest.py b/src/_pytest/doctest.py index cf886f906..1bd2642ae 100644 --- a/src/_pytest/doctest.py +++ b/src/_pytest/doctest.py @@ -1,4 +1,5 @@ """ discover and run doctests in modules and test files.""" +import bdb import inspect import platform import sys @@ -7,6 +8,7 @@ import warnings 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 @@ -155,6 +157,8 @@ def _init_runner_class(): 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) diff --git a/testing/test_pdb.py b/testing/test_pdb.py index 8d327cbb3..c31b6b0b4 100644 --- a/testing/test_pdb.py +++ b/testing/test_pdb.py @@ -458,7 +458,6 @@ class TestPDB: def test_pdb_interaction_doctest(self, testdir, monkeypatch): p1 = testdir.makepyfile( """ - import pytest def function_1(): ''' >>> i = 0 @@ -477,9 +476,32 @@ class TestPDB: 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( """ From a51bb3eedb43c2951ce8ee2dd3fab849c4d6ee7c Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 22 Oct 2019 19:43:42 -0300 Subject: [PATCH 2/2] Add CHANGELOG for #5630 --- changelog/5630.improvement.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/5630.improvement.rst diff --git a/changelog/5630.improvement.rst b/changelog/5630.improvement.rst new file mode 100644 index 000000000..45d49bdae --- /dev/null +++ b/changelog/5630.improvement.rst @@ -0,0 +1 @@ +Quitting from debuggers is now properly handled in ``doctest`` items.