Skip to content

#3332 improve traceback for import error #3345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions _pytest/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __str__(self):
etype, evalue, etb = self.excinfo
formatted = traceback.format_tb(etb)
# The level of the tracebacks we want to print is hand crafted :(
return repr(evalue) + '\n' + ''.join(formatted[2:])
return ''.join(formatted[2:]) + '\nE ' + etype.__name__ + ': ' + str(evalue)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about this line? Is it good? Now it looks like

E    ModuleNotFoundError: No module named 'wrong_import'



def main(args=None, plugins=None):
Expand All @@ -52,9 +52,15 @@ def main(args=None, plugins=None):
config = _prepareconfig(args, plugins)
except ConftestImportFailure as e:
tw = py.io.TerminalWriter(sys.stderr)
for line in traceback.format_exception(*e.excinfo):
tw.line(line.rstrip(), red=True)
tw.line("ERROR: could not load %s\n" % (e.path,), red=True)
formatted_tb = safe_str(e)
tw.line(
"ImportError while importing conftest module '{path}'.\n"
"Hint: make sure your test modules/packages have valid Python names.\n"
"Traceback:\n"
"{traceback}".format(path=e.path, traceback=formatted_tb),
red=True
)

return 4
else:
try:
Expand Down Expand Up @@ -166,6 +172,23 @@ def _prepareconfig(args=None, plugins=None):
raise


def print_short_traceback(error, config):
from _pytest.nodes import Collector
from _pytest._code.code import ExceptionInfo
from _pytest.python import filter_traceback
exc_info = ExceptionInfo()
if config and config.getoption('verbose') < 2:
exc_info.traceback = exc_info.traceback.filter(filter_traceback)
exc_repr = exc_info.getrepr(style='short') if exc_info.traceback else exc_info.exconly()
formatted_tb = safe_str(exc_repr)
raise Collector.CollectError(
"ImportError while importing test module '{fspath}'.\n"
"Hint: make sure your test modules/packages have valid Python names.\n"
"Traceback:\n"
"{traceback}".format(fspath=error.path, traceback=formatted_tb)
) from None


class PytestPluginManager(PluginManager):
"""
Overwrites :py:class:`pluggy.PluginManager <pluggy.PluginManager>` to add pytest-specific
Expand Down Expand Up @@ -203,6 +226,7 @@ def __init__(self):
self.rewrite_hook = _pytest.assertion.DummyRewriteHook()
# Used to know when we are importing conftests after the pytest_configure stage
self._configured = False
self._config = None

def addhooks(self, module_or_class):
"""
Expand Down Expand Up @@ -279,6 +303,7 @@ def pytest_configure(self, config):
"trylast: mark a hook implementation function such that the "
"plugin machinery will try to call it last/as late as possible.")
self._configured = True
self._config = config

def _warn(self, message):
kwargs = message if isinstance(message, dict) else {
Expand Down Expand Up @@ -345,7 +370,11 @@ def _getconftestmodules(self, path):
continue
conftestpath = parent.join("conftest.py")
if conftestpath.isfile():
mod = self._importconftest(conftestpath)
mod = None
try:
mod = self._importconftest(conftestpath)
except ConftestImportFailure as e:
print_short_traceback(e, self._config)
clist.append(mod)

self._path2confmods[path] = clist
Expand Down
19 changes: 4 additions & 15 deletions _pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import py
import six
from _pytest.mark import MarkerError
from _pytest.config import hookimpl
from _pytest.config import hookimpl, print_short_traceback

import _pytest
import pluggy
Expand All @@ -25,7 +25,7 @@
isclass, isfunction, is_generator, ascii_escaped,
REGEX_TYPE, STRING_TYPES, NoneType, NOTSET,
get_real_func, getfslineno, safe_getattr,
safe_str, getlocation, enum,
getlocation, enum,
)
from _pytest.outcomes import fail
from _pytest.mark.structures import transfer_markers
Expand Down Expand Up @@ -424,19 +424,8 @@ def _importtestmodule(self):
"unique basename for your test file modules"
% e.args
)
except ImportError:
from _pytest._code.code import ExceptionInfo
exc_info = ExceptionInfo()
if self.config.getoption('verbose') < 2:
exc_info.traceback = exc_info.traceback.filter(filter_traceback)
exc_repr = exc_info.getrepr(style='short') if exc_info.traceback else exc_info.exconly()
formatted_tb = safe_str(exc_repr)
raise self.CollectError(
"ImportError while importing test module '{fspath}'.\n"
"Hint: make sure your test modules/packages have valid Python names.\n"
"Traceback:\n"
"{traceback}".format(fspath=self.fspath, traceback=formatted_tb)
)
except ImportError as e:
print_short_traceback(e, self.config)
except _pytest.runner.Skipped as e:
if e.allow_module_level:
raise
Expand Down
1 change: 1 addition & 0 deletions changelog/3332.trivial
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved tracebacks for ImportErrors in conftest.py
6 changes: 3 additions & 3 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ def test_issue486_better_reporting_on_conftest_load_failure(self, testdir):
*warning*conftest.py*
""")
result = testdir.runpytest()
result.stderr.fnmatch_lines("""
*ERROR*could not load*conftest.py*
""")
result.stderr.fnmatch_lines(["*ImportError while importing conftest module*conftest.py*",
"E *Error: No module named*qwerty*"]
)

def test_early_skip(self, testdir):
testdir.mkdir("xyz")
Expand Down