diff --git a/py/test/config.py b/py/test/config.py index ef59ef44b..6512f6b78 100644 --- a/py/test/config.py +++ b/py/test/config.py @@ -76,6 +76,7 @@ class Config(object): def _preparse(self, args): self._conftest.setinitial(args) + self.pluginmanager.consider_preparse(args) self.pluginmanager.consider_env() self.pluginmanager.do_addoption(self._parser) diff --git a/py/test/pluginmanager.py b/py/test/pluginmanager.py index efdb9b225..c0185994b 100644 --- a/py/test/pluginmanager.py +++ b/py/test/pluginmanager.py @@ -47,6 +47,11 @@ class PluginManager(object): for spec in self._envlist("PYTEST_PLUGINS"): self.import_plugin(spec) + def consider_preparse(self, args): + for opt1,opt2 in zip(args, args[1:]): + if opt1 == "-p": + self.import_plugin(opt2) + def consider_conftest(self, conftestmodule): cls = getattr(conftestmodule, 'ConftestPlugin', None) if cls is not None and cls not in self.impname2plugin: diff --git a/py/test/testing/acceptance_test.py b/py/test/testing/acceptance_test.py index 8539f395a..8f107d877 100644 --- a/py/test/testing/acceptance_test.py +++ b/py/test/testing/acceptance_test.py @@ -17,6 +17,23 @@ class TestGeneralUsage: '*ERROR: hello' ]) + def test_config_preparse_plugin_option(self, testdir): + testdir.makepyfile(pytest_xyz=""" + class XyzPlugin: + def pytest_addoption(self, parser): + parser.addoption("--xyz", dest="xyz", action="store") + """) + testdir.makepyfile(test_one=""" + import py + def test_option(): + assert py.test.config.option.xyz == "123" + """) + result = testdir.runpytest("-p", "xyz", "--xyz=123") + assert result.ret == 0 + assert result.stdout.fnmatch_lines([ + '*1 passed*', + ]) + def test_basetemp(self, testdir): mytemp = testdir.tmpdir.mkdir("mytemp") p = testdir.makepyfile(""" diff --git a/py/test/testing/test_pluginmanager.py b/py/test/testing/test_pluginmanager.py index 6e349ff12..2b11e7ffb 100644 --- a/py/test/testing/test_pluginmanager.py +++ b/py/test/testing/test_pluginmanager.py @@ -8,6 +8,12 @@ class TestBootstrapping: monkeypatch.setitem(os.environ, 'PYTEST_PLUGINS', 'nonexistingmodule') py.test.raises(ImportError, "pluginmanager.consider_env()") + def test_preparse_args(self, monkeypatch): + pluginmanager = PluginManager() + py.test.raises(ImportError, """ + pluginmanager.consider_preparse(["xyz", "-p", "hello123"]) + """) + def test_consider_env_plugin_instantiation(self, testdir, monkeypatch): pluginmanager = PluginManager() testdir.syspathinsert()