Skip to content

Commit d532099

Browse files
committed
test_pdb_continue_with_recursive_debug: mock pdb.set_trace
1 parent 951213e commit d532099

File tree

1 file changed

+58
-17
lines changed

1 file changed

+58
-17
lines changed

testing/test_pdb.py

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -604,52 +604,93 @@ def test_1():
604604
child.expect("1 passed")
605605
self.flush(child)
606606

607-
@pytest.mark.parametrize("capture", (True, False))
608-
def test_pdb_continue_with_recursive_debug(self, capture, testdir):
607+
@pytest.mark.parametrize("capture_arg", ("", "-s", "-p no:capture"))
608+
def test_pdb_continue_with_recursive_debug(self, capture_arg, testdir):
609609
"""Full coverage for do_debug without capturing.
610610
611-
This is very similar to test_pdb_interaction_continue_recursive, but
612-
simpler, and providing more coverage.
611+
This is very similar to test_pdb_interaction_continue_recursive in general,
612+
but mocks out ``pdb.set_trace`` for providing more coverage.
613613
"""
614614
p1 = testdir.makepyfile(
615615
"""
616+
try:
617+
input = raw_input
618+
except NameError:
619+
pass
620+
616621
def set_trace():
617622
__import__('pdb').set_trace()
618623
619-
def test_1():
624+
def test_1(monkeypatch):
625+
import _pytest.debugging
626+
627+
class pytestPDBTest(_pytest.debugging.pytestPDB):
628+
@classmethod
629+
def set_trace(cls, *args, **kwargs):
630+
# Init _PdbWrapper to handle capturing.
631+
_pdb = cls._init_pdb(*args, **kwargs)
632+
633+
# Mock out pdb.Pdb.do_continue.
634+
import pdb
635+
pdb.Pdb.do_continue = lambda self, arg: None
636+
637+
print("=== SET_TRACE ===")
638+
assert input() == "debug set_trace()"
639+
640+
# Simulate _PdbWrapper.do_debug
641+
cls._recursive_debug += 1
642+
print("ENTERING RECURSIVE DEBUGGER")
643+
print("=== SET_TRACE_2 ===")
644+
645+
assert input() == "c"
646+
_pdb.do_continue("")
647+
print("=== SET_TRACE_3 ===")
648+
649+
# Simulate _PdbWrapper.do_debug
650+
print("LEAVING RECURSIVE DEBUGGER")
651+
cls._recursive_debug -= 1
652+
653+
print("=== SET_TRACE_4 ===")
654+
assert input() == "c"
655+
_pdb.do_continue("")
656+
657+
def do_continue(self, arg):
658+
print("=== do_continue")
659+
# _PdbWrapper.do_continue("")
660+
661+
monkeypatch.setattr(_pytest.debugging, "pytestPDB", pytestPDBTest)
662+
663+
import pdb
664+
monkeypatch.setattr(pdb, "set_trace", pytestPDBTest.set_trace)
665+
620666
set_trace()
621667
"""
622668
)
623-
if capture:
624-
child = testdir.spawn_pytest("%s" % p1)
625-
else:
626-
child = testdir.spawn_pytest("-s %s" % p1)
627-
child.expect("Pdb")
669+
child = testdir.spawn_pytest("%s %s" % (p1, capture_arg))
670+
child.expect("=== SET_TRACE ===")
628671
before = child.before.decode("utf8")
629-
if capture:
672+
if not capture_arg:
630673
assert ">>> PDB set_trace (IO-capturing turned off) >>>" in before
631674
else:
632675
assert ">>> PDB set_trace >>>" in before
633676
child.sendline("debug set_trace()")
634-
child.expect(r"\(Pdb.*")
677+
child.expect("=== SET_TRACE_2 ===")
635678
before = child.before.decode("utf8")
636679
assert "\r\nENTERING RECURSIVE DEBUGGER\r\n" in before
637680
child.sendline("c")
638-
child.expect(r"\(Pdb.*")
681+
child.expect("=== SET_TRACE_3 ===")
639682

640683
# No continue message with recursive debugging.
641684
before = child.before.decode("utf8")
642685
assert ">>> PDB continue " not in before
643-
# No extra newline.
644-
assert before.startswith("c\r\n\r\n--Return--")
645686

646687
child.sendline("c")
647-
child.expect("Pdb")
688+
child.expect("=== SET_TRACE_4 ===")
648689
before = child.before.decode("utf8")
649690
assert "\r\nLEAVING RECURSIVE DEBUGGER\r\n" in before
650691
child.sendline("c")
651692
rest = child.read().decode("utf8")
652-
if capture:
693+
if not capture_arg:
653694
assert "> PDB continue (IO-capturing resumed) >" in rest
654695
else:
655696
assert "> PDB continue >" in rest

0 commit comments

Comments
 (0)