Skip to content

Commit

Permalink
Merge pull request #7271 from asottile/backport-7257
Browse files Browse the repository at this point in the history
[5.4.x] Merge pull request #7257 from DahlitzFlorian/fix-issue-6956
  • Loading branch information
asottile authored May 27, 2020
2 parents 565f4cb + 21ca38b commit b7b7292
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelog/6956.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prevent pytest from printing ConftestImportFailure traceback to stdout.
6 changes: 4 additions & 2 deletions src/_pytest/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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
Expand All @@ -28,7 +29,6 @@
from _pytest.mark.structures import MarkDecorator
from _pytest.mark.structures import NodeKeywords
from _pytest.outcomes import fail
from _pytest.outcomes import Failed
from _pytest.store import Store

if TYPE_CHECKING:
Expand Down Expand Up @@ -318,8 +318,10 @@ def _prunetraceback(self, excinfo):
pass

def _repr_failure_py(
self, excinfo: ExceptionInfo[Union[Failed, FixtureLookupError]], style=None
self, excinfo: ExceptionInfo[BaseException], style=None,
) -> Union[str, ReprExceptionInfo, ExceptionChainRepr, FixtureLookupErrorRepr]:
if isinstance(excinfo.value, ConftestImportFailure):
excinfo = ExceptionInfo(excinfo.value.excinfo)
if isinstance(excinfo.value, fail.Exception):
if not excinfo.value.pytrace:
return str(excinfo.value)
Expand Down
9 changes: 3 additions & 6 deletions testing/python/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1225,23 +1225,20 @@ 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(
[
"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 !*",
]
)
Expand Down
8 changes: 8 additions & 0 deletions testing/test_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,14 @@ def check_longrepr(longrepr):
# 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 *Error: No module named 'unknown'"])
result.stdout.no_fnmatch_line("ERROR - *ConftestImportFailure*")


class TestHooks:
"""Test that the hooks are working correctly for plugins"""
Expand Down

0 comments on commit b7b7292

Please sign in to comment.