Display traceback from Import errors using pytest's short representation
Also omit pytest's own modules and internal libraries (py and pluggy) in low verbosity Fix #1976
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import os
|
||||
import sys
|
||||
from textwrap import dedent
|
||||
|
||||
@@ -71,8 +72,12 @@ class TestModule:
|
||||
"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)."""
|
||||
@pytest.mark.parametrize('verbose', [0, 1, 2])
|
||||
def test_show_traceback_import_error(self, testdir, verbose):
|
||||
"""Import errors when collecting modules should display the traceback (#1976).
|
||||
|
||||
With low verbosity we omit pytest and internal modules, otherwise show all traceback entries.
|
||||
"""
|
||||
testdir.makepyfile(
|
||||
foo_traceback_import_error="""
|
||||
from bar_traceback_import_error import NOT_AVAILABLE
|
||||
@@ -82,15 +87,23 @@ class TestModule:
|
||||
testdir.makepyfile("""
|
||||
import foo_traceback_import_error
|
||||
""")
|
||||
result = testdir.runpytest()
|
||||
args = ('-v',) * verbose
|
||||
result = testdir.runpytest(*args)
|
||||
result.stdout.fnmatch_lines([
|
||||
"ImportError while importing test module*",
|
||||
"Original traceback:",
|
||||
"Traceback:",
|
||||
"*from bar_traceback_import_error import NOT_AVAILABLE",
|
||||
"*cannot import name *NOT_AVAILABLE*",
|
||||
])
|
||||
assert result.ret == 2
|
||||
|
||||
stdout = result.stdout.str()
|
||||
for name in ('_pytest', os.path.join('py', '_path')):
|
||||
if verbose == 2:
|
||||
assert name in stdout
|
||||
else:
|
||||
assert name not in stdout
|
||||
|
||||
|
||||
class TestClass:
|
||||
def test_class_with_init_warning(self, testdir):
|
||||
|
||||
Reference in New Issue
Block a user