From af6548a4e7a78d7a5a2f1b5fb3a3e056b14ba593 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 23 May 2020 12:09:42 -0700 Subject: [PATCH] Merge pull request #7244 from DahlitzFlorian/fix-issue-7150 Prevent hiding underlying exception when ConfTestImportFailure is raised (cherry picked from commit 45f53266e6fa1539b702df315847cfbce3e3f19e) --- changelog/7150.bugfix.rst | 1 + src/_pytest/debugging.py | 5 +++++ testing/test_debugging.py | 9 +++++++++ 3 files changed, 15 insertions(+) create mode 100644 changelog/7150.bugfix.rst diff --git a/changelog/7150.bugfix.rst b/changelog/7150.bugfix.rst new file mode 100644 index 00000000000..42cf5c7d2b9 --- /dev/null +++ b/changelog/7150.bugfix.rst @@ -0,0 +1 @@ +Prevent hiding the underlying exception when ``ConfTestImportFailure`` is raised. diff --git a/src/_pytest/debugging.py b/src/_pytest/debugging.py index 17915db73fc..26c3095dccd 100644 --- a/src/_pytest/debugging.py +++ b/src/_pytest/debugging.py @@ -4,6 +4,7 @@ import sys from _pytest import outcomes +from _pytest.config import ConftestImportFailure from _pytest.config import hookimpl from _pytest.config.exceptions import UsageError @@ -338,6 +339,10 @@ def _postmortem_traceback(excinfo): # A doctest.UnexpectedException is not useful for post_mortem. # Use the underlying exception instead: return excinfo.value.exc_info[2] + elif isinstance(excinfo.value, ConftestImportFailure): + # A config.ConftestImportFailure is not useful for post_mortem. + # Use the underlying exception instead: + return excinfo.value.excinfo[2] else: return excinfo._excinfo[2] diff --git a/testing/test_debugging.py b/testing/test_debugging.py index 719d6477bff..00af4a088a7 100644 --- a/testing/test_debugging.py +++ b/testing/test_debugging.py @@ -342,6 +342,15 @@ def pytest_runtest_protocol(): child.sendeof() self.flush(child) + def test_pdb_prevent_ConftestImportFailure_hiding_exception(self, testdir): + testdir.makepyfile("def test_func(): pass") + sub_dir = testdir.tmpdir.join("ns").ensure_dir() + sub_dir.join("conftest").new(ext=".py").write("import unknown") + sub_dir.join("test_file").new(ext=".py").write("def test_func(): pass") + + result = testdir.runpytest_subprocess("--pdb", ".") + result.stdout.fnmatch_lines(["-> import unknown"]) + def test_pdb_interaction_capturing_simple(self, testdir): p1 = testdir.makepyfile( """