Arthur Arthur - 11 months ago 80
Python Question

pytest print stacktrace when import fails

Suppose a python test module generates an

(version 3.0.2) generates a compact error report:

__________________________________________ ERROR collecting tests/wc_tests/log/ __________________________________________
ImportError while importing test module '/Users/arthur_at_sinai/gitOnMyLaptopLocal/Mpn-Example/tests/wc_tests/log/'.
Original error message:
'No module named 'wc.config.core''
Make sure your test modules/packages have valid Python names.

In contrast,
generates a stacktrace, like this:

ERROR: Failure: ImportError (No module named 'wc.config.core')
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nose/", line 39, in runTest
raise self.exc_val.with_traceback(self.tb)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nose/", line 418, in loadTestsFromName
addr.filename, addr.module)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nose/", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nose/", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/", line 234, in load_module
return load_source(name, filename, file)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/", line 172, in load_source
module = _load(spec)
File "<frozen importlib._bootstrap>", line 693, in _load
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/Users/arthur_at_sinai/gitOnMyLaptopLocal/Mpn-Example/tests/wc_tests/log/", line 12, in <module>
from wc.sim.core import Simulator
File "/Users/arthur_at_sinai/gitOnMyLaptopLocal/Mpn-Example/wc/sim/", line 16, in <module>
from wc.log.checkpoint import CheckpointLogger
File "/Users/arthur_at_sinai/gitOnMyLaptopLocal/Mpn-Example/wc/log/", line 9, in <module>
from wc.config.core import config
ImportError: No module named 'wc.config.core'

How can one get
to produce similar stacktrace information? These options are available

-l, --showlocals show locals in tracebacks (disabled by default).
--tb=style traceback print mode (auto/long/short/line/native/no).
--full-trace don't cut any tracebacks (default is to cut).

but none of them make a tb for me. Nor does
pytest -vv



Answer Source

This was changed in pytest a week ago to display the full traceback.

If you don't want to wait for the next release, you could use pytest from the git repository via pip install git+ in the meantime.