def get_plugin_manager(): try: from _pytest.config import get_plugin_manager return get_plugin_manager() except ImportError: from _pytest.core import PluginManager return PluginManager(load=True)
def test_register_mismatch_method(self): pp = get_plugin_manager() class hello: def pytest_gurgel(self): pass pytest.raises(Exception, lambda: pp.register(hello()))
def test_register_mismatch_arg(self): pp = get_plugin_manager() class hello: def pytest_configure(self, asd): pass pytest.raises(Exception, lambda: pp.register(hello()))
def main(): _pluginmanager = get_plugin_manager() hook = _pluginmanager.hook try: config = hook.pytest_cmdline_parse(pluginmanager=_pluginmanager, args=args) exitstatus = hook.pytest_cmdline_main(config=config) except pytest.UsageError: e = sys.exc_info()[1] sys.stderr.write("ERROR: %s\n" % (e.args[0],)) exitstatus = 3 return exitstatus
def test_load_initial_conftest_last_ordering(testdir): from _pytest.config import get_plugin_manager pm = get_plugin_manager() class My: def pytest_load_initial_conftests(self): pass m = My() pm.register(m) l = pm.listattr("pytest_load_initial_conftests") assert l[-1].__module__ == "_pytest.capture" assert l[-2] == m.pytest_load_initial_conftests assert l[-3].__module__ == "_pytest.config"
def main(): _pluginmanager = get_plugin_manager() hook = _pluginmanager.hook try: config = hook.pytest_cmdline_parse(pluginmanager=_pluginmanager, args=args) exitstatus = hook.pytest_cmdline_main(config=config) except pytest.UsageError: e = sys.exc_info()[1] sys.stderr.write("ERROR: %s\n" % (e.args[0], )) exitstatus = 3 return exitstatus
def test_consider_module_import_module(self, testdir): mod = py.std.types.ModuleType("x") mod.pytest_plugins = "pytest_a" aplugin = testdir.makepyfile(pytest_a="#") pluginmanager = get_plugin_manager() reprec = testdir.getreportrecorder(pluginmanager) #syspath.prepend(aplugin.dirpath()) py.std.sys.path.insert(0, str(aplugin.dirpath())) pluginmanager.consider_module(mod) call = reprec.getcall(pluginmanager.hook.pytest_plugin_registered.name) assert call.plugin.__name__ == "pytest_a" # check that it is not registered twice pluginmanager.consider_module(mod) l = reprec.getcalls("pytest_plugin_registered") assert len(l) == 1
def test_register(self): pm = get_plugin_manager() class MyPlugin: pass my = MyPlugin() pm.register(my) assert pm.getplugins() my2 = MyPlugin() pm.register(my2) assert pm.getplugins()[-2:] == [my, my2] assert pm.isregistered(my) assert pm.isregistered(my2) pm.unregister(my) assert not pm.isregistered(my) assert pm.getplugins()[-1:] == [my2]
def main(): # This run causes `pytest_runtest_call` to fire args = [ "test-plugin.py", "-s", ] pytest.main(args) # This run will not fire `pytest_runtest_call` args += "-d --tx 4*popen//python=python3".split(' ') pytest.main(args) # Gather data about execution mgr = get_plugin_manager() myplugin = mgr.getplugin("myplugin") print("Collected:", myplugin.collected)
def _init_config(slave_options, slave_args): # Create a pytest Config based on options/args parsed in the master # This is a slightly modified form of _pytest.config.Config.fromdictargs # yaml is able to pack up the entire CmdOptions call from pytest, so # we can just set config.option to what was passed from the master in the slave_config yaml from _pytest import config pluginmanager = config.get_plugin_manager() config = pluginmanager.config config.args = slave_args config._preparse(config.args, addopts=False) config.option = slave_options # The master handles the result log, slaves shouldn't also write to it config.option['resultlog'] = None # Unset appliances to prevent the slaves from starting distributes tests :) config.option.appliances = [] for pluginarg in config.option.plugins: config.pluginmanager.consider_pluginarg(pluginarg) return config
def test_addhooks_conftestplugin(self, testdir): testdir.makepyfile(newhooks=""" def pytest_myhook(xyz): "new hook" """) conf = testdir.makeconftest(""" import sys ; sys.path.insert(0, '.') import newhooks def pytest_addhooks(pluginmanager): pluginmanager.addhooks(newhooks) def pytest_myhook(xyz): return xyz + 1 """) config = get_plugin_manager().config config._conftest.importconftest(conf) print(config.pluginmanager.getplugins()) res = config.hook.pytest_myhook(xyz=10) assert res == [11]
def test_addhooks_conftestplugin(self, testdir): newhooks = testdir.makepyfile(newhooks=""" def pytest_myhook(xyz): "new hook" """) conf = testdir.makeconftest(""" import sys ; sys.path.insert(0, '.') import newhooks def pytest_addhooks(pluginmanager): pluginmanager.addhooks(newhooks) def pytest_myhook(xyz): return xyz + 1 """) config = get_plugin_manager().config config._conftest.importconftest(conf) print(config.pluginmanager.getplugins()) res = config.hook.pytest_myhook(xyz=10) assert res == [11]
def test_hook_tracing(self): pm = get_plugin_manager() saveindent = [] class api1: x = 41 def pytest_plugin_registered(self, plugin): saveindent.append(pm.trace.root.indent) raise ValueError(42) l = [] pm.trace.root.setwriter(l.append) indent = pm.trace.root.indent p = api1() pm.register(p) assert pm.trace.root.indent == indent assert len(l) == 1 assert 'pytest_plugin_registered' in l[0] pytest.raises(ValueError, lambda: pm.register(api1())) assert pm.trace.root.indent == indent assert saveindent[0] > indent
set_parallel_mode from teamcity import pytest_plugin import os if __name__ == '__main__': path, targets, additional_args = parse_arguments() sys.argv += additional_args joined_targets = jb_patch_separator(targets, fs_glue="/", python_glue="::", fs_to_python_glue=".py::") # When file is launched in pytest it should be file.py: you can't provide it as bare module joined_targets = [t + ".py" if ":" not in t else t for t in joined_targets] sys.argv += [path] if path else joined_targets # plugin is discovered automatically in 3, but not in 2 # to prevent "plugin already registered" problem we check it first plugins_to_load = [] if not get_plugin_manager().hasplugin("pytest-teamcity"): if "pytest-teamcity" not in map(lambda e: e.name, iter_entry_points(group='pytest11', name=None)): plugins_to_load.append(pytest_plugin) args = sys.argv[1:] if "--jb-show-summary" in args: args.remove("--jb-show-summary") elif int(pytest.__version__.split('.')[0]) >= 6: args += ["--no-header", "--no-summary", "-q"] if JB_DISABLE_BUFFERING and "-s" not in args: args += ["-s"] jb_doc_args("pytest", args)
def test_register_mismatch_arg(self): pp = get_plugin_manager() class hello: def pytest_configure(self, asd): pass excinfo = pytest.raises(Exception, "pp.register(hello())")
def test_register_mismatch_method(self): pp = get_plugin_manager() class hello: def pytest_gurgel(self): pass pytest.raises(Exception, "pp.register(hello())")
def setstate(self, state): setattr(self, '_pluginmanager', get_plugin_manager())
from teamcity import pytest_plugin if __name__ == '__main__': real_prepare_config = config._prepareconfig path, targets, additional_args = parse_arguments() sys.argv += additional_args joined_targets = jb_patch_separator(targets, fs_glue="/", python_glue="::", fs_to_python_glue=".py::") # When file is launched in pytest it should be file.py: you can't provide it as bare module joined_targets = [t + ".py" if ":" not in t else t for t in joined_targets] sys.argv += [path] if path else joined_targets # plugin is discovered automatically in 3, but not in 2 # to prevent "plugin already registered" problem we check it first plugins_to_load = [] if not get_plugin_manager().hasplugin("pytest-teamcity"): if "pytest-teamcity" not in map(lambda e: e.name, iter_entry_points(group='pytest11', name=None)): plugins_to_load.append(pytest_plugin) args = sys.argv[1:] if JB_DISABLE_BUFFERING and "-s" not in args: args += ["-s"] jb_doc_args("pytest", args) # We need to preparse numprocesses because user may set it using ini file config_result = real_prepare_config(args, plugins_to_load) if getattr(config_result.option, "numprocesses", None): set_parallel_mode() config._prepareconfig = lambda _, __: config_result