Exemplo n.º 1
0
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)
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
    def test_register_mismatch_method(self):
        pp = get_plugin_manager()

        class hello:
            def pytest_gurgel(self):
                pass

        pytest.raises(Exception, lambda: pp.register(hello()))
Exemplo n.º 4
0
    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()))
Exemplo n.º 5
0
 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
Exemplo n.º 6
0
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"
Exemplo n.º 7
0
 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
Exemplo n.º 8
0
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"
Exemplo n.º 9
0
    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
Exemplo n.º 10
0
    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]
Exemplo n.º 11
0
    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
Exemplo n.º 12
0
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)
Exemplo n.º 13
0
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
Exemplo n.º 14
0
    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]
Exemplo n.º 15
0
 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]
Exemplo n.º 16
0
 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]
Exemplo n.º 17
0
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
Exemplo n.º 18
0
    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
Exemplo n.º 19
0
    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
Exemplo n.º 20
0
  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)
Exemplo n.º 21
0
 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())")
Exemplo n.º 22
0
 def test_register_mismatch_method(self):
     pp = get_plugin_manager()
     class hello:
         def pytest_gurgel(self):
             pass
     pytest.raises(Exception, "pp.register(hello())")
Exemplo n.º 23
0
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