Skip to content

Commit b7b7292

Browse files
authored
Merge pull request #7271 from asottile/backport-7257
[5.4.x] Merge pull request #7257 from DahlitzFlorian/fix-issue-6956
2 parents 565f4cb + 21ca38b commit b7b7292

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:
@@ -318,8 +318,10 @@ def _prunetraceback(self, excinfo):
318318
pass
319319

320320
def _repr_failure_py(
321-
self, excinfo: ExceptionInfo[Union[Failed, FixtureLookupError]], style=None
321+
self, excinfo: ExceptionInfo[BaseException], style=None,
322322
) -> Union[str, ReprExceptionInfo, ExceptionChainRepr, FixtureLookupErrorRepr]:
323+
if isinstance(excinfo.value, ConftestImportFailure):
324+
excinfo = ExceptionInfo(excinfo.value.excinfo)
323325
if isinstance(excinfo.value, fail.Exception):
324326
if not excinfo.value.pytrace:
325327
return str(excinfo.value)

testing/python/collect.py

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

12271227

1228-
def test_collecterror_with_fulltrace(testdir):
1228+
def test_collect_error_with_fulltrace(testdir):
12291229
testdir.makepyfile("assert 0")
12301230
result = testdir.runpytest("--fulltrace")
12311231
result.stdout.fnmatch_lines(
12321232
[
12331233
"collected 0 items / 1 error",
12341234
"",
12351235
"*= ERRORS =*",
1236-
"*_ ERROR collecting test_collecterror_with_fulltrace.py _*",
1237-
"",
1238-
"*/_pytest/python.py:*: ",
1239-
"_ _ _ _ _ _ _ _ *",
1236+
"*_ ERROR collecting test_collect_error_with_fulltrace.py _*",
12401237
"",
12411238
"> assert 0",
12421239
"E assert 0",
12431240
"",
1244-
"test_collecterror_with_fulltrace.py:1: AssertionError",
1241+
"test_collect_error_with_fulltrace.py:1: AssertionError",
12451242
"*! Interrupted: 1 error during collection !*",
12461243
]
12471244
)

testing/test_reports.py

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

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

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

0 commit comments

Comments
 (0)