MCPcopy Create free account
hub / github.com/pytest-dev/pytest / test_show_traceback_import_error

Method test_show_traceback_import_error

testing/python/collect.py:93–127  ·  view source on GitHub ↗

Import errors when collecting modules should display the traceback (#1976). With low verbosity we omit pytest and internal modules, otherwise show all traceback entries.

(
        self, pytester: Pytester, verbose: int
    )

Source from the content-addressed store, hash-verified

91
92 @pytest.mark.parametrize("verbose", [0, 1, 2])
93 def test_show_traceback_import_error(
94 self, pytester: Pytester, verbose: int
95 ) -> None:
96 """Import errors when collecting modules should display the traceback (#1976).
97
98 With low verbosity we omit pytest and internal modules, otherwise show all traceback entries.
99 """
100 pytester.makepyfile(
101 foo_traceback_import_error="""
102 from bar_traceback_import_error import NOT_AVAILABLE
103 """,
104 bar_traceback_import_error="",
105 )
106 pytester.makepyfile(
107 """
108 import foo_traceback_import_error
109 """
110 )
111 args = ("-v",) * verbose
112 result = pytester.runpytest(*args)
113 result.stdout.fnmatch_lines(
114 [
115 "ImportError while importing test module*",
116 "Traceback:",
117 "*from bar_traceback_import_error import NOT_AVAILABLE",
118 "*cannot import name *NOT_AVAILABLE*",
119 ]
120 )
121 assert result.ret == 2
122
123 stdout = result.stdout.str()
124 if verbose == 2:
125 assert "_pytest" in stdout
126 else:
127 assert "_pytest" not in stdout
128
129 def test_show_traceback_import_error_unicode(self, pytester: Pytester) -> None:
130 """Check test modules collected which raise ImportError with unicode messages

Callers

nothing calls this directly

Calls 4

fnmatch_linesMethod · 0.80
strMethod · 0.80
makepyfileMethod · 0.45
runpytestMethod · 0.45

Tested by

no test coverage detected