Merge pull request #860 from nicoddemus/warn-plugins-as-str-main

Make passing plugins as str objects a more obvious failure
This commit is contained in:
Bruno Oliveira 2015-07-24 19:13:39 -03:00
commit 91e8e59cea
3 changed files with 25 additions and 1 deletions

View File

@ -7,6 +7,11 @@
- fix issue856: consider --color parameter in all outputs (for example - fix issue856: consider --color parameter in all outputs (for example
--fixtures). Thanks Barney Gale for the report and Bruno Oliveira for the PR. --fixtures). Thanks Barney Gale for the report and Bruno Oliveira for the PR.
- fix issue855: passing str objects as `plugins` argument to pytest.main
is now interpreted as a module name to be imported and registered as a
plugin, instead of silently having no effect.
Thanks xmo-odoo for the report and Bruno Oliveira for the PR.
- fix issue744: fix for ast.Call changes in Python 3.5+. Thanks - fix issue744: fix for ast.Call changes in Python 3.5+. Thanks
Guido van Rossum, Matthias Bussonnier, Stefan Zimmermann and Guido van Rossum, Matthias Bussonnier, Stefan Zimmermann and
Thomas Kluyver. Thomas Kluyver.

View File

@ -80,7 +80,10 @@ def _prepareconfig(args=None, plugins=None):
try: try:
if plugins: if plugins:
for plugin in plugins: for plugin in plugins:
pluginmanager.register(plugin) if isinstance(plugin, py.builtin._basestring):
pluginmanager.consider_pluginarg(plugin)
else:
pluginmanager.register(plugin)
return pluginmanager.hook.pytest_cmdline_parse( return pluginmanager.hook.pytest_cmdline_parse(
pluginmanager=pluginmanager, args=args) pluginmanager=pluginmanager, args=args)
except Exception: except Exception:

View File

@ -1,3 +1,4 @@
import sys
import py, pytest import py, pytest
class TestGeneralUsage: class TestGeneralUsage:
@ -370,6 +371,21 @@ class TestGeneralUsage:
"*fixture 'invalid_fixture' not found", "*fixture 'invalid_fixture' not found",
]) ])
def test_plugins_given_as_strings(self, tmpdir, monkeypatch):
"""test that str values passed to main() as `plugins` arg
are interpreted as module names to be imported and registered.
#855.
"""
with pytest.raises(ImportError) as excinfo:
pytest.main([str(tmpdir)], plugins=['invalid.module'])
assert 'invalid' in str(excinfo.value)
p = tmpdir.join('test_test_plugins_given_as_strings.py')
p.write('def test_foo(): pass')
mod = py.std.types.ModuleType("myplugin")
monkeypatch.setitem(sys.modules, 'myplugin', mod)
assert pytest.main(args=[str(tmpdir)], plugins=['myplugin']) == 0
class TestInvocationVariants: class TestInvocationVariants:
def test_earlyinit(self, testdir): def test_earlyinit(self, testdir):