diff --git a/AUTHORS b/AUTHORS index 583fa6f9e..ab646f406 100644 --- a/AUTHORS +++ b/AUTHORS @@ -29,6 +29,7 @@ Benjamin Peterson Bernard Pratz Bob Ippolito Brian Dorsey +Brian Maissy Brian Okken Brianna Laugher Bruno Oliveira diff --git a/_pytest/debugging.py b/_pytest/debugging.py index 23d94e688..8b5f5cc2c 100644 --- a/_pytest/debugging.py +++ b/_pytest/debugging.py @@ -85,6 +85,17 @@ def _enter_pdb(node, excinfo, rep): # for not completely clear reasons. tw = node.config.pluginmanager.getplugin("terminalreporter")._tw tw.line() + + captured_stdout = rep.capstdout + if len(captured_stdout) > 0: + tw.sep(">", "captured stdout") + tw.line(captured_stdout) + + captured_stderr = rep.capstderr + if len(captured_stderr) > 0: + tw.sep(">", "captured stderr") + tw.line(captured_stderr) + tw.sep(">", "traceback") rep.toterminal(tw) tw.sep(">", "entering PDB") diff --git a/changelog/3052.bugfix b/changelog/3052.bugfix new file mode 100644 index 000000000..ea8c362a4 --- /dev/null +++ b/changelog/3052.bugfix @@ -0,0 +1 @@ +Added printing of captured stdout/stderr before entering pdb, and improved a test which was giving false negatives about output capturing. diff --git a/testing/test_pdb.py b/testing/test_pdb.py index 8618473bb..01604b633 100644 --- a/testing/test_pdb.py +++ b/testing/test_pdb.py @@ -141,19 +141,50 @@ class TestPDB(object): child.sendeof() self.flush(child) - def test_pdb_interaction_capture(self, testdir): + def test_pdb_print_captured_stdout(self, testdir): p1 = testdir.makepyfile(""" def test_1(): - print("getrekt") + print("get\\x20rekt") assert False """) child = testdir.spawn_pytest("--pdb %s" % p1) - child.expect("getrekt") + child.expect("captured stdout") + child.expect("get rekt") child.expect("(Pdb)") child.sendeof() rest = child.read().decode("utf8") assert "1 failed" in rest - assert "getrekt" not 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("(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_dont_print_empty_captured_stdout_and_stderr(self, testdir): + p1 = testdir.makepyfile(""" + def test_1(): + assert False + """) + child = testdir.spawn_pytest("--pdb %s" % p1) + child.expect("(Pdb)") + output = child.before.decode("utf8") + child.sendeof() + assert "captured stdout" not in output + assert "captured stderr" not in output self.flush(child) def test_pdb_interaction_exception(self, testdir):