Skip to content

Commit

Permalink
Display full traceback from Import errors when collecting test modules
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Oct 3, 2016
1 parent fc02003 commit 7d66e4e
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 20 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@

*

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

*

*


.. _@cwitty: https://github.com/cwitty

.. _#1976: https://github.com/pytest-dev/pytest/issues/1976



3.0.3
=====

Expand Down
13 changes: 8 additions & 5 deletions _pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,15 @@ def _importtestmodule(self):
% e.args
)
except ImportError:
exc_class, exc, _ = sys.exc_info()
import traceback
stream = py.io.TextIO()
traceback.print_exc(file=stream)
formatted_tb = stream.getvalue()
raise self.CollectError(
"ImportError while importing test module '%s'.\n"
"Original error message:\n'%s'\n"
"Make sure your test modules/packages have valid Python names."
% (self.fspath, exc or exc_class)
"ImportError while importing test module '{fspath}'.\n"
"Hint: make sure your test modules/packages have valid Python names.\n"
"Original traceback:\n"
"{traceback}".format(fspath=self.fspath, traceback=formatted_tb)
)
except _pytest.runner.Skipped as e:
if e.allow_module_level:
Expand Down
2 changes: 1 addition & 1 deletion testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_this():
result.stdout.fnmatch_lines([
#XXX on jython this fails: "> import import_fails",
"ImportError while importing test module*",
"'No module named *does_not_work*",
"*No module named *does_not_work*",
])
assert result.ret == 2

Expand Down
22 changes: 21 additions & 1 deletion testing/python/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,29 @@ def test_invalid_test_module_name(self, testdir):
result = testdir.runpytest("-rw")
result.stdout.fnmatch_lines([
"ImportError while importing test module*test_one.part1*",
"Make sure your test modules/packages have valid Python names.",
"Hint: make sure your test modules/packages have valid Python names.",
])

def test_show_full_traceback_import_error(self, testdir):
"""Import errors when collecting modules should display the full traceback (#1976)."""
testdir.makepyfile(
foo_traceback_import_error="""
from bar_traceback_import_error import NOT_AVAILABLE
""",
bar_traceback_import_error="",
)
testdir.makepyfile("""
import foo_traceback_import_error
""")
result = testdir.runpytest()
result.stdout.fnmatch_lines([
"ImportError while importing test module*",
"Original traceback:",
"*from bar_traceback_import_error import NOT_AVAILABLE",
"*cannot import name *NOT_AVAILABLE*",
])
assert result.ret == 2


class TestClass:
def test_class_with_init_warning(self, testdir):
Expand Down
11 changes: 0 additions & 11 deletions testing/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,6 @@ def pytest_collect_directory(self, path, parent):
assert "world" in wascalled

class TestPrunetraceback:
def test_collection_error(self, testdir):
p = testdir.makepyfile("""
import not_exists
""")
result = testdir.runpytest(p)
assert "__import__" not in result.stdout.str(), "too long traceback"
result.stdout.fnmatch_lines([
"*ERROR collecting*",
"ImportError while importing test module*",
"'No module named *not_exists*",
])

def test_custom_repr_failure(self, testdir):
p = testdir.makepyfile("""
Expand Down
2 changes: 1 addition & 1 deletion testing/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ def test_collect_fail(self, testdir, option):
result = testdir.runpytest(*option.args)
result.stdout.fnmatch_lines([
"ImportError while importing*",
"'No module named *xyz*",
"*No module named *xyz*",
"*1 error*",
])

Expand Down

0 comments on commit 7d66e4e

Please sign in to comment.