Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 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
14 changes: 10 additions & 4 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
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