Skip to content

Commit 7d66e4e

Browse files
committed
Display full traceback from Import errors when collecting test modules
Fix pytest-dev#1976
1 parent fc02003 commit 7d66e4e

File tree

6 files changed

+39
-20
lines changed

6 files changed

+39
-20
lines changed

CHANGELOG.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33

44
*
55

6-
*
6+
* Import errors when collecting test modules now display the full traceback (`#1976`_).
7+
Thanks `@cwitty`_ for the report and `@nicoddemus`_ for the PR.
78

89
*
910

1011
*
1112

1213

14+
.. _@cwitty: https://github.com/cwitty
15+
16+
.. _#1976: https://github.com/pytest-dev/pytest/issues/1976
17+
18+
19+
1320
3.0.3
1421
=====
1522

_pytest/python.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -424,12 +424,15 @@ def _importtestmodule(self):
424424
% e.args
425425
)
426426
except ImportError:
427-
exc_class, exc, _ = sys.exc_info()
427+
import traceback
428+
stream = py.io.TextIO()
429+
traceback.print_exc(file=stream)
430+
formatted_tb = stream.getvalue()
428431
raise self.CollectError(
429-
"ImportError while importing test module '%s'.\n"
430-
"Original error message:\n'%s'\n"
431-
"Make sure your test modules/packages have valid Python names."
432-
% (self.fspath, exc or exc_class)
432+
"ImportError while importing test module '{fspath}'.\n"
433+
"Hint: make sure your test modules/packages have valid Python names.\n"
434+
"Original traceback:\n"
435+
"{traceback}".format(fspath=self.fspath, traceback=formatted_tb)
433436
)
434437
except _pytest.runner.Skipped as e:
435438
if e.allow_module_level:

testing/acceptance_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def test_this():
120120
result.stdout.fnmatch_lines([
121121
#XXX on jython this fails: "> import import_fails",
122122
"ImportError while importing test module*",
123-
"'No module named *does_not_work*",
123+
"*No module named *does_not_work*",
124124
])
125125
assert result.ret == 2
126126

testing/python/collect.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,29 @@ def test_invalid_test_module_name(self, testdir):
6868
result = testdir.runpytest("-rw")
6969
result.stdout.fnmatch_lines([
7070
"ImportError while importing test module*test_one.part1*",
71-
"Make sure your test modules/packages have valid Python names.",
71+
"Hint: make sure your test modules/packages have valid Python names.",
7272
])
7373

74+
def test_show_full_traceback_import_error(self, testdir):
75+
"""Import errors when collecting modules should display the full traceback (#1976)."""
76+
testdir.makepyfile(
77+
foo_traceback_import_error="""
78+
from bar_traceback_import_error import NOT_AVAILABLE
79+
""",
80+
bar_traceback_import_error="",
81+
)
82+
testdir.makepyfile("""
83+
import foo_traceback_import_error
84+
""")
85+
result = testdir.runpytest()
86+
result.stdout.fnmatch_lines([
87+
"ImportError while importing test module*",
88+
"Original traceback:",
89+
"*from bar_traceback_import_error import NOT_AVAILABLE",
90+
"*cannot import name *NOT_AVAILABLE*",
91+
])
92+
assert result.ret == 2
93+
7494

7595
class TestClass:
7696
def test_class_with_init_warning(self, testdir):

testing/test_collection.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,6 @@ def pytest_collect_directory(self, path, parent):
172172
assert "world" in wascalled
173173

174174
class TestPrunetraceback:
175-
def test_collection_error(self, testdir):
176-
p = testdir.makepyfile("""
177-
import not_exists
178-
""")
179-
result = testdir.runpytest(p)
180-
assert "__import__" not in result.stdout.str(), "too long traceback"
181-
result.stdout.fnmatch_lines([
182-
"*ERROR collecting*",
183-
"ImportError while importing test module*",
184-
"'No module named *not_exists*",
185-
])
186175

187176
def test_custom_repr_failure(self, testdir):
188177
p = testdir.makepyfile("""

testing/test_terminal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ def test_collect_fail(self, testdir, option):
667667
result = testdir.runpytest(*option.args)
668668
result.stdout.fnmatch_lines([
669669
"ImportError while importing*",
670-
"'No module named *xyz*",
670+
"*No module named *xyz*",
671671
"*1 error*",
672672
])
673673

0 commit comments

Comments
 (0)