Skip to content

bugfix for issue #2491 #2762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions _pytest/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import traceback
import types
import warnings
import six

import py
# DON't import pytest here because it causes import cycle troubles
Expand Down Expand Up @@ -442,12 +443,12 @@ def import_plugin(self, modname):
try:
__import__(importspec)
except ImportError as e:
new_exc = ImportError('Error importing plugin "%s": %s' % (modname, safe_str(e.args[0])))
# 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
new_exc_type = ImportError
new_exc_message = 'Error importing plugin "%s": %s' % (modname, safe_str(e.args[0]))
new_exc = new_exc_type(new_exc_message)

six.reraise(new_exc_type, new_exc, sys.exc_info()[2])

except Exception as e:
import pytest
if not hasattr(pytest, 'skip') or not isinstance(e, pytest.skip.Exception):
Expand Down
4 changes: 4 additions & 0 deletions changelog/2491.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
If an exception happens while loading a plugin, PyTest no longer hides the original traceback.
In python2 it will show the original traceback with a new message that explains in which plugin.
In python3 it will show 2 canonized exceptions, the original exception while loading the plugin
in addition to an exception that PyTest throws about loading a plugin.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def has_environment_marker_support():


def main():
install_requires = ['py>=1.4.33', 'setuptools'] # pluggy is vendored in _pytest.vendored_packages
install_requires = ['py>=1.4.33', 'setuptools', 'six'] # pluggy is vendored in _pytest.vendored_packages
extras_require = {}
if has_environment_marker_support():
extras_require[':python_version=="2.6"'] = ['argparse', 'ordereddict']
Expand Down
11 changes: 8 additions & 3 deletions testing/test_pluginmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,17 @@ def test_importplugin_error_message(testdir, pytestpm):
testdir.syspathinsert(testdir.tmpdir)
testdir.makepyfile(qwe="""
# encoding: UTF-8
raise ImportError(u'Not possible to import: ☺')
def test_traceback():
raise ImportError(u'Not possible to import: ☺')

test_traceback()
""")
with pytest.raises(ImportError) as excinfo:
pytestpm.import_plugin("qwe")
expected = '.*Error importing plugin "qwe": Not possible to import: .'
assert py.std.re.match(expected, str(excinfo.value))
expected_message = '.*Error importing plugin "qwe": Not possible to import: .'
expected_traceback = ".*in test_traceback"
assert py.std.re.match(expected_message, str(excinfo.value))
assert py.std.re.match(expected_traceback, str(excinfo.traceback[-1]))


class TestPytestPluginManager(object):
Expand Down