Improve error message when a plugin fails to import

This commit is contained in:
Bruno Oliveira 2016-03-08 19:18:13 -03:00
parent 6d4b14d7ee
commit 7a186df271
3 changed files with 15 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,8 @@ class PytestPluginManager(PluginManager):
importspec = modname
try:
__import__(importspec)
except ImportError:
raise
except ImportError as e:
raise ImportError('Error importing plugin "%s": %s' % (modname, e))
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: