Skip to content

Commit b3db440

Browse files
authored
Merge pull request #7257 from DahlitzFlorian/fix-issue-6956
Prevent pytest from printing ConftestImportFailure traceback
2 parents 54b6fe2 + 5b9924e commit b3db440

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

changelog/6956.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prevent pytest from printing ConftestImportFailure traceback to stdout.

src/_pytest/nodes.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from _pytest.compat import cached_property
2020
from _pytest.compat import TYPE_CHECKING
2121
from _pytest.config import Config
22+
from _pytest.config import ConftestImportFailure
2223
from _pytest.config import PytestPluginManager
2324
from _pytest.deprecated import NODE_USE_FROM_PARENT
2425
from _pytest.fixtures import FixtureDef
@@ -28,7 +29,6 @@
2829
from _pytest.mark.structures import MarkDecorator
2930
from _pytest.mark.structures import NodeKeywords
3031
from _pytest.outcomes import fail
31-
from _pytest.outcomes import Failed
3232
from _pytest.store import Store
3333

3434
if TYPE_CHECKING:
@@ -331,8 +331,10 @@ def _prunetraceback(self, excinfo):
331331
pass
332332

333333
def _repr_failure_py(
334-
self, excinfo: ExceptionInfo[Union[Failed, FixtureLookupError]], style=None
334+
self, excinfo: ExceptionInfo[BaseException], style=None,
335335
) -> Union[str, ReprExceptionInfo, ExceptionChainRepr, FixtureLookupErrorRepr]:
336+
if isinstance(excinfo.value, ConftestImportFailure):
337+
excinfo = ExceptionInfo(excinfo.value.excinfo)
336338
if isinstance(excinfo.value, fail.Exception):
337339
if not excinfo.value.pytrace:
338340
return str(excinfo.value)

testing/python/collect.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,23 +1251,20 @@ def test_syntax_error_with_non_ascii_chars(testdir):
12511251
result.stdout.fnmatch_lines(["*ERROR collecting*", "*SyntaxError*", "*1 error in*"])
12521252

12531253

1254-
def test_collecterror_with_fulltrace(testdir):
1254+
def test_collect_error_with_fulltrace(testdir):
12551255
testdir.makepyfile("assert 0")
12561256
result = testdir.runpytest("--fulltrace")
12571257
result.stdout.fnmatch_lines(
12581258
[
12591259
"collected 0 items / 1 error",
12601260
"",
12611261
"*= ERRORS =*",
1262-
"*_ ERROR collecting test_collecterror_with_fulltrace.py _*",
1263-
"",
1264-
"*/_pytest/python.py:*: ",
1265-
"_ _ _ _ _ _ _ _ *",
1262+
"*_ ERROR collecting test_collect_error_with_fulltrace.py _*",
12661263
"",
12671264
"> assert 0",
12681265
"E assert 0",
12691266
"",
1270-
"test_collecterror_with_fulltrace.py:1: AssertionError",
1267+
"test_collect_error_with_fulltrace.py:1: AssertionError",
12711268
"*! Interrupted: 1 error during collection !*",
12721269
]
12731270
)

testing/test_reports.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,14 @@ def check_longrepr(longrepr):
396396
# for same reasons as previous test, ensure we don't blow up here
397397
loaded_report.longrepr.toterminal(tw_mock)
398398

399+
def test_report_prevent_ConftestImportFailure_hiding_exception(self, testdir):
400+
sub_dir = testdir.tmpdir.join("ns").ensure_dir()
401+
sub_dir.join("conftest").new(ext=".py").write("import unknown")
402+
403+
result = testdir.runpytest_subprocess(".")
404+
result.stdout.fnmatch_lines(["E *Error: No module named 'unknown'"])
405+
result.stdout.no_fnmatch_line("ERROR - *ConftestImportFailure*")
406+
399407

400408
class TestHooks:
401409
"""Test that the hooks are working correctly for plugins"""

0 commit comments

Comments
 (0)