Use a nice string repr for ConftestImportFailure

The default message is often hard to read:

    E   _pytest.config.ConftestImportFailure: (local('D:\\projects\\pytest\\.tmp\\root\\foo\\conftest.py'), (<class 'RuntimeError'>, RuntimeError('some error',), <traceback object at 0x000001CCC3E39348>))

Using a shorter message is better:

    E   _pytest.config.ConftestImportFailure: RuntimeError: some error (from D:\projects\pytest\.tmp\root\foo\conftest.py)

And we don't really lose any information due to exception chaining.
This commit is contained in:
Bruno Oliveira 2020-05-16 16:05:12 -03:00
parent 5eaebc1900
commit 9e1e7fcabe
2 changed files with 23 additions and 1 deletions

View File

@ -88,10 +88,15 @@ class ExitCode(enum.IntEnum):
class ConftestImportFailure(Exception):
def __init__(self, path, excinfo):
Exception.__init__(self, path, excinfo)
super().__init__(path, excinfo)
self.path = path
self.excinfo = excinfo # type: Tuple[Type[Exception], Exception, TracebackType]
def __str__(self):
return "{}: {} (from {})".format(
self.excinfo[0].__name__, self.excinfo[1], self.path
)
def main(args=None, plugins=None) -> Union[int, ExitCode]:
""" return exit code, after performing an in-process test run.

View File

@ -10,6 +10,7 @@ import pytest
from _pytest.compat import importlib_metadata
from _pytest.config import _iter_rewritable_modules
from _pytest.config import Config
from _pytest.config import ConftestImportFailure
from _pytest.config import ExitCode
from _pytest.config.exceptions import UsageError
from _pytest.config.findpaths import determine_setup
@ -1471,3 +1472,19 @@ class TestPytestPluginsVariable:
assert res.ret == 0
msg = "Defining 'pytest_plugins' in a non-top-level conftest is no longer supported"
assert msg not in res.stdout.str()
def test_conftest_import_error_repr(tmpdir):
"""
ConftestImportFailure should use a short error message and readable path to the failed
conftest.py file
"""
path = tmpdir.join("foo/conftest.py")
with pytest.raises(
ConftestImportFailure,
match=re.escape("RuntimeError: some error (from {})".format(path)),
):
try:
raise RuntimeError("some error")
except Exception:
raise ConftestImportFailure(path, sys.exc_info())