From 7a186df2718e8cf210fe18c0fe7cdde92d96aa64 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 8 Mar 2016 19:18:13 -0300 Subject: [PATCH] Improve error message when a plugin fails to import --- CHANGELOG.rst | 8 +++++++- _pytest/config.py | 4 ++-- testing/test_pluginmanager.py | 8 ++++++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index fb545701e..c81f342b0 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -5,6 +5,11 @@ * +* + +* Improve error message when a plugin fails to load. + Thanks `@nicoddemus`_ for the PR. + * Fix (`#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 `_): 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 diff --git a/_pytest/config.py b/_pytest/config.py index 70e19b1d1..ed5138cf4 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -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): diff --git a/testing/test_pluginmanager.py b/testing/test_pluginmanager.py index 97daf2eae..36847638d 100644 --- a/testing/test_pluginmanager.py +++ b/testing/test_pluginmanager.py @@ -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: