Prevent pytest from printing ConftestImportFailure traceback

This commit is contained in:
Florian Dahlitz 2020-05-25 13:53:56 +02:00
parent 981b096940
commit 6546d1f725
No known key found for this signature in database
GPG Key ID: 628E551560123F45
4 changed files with 19 additions and 6 deletions

View File

@ -0,0 +1 @@
Prevent pytest from printing ConftestImportFailure traceback to stdout.

View File

@ -19,6 +19,7 @@ from _pytest._code.code import ReprExceptionInfo
from _pytest.compat import cached_property
from _pytest.compat import TYPE_CHECKING
from _pytest.config import Config
from _pytest.config import ConftestImportFailure
from _pytest.config import PytestPluginManager
from _pytest.deprecated import NODE_USE_FROM_PARENT
from _pytest.fixtures import FixtureDef
@ -340,6 +341,8 @@ class Node(metaclass=NodeMeta):
return excinfo.value.formatrepr()
if self.config.getoption("fulltrace", False):
style = "long"
if excinfo.type is ConftestImportFailure: # type: ignore
excinfo = ExceptionInfo.from_exc_info(excinfo.value.excinfo) # type: ignore
else:
tb = _pytest._code.Traceback([excinfo.traceback[-1]])
self._prunetraceback(excinfo)

View File

@ -1251,7 +1251,7 @@ def test_syntax_error_with_non_ascii_chars(testdir):
result.stdout.fnmatch_lines(["*ERROR collecting*", "*SyntaxError*", "*1 error in*"])
def test_collecterror_with_fulltrace(testdir):
def test_collect_error_with_fulltrace(testdir):
testdir.makepyfile("assert 0")
result = testdir.runpytest("--fulltrace")
result.stdout.fnmatch_lines(
@ -1259,15 +1259,12 @@ def test_collecterror_with_fulltrace(testdir):
"collected 0 items / 1 error",
"",
"*= ERRORS =*",
"*_ ERROR collecting test_collecterror_with_fulltrace.py _*",
"",
"*/_pytest/python.py:*: ",
"_ _ _ _ _ _ _ _ *",
"*_ ERROR collecting test_collect_error_with_fulltrace.py _*",
"",
"> assert 0",
"E assert 0",
"",
"test_collecterror_with_fulltrace.py:1: AssertionError",
"test_collect_error_with_fulltrace.py:1: AssertionError",
"*! Interrupted: 1 error during collection !*",
]
)

View File

@ -396,6 +396,18 @@ class TestReportSerialization:
# for same reasons as previous test, ensure we don't blow up here
loaded_report.longrepr.toterminal(tw_mock)
def test_report_prevent_ConftestImportFailure_hiding_exception(self, testdir):
sub_dir = testdir.tmpdir.join("ns").ensure_dir()
sub_dir.join("conftest").new(ext=".py").write("import unknown")
result = testdir.runpytest_subprocess(".")
result.stdout.fnmatch_lines(
["E ModuleNotFoundError: No module named 'unknown'"]
)
result.stdout.no_fnmatch_line(
"ERROR - _pytest.config.ConftestImportFailure: ModuleNotFoundError:*"
)
class TestHooks:
"""Test that the hooks are working correctly for plugins"""