From 7656fc8320b5920d860e9028f322475a021e7666 Mon Sep 17 00:00:00 2001 From: Brian Maissy Date: Mon, 12 Feb 2018 23:17:51 +0200 Subject: [PATCH] Added printing of captured stdout and stderr before entering pdb --- AUTHORS | 1 + _pytest/debugging.py | 11 +++++++++++ changelog/3052.bugfix | 1 + testing/test_pdb.py | 39 +++++++++++++++++++++++++++++++++++---- 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 changelog/3052.bugfix diff --git a/AUTHORS b/AUTHORS index 583fa6f9e18..ab646f4065c 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 23d94e6880d..8b5f5cc2ca4 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 00000000000..ea8c362a4c8 --- /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 8618473bb15..01604b63397 100644 --- a/testing/test_pdb.py +++ b/testing/test_pdb.py @@ -141,19 +141,50 @@ def test_one(self): 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):