Arthur Arthur - 1 month ago 21
Python Question

pytest print stacktrace when import fails

Suppose a python test module generates an

ImportError
.
pytest
(version 3.0.2) generates a compact error report:

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


In contrast,
nosetests-3.4
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/failure.py", 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/loader.py", line 418, in loadTestsFromName
addr.filename, addr.module)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nose/importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nose/importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/imp.py", line 234, in load_module
return load_source(name, filename, file)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/imp.py", 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/test_logger.py", line 12, in <module>
from wc.sim.core import Simulator
File "/Users/arthur_at_sinai/gitOnMyLaptopLocal/Mpn-Example/wc/sim/core.py", line 16, in <module>
from wc.log.checkpoint import CheckpointLogger
File "/Users/arthur_at_sinai/gitOnMyLaptopLocal/Mpn-Example/wc/log/checkpoint.py", line 9, in <module>
from wc.config.core import config
ImportError: No module named 'wc.config.core'


How can one get
pytest
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
.

Thanks

Arthur

Answer

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+https://github.com/pytest-dev/pytest.git in the meantime.