Skip to content

Improve tracebacks for ImportErrors in conftest #3332

@The-Compiler

Description

@The-Compiler

When a conftest file e.g. has an invalid import, we print a verbose traceback with lots of irrelevant pytest internals in it - this seems to confuse people, like in #3112:

Traceback (most recent call last):
  File "/home/florian/proj/qutebrowser/git/.tox/py36/lib/python3.6/site-packages/_pytest/config.py", line 329, in _getconftestmodules
    return self._path2confmods[path]
KeyError: local('/home/florian/proj/qutebrowser/git/tests')

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/home/florian/proj/qutebrowser/git/.tox/py36/lib/python3.6/site-packages/_pytest/config.py", line 360, in _importconftest
    return self._conftestpath2mod[conftestpath]
KeyError: local('/home/florian/proj/qutebrowser/git/tests/conftest.py')

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/home/florian/proj/qutebrowser/git/.tox/py36/lib/python3.6/site-packages/_pytest/config.py", line 366, in _importconftest
    mod = conftestpath.pyimport()
  File "/home/florian/proj/qutebrowser/git/.tox/py36/lib/python3.6/site-packages/py/_path/local.py", line 668, in pyimport
    __import__(modname)
  File "/home/florian/proj/qutebrowser/git/.tox/py36/lib/python3.6/site-packages/_pytest/assertion/rewrite.py", line 213, in load_module
    py.builtin.exec_(co, mod.__dict__)
  File "/home/florian/proj/qutebrowser/git/tests/conftest.py", line 24, in <module>
    import doesnotexist
ModuleNotFoundError: No module named 'doesnotexist'
ERROR: could not load /home/florian/proj/qutebrowser/git/tests/conftest.py

Compare that with the much nicer input when there's an error inside a test module:

________ ERROR collecting tests/test_conftest.py ________
ImportError while importing test module '/home/florian/proj/qutebrowser/git/tests/test_conftest.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_conftest.py:22: in <module>
    import doesnotexist
E   ModuleNotFoundError: No module named 'doesnotexist'

Metadata

Metadata

Assignees

Labels

topic: tracebacksrelated to displaying and handling of tracebackstype: enhancementnew feature or API change, should be merged into features branch

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions