Skip to content

Commit

Permalink
Handle quitting from pdb with --trace
Browse files Browse the repository at this point in the history
This raises ``outcomes.exit`` via ``set_quit``, and ``post_mortem``
directly already.

It merges ``test_pdb_interaction``, ``test_pdb_print_captured_stdout``,
and ``test_pdb_print_captured_stderr`` into
``test_pdb_print_captured_stdout_and_stderr`` (clarity and performance,
especially since pexpect tests are slow).
  • Loading branch information
blueyed committed Jan 14, 2019
1 parent 5dcb370 commit 144086e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 56 deletions.
4 changes: 4 additions & 0 deletions changelog/4280.bugfix.rst
@@ -0,0 +1,4 @@
Handle quitting from pdb with ``--trace`` and parametrized tests by exiting pytest.

Using ``[q]uit`` when in pdb will now exit pytest instead of running
the remaining tests.
10 changes: 7 additions & 3 deletions src/_pytest/debugging.py
Expand Up @@ -118,6 +118,10 @@ def do_continue(self, arg):

do_c = do_cont = do_continue

def set_quit(self):
super(_PdbWrapper, self).set_quit()
outcomes.exit("Quitting debugger")

def setup(self, f, tb):
"""Suspend on setup().
Expand Down Expand Up @@ -210,8 +214,7 @@ def _enter_pdb(node, excinfo, rep):
tw.sep(">", "entering PDB")
tb = _postmortem_traceback(excinfo)
rep._pdbshown = True
if post_mortem(tb):
outcomes.exit("Quitting debugger")
post_mortem(tb)
return rep


Expand Down Expand Up @@ -242,4 +245,5 @@ def get_stack(self, f, t):
p = Pdb()
p.reset()
p.interaction(None, t)
return p.quitting
if p.quitting:
outcomes.exit("Quitting debugger")
90 changes: 37 additions & 53 deletions testing/test_pdb.py
Expand Up @@ -147,29 +147,6 @@ def test_func():
assert rep.failed
assert len(pdblist) == 1

def test_pdb_interaction(self, testdir):
p1 = testdir.makepyfile(
"""
def test_1():
i = 0
assert i == 1
def test_not_called_due_to_quit():
pass
"""
)
child = testdir.spawn_pytest("--pdb %s" % p1)
child.expect(".*def test_1")
child.expect(".*i = 0")
child.expect("Pdb")
child.sendeof()
rest = child.read().decode("utf8")
assert "= 1 failed in" in rest
assert "def test_1" not in rest
assert "Exit: Quitting debugger" in rest
assert "PDB continue (IO-capturing resumed)" not in rest
self.flush(child)

@staticmethod
def flush(child):
if platform.system() == "Darwin":
Expand Down Expand Up @@ -214,40 +191,32 @@ def test_one(self):
child.sendeof()
self.flush(child)

def test_pdb_print_captured_stdout(self, testdir):
def test_pdb_print_captured_stdout_and_stderr(self, testdir):
p1 = testdir.makepyfile(
"""
def test_1():
import sys
sys.stderr.write("get\\x20rekt")
print("get\\x20rekt")
assert False
def test_not_called_due_to_quit():
pass
"""
)
child = testdir.spawn_pytest("--pdb %s" % p1)
child.expect("captured stdout")
child.expect("get rekt")
child.expect("Pdb")
child.sendeof()
rest = child.read().decode("utf8")
assert "1 failed" in rest
assert "get rekt" not in rest
self.flush(child)

def test_pdb_print_captured_stderr(self, testdir):
p1 = testdir.makepyfile(
"""
def test_1():
import sys
sys.stderr.write("get\\x20rekt")
assert False
"""
)
child = testdir.spawn_pytest("--pdb %s" % p1)
child.expect("captured stderr")
child.expect("get rekt")
child.expect("traceback")
child.expect("def test_1")
child.expect("Pdb")
child.sendeof()
rest = child.read().decode("utf8")
assert "1 failed" in rest
assert "Exit: Quitting debugger" in rest
assert "= 1 failed in" in rest
assert "def test_1" not in rest
assert "get rekt" not in rest
self.flush(child)

Expand Down Expand Up @@ -375,15 +344,17 @@ def test_1():
i = 0
print("hello17")
pytest.set_trace()
x = 3
i == 1
assert 0
"""
)
child = testdir.spawn_pytest(str(p1))
child.expect("test_1")
child.expect("x = 3")
child.expect(r"test_1\(\)")
child.expect("i == 1")
child.expect("Pdb")
child.sendeof()
child.sendline("c")
rest = child.read().decode("utf-8")
assert "AssertionError" in rest
assert "1 failed" in rest
assert "def test_1" in rest
assert "hello17" in rest # out is captured
Expand All @@ -398,13 +369,14 @@ def test_1():
print("hello17")
pytest.set_trace(header="== my_header ==")
x = 3
assert 0
"""
)
child = testdir.spawn_pytest(str(p1))
child.expect("== my_header ==")
assert "PDB set_trace" not in child.before.decode()
child.expect("Pdb")
child.sendeof()
child.sendline("c")
rest = child.read().decode("utf-8")
assert "1 failed" in rest
assert "def test_1" in rest
Expand All @@ -424,9 +396,9 @@ def test_1():
child.expect("Pdb")
child.sendeof()
rest = child.read().decode("utf8")
assert "1 failed" in rest
assert "no tests ran" in rest
assert "reading from stdin while output" not in rest
assert "BdbQuit" in rest
assert "BdbQuit" not in rest
self.flush(child)

def test_pdb_and_capsys(self, testdir):
Expand Down Expand Up @@ -518,6 +490,7 @@ def test_1():
print("hello18")
pytest.set_trace()
x = 4
assert 0
"""
)
child = testdir.spawn_pytest(str(p1))
Expand All @@ -530,11 +503,11 @@ def test_1():
child.expect(r"PDB set_trace \(IO-capturing turned off\)")
child.expect("x = 4")
child.expect("Pdb")
child.sendeof()
child.sendline("c")
child.expect("_ test_1 _")
child.expect("def test_1")
child.expect("Captured stdout call")
rest = child.read().decode("utf8")
assert "Captured stdout call" in rest
assert "hello17" in rest # out is captured
assert "hello18" in rest # out is captured
assert "1 failed" in rest
Expand Down Expand Up @@ -826,15 +799,26 @@ def test_trace_sets_breakpoint(self, testdir):
"""
def test_1():
assert True
def test_2():
pass
def test_3():
pass
"""
)
child = testdir.spawn_pytest("--trace " + str(p1))
child.expect("test_1")
child.expect("Pdb")
child.sendeof()
child.sendline("c")
child.expect("test_2")
child.expect("Pdb")
child.sendline("q")
child.expect_exact("Exit: Quitting debugger")
rest = child.read().decode("utf8")
assert "1 passed" in rest
assert "1 passed in" in rest
assert "reading from stdin while output" not in rest
assert "Exit: Quitting debugger" in child.before.decode("utf8")
TestPDB.flush(child)


Expand Down

0 comments on commit 144086e

Please sign in to comment.