diff --git a/_pytest/debugging.py b/_pytest/debugging.py index 43472f23bb3..fd7ee23fc32 100644 --- a/_pytest/debugging.py +++ b/_pytest/debugging.py @@ -97,6 +97,12 @@ def _enter_pdb(node, excinfo, rep): tw.sep(">", "captured stderr") tw.line(captured_stderr) + captured_logs = rep.caplog + if len(captured_logs) > 0: + if node.config.getoption('log_print') is not False: + tw.sep(">", "captured logs") + tw.line(captured_logs) + tw.sep(">", "traceback") rep.toterminal(tw) tw.sep(">", "entering PDB") diff --git a/changelog/3204.feature b/changelog/3204.feature new file mode 100644 index 00000000000..8396b186055 --- /dev/null +++ b/changelog/3204.feature @@ -0,0 +1 @@ +Captured logs are printed before entering pdb, unless ``--no-print-logs`` is defined. diff --git a/testing/test_pdb.py b/testing/test_pdb.py index d882c2cf6dc..6be17885bbf 100644 --- a/testing/test_pdb.py +++ b/testing/test_pdb.py @@ -187,6 +187,56 @@ def test_1(): assert "captured stderr" not in output self.flush(child) + def test_pdb_print_captured_logs(self, testdir): + p1 = testdir.makepyfile(""" + def test_1(): + import logging + logging.warn("get rekt") + assert False + """) + child = testdir.spawn_pytest("--pdb %s" % p1) + child.expect("captured logs") + child.expect("get rekt") + child.expect("(Pdb)") + child.sendeof() + rest = child.read().decode("utf8") + assert "1 failed" in rest + self.flush(child) + + def test_pdb_dont_print_captured_logs_when_no_print_logs_is_defined(self, testdir): + p1 = testdir.makepyfile(""" + def test_1(): + import logging + logging.warn("get rekt") + assert False + """) + child = testdir.spawn_pytest("--pdb --no-print-logs %s" % p1) + child.expect("get rekt") + output = child.before.decode("utf8") + assert "captured logs" not in output + child.expect("(Pdb)") + child.sendeof() + rest = child.read().decode("utf8") + assert "1 failed" in rest + self.flush(child) + + def test_pdb_doesnt_print_captured_logs_when_logging_plugin_is_disabled(self, testdir): + p1 = testdir.makepyfile(""" + def test_1(): + import logging + logging.warn("get rekt") + assert False + """) + child = testdir.spawn_pytest("--pdb -p no:logging %s" % p1) + child.expect("get rekt") + output = child.before.decode("utf8") + assert "captured logs" not in output + child.expect("(Pdb)") + child.sendeof() + rest = child.read().decode("utf8") + assert "1 failed" in rest + self.flush(child) + def test_pdb_interaction_exception(self, testdir): p1 = testdir.makepyfile(""" import pytest