From 35bbcc39a28b3464612f1b4060b05877290794ff Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sat, 18 Jul 2015 14:40:00 -0300 Subject: [PATCH] Interpret strings to "plugins" arg in pytest.main() as module names See #855 --- CHANGELOG | 5 +++++ _pytest/config.py | 5 ++++- testing/acceptance_test.py | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 26135beac1f..623e2740695 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -4,6 +4,11 @@ - fix issue856: consider --color parameter in all outputs (for example --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 Guido van Rossum, Matthias Bussonnier, Stefan Zimmermann and Thomas Kluyver. diff --git a/_pytest/config.py b/_pytest/config.py index a954a622229..ad944adc582 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -80,7 +80,10 @@ def _prepareconfig(args=None, plugins=None): try: if 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( pluginmanager=pluginmanager, args=args) except Exception: diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 614af6a3af4..fffb67e71d9 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -1,3 +1,4 @@ +import sys import py, pytest class TestGeneralUsage: @@ -370,6 +371,21 @@ def test_foo(invalid_fixture): "*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: def test_earlyinit(self, testdir):