Merge pull request #1443 from nicoddemus/improve-import-error-msg

Improve error message when a plugin fails to import
This commit is contained in:
Florian Bruhin 2016-03-11 06:10:58 +01:00
commit 2b0ad4630d
3 changed files with 20 additions and 5 deletions

View File

@ -5,6 +5,11 @@
*
*
* Improve error message when a plugin fails to load.
Thanks `@nicoddemus`_ for the PR.
* Fix (`#1178 <https://github.com/pytest-dev/pytest/issues/1178>`_):
``pytest.fail`` with non-ascii characters raises an internal pytest error.
Thanks `@nicoddemus`_ for the PR.
@ -12,7 +17,6 @@
* Fix (`#469`_): junit parses report.nodeid incorrectly, when params IDs
contain ``::``. Thanks `@tomviner`_ for the PR (`#1431`_).
* Fix (`#578 <https://github.com/pytest-dev/pytest/issues/578>`_): SyntaxErrors
containing non-ascii lines at the point of failure generated an internal
py.test error.
@ -23,6 +27,8 @@
*
*
.. _#1437: https://github.com/pytest-dev/pytest/issues/1437
.. _#469: https://github.com/pytest-dev/pytest/issues/469

View File

@ -383,8 +383,13 @@ class PytestPluginManager(PluginManager):
importspec = modname
try:
__import__(importspec)
except ImportError:
raise
except ImportError as e:
new_exc = ImportError('Error importing plugin "%s": %s' % (modname, e))
# copy over name and path attributes
for attr in ('name', 'path'):
if hasattr(e, attr):
setattr(new_exc, attr, getattr(e, attr))
raise new_exc
except Exception as e:
import pytest
if not hasattr(pytest, 'skip') or not isinstance(e, pytest.skip.Exception):

View File

@ -178,13 +178,17 @@ def test_default_markers(testdir):
"*trylast*last*",
])
def test_importplugin_issue375(testdir, pytestpm):
"""Don't hide import errors when importing plugins and provide
an easy to debug message.
"""
testdir.syspathinsert(testdir.tmpdir)
testdir.makepyfile(qwe="import aaaa")
with pytest.raises(ImportError) as excinfo:
pytestpm.import_plugin("qwe")
assert "qwe" not in str(excinfo.value)
assert "aaaa" in str(excinfo.value)
expected = '.*Error importing plugin "qwe": No module named \'?aaaa\'?'
assert py.std.re.match(expected, str(excinfo.value))
class TestPytestPluginManager: