def test_listattr(self): pluginmanager = PluginManager() class My2: x = 42 pluginmanager.register(My2()) assert not pluginmanager.listattr("hello") assert pluginmanager.listattr("x") == [42]
def test_consider_module(self, testdir): pluginmanager = PluginManager() testdir.syspathinsert() testdir.makepyfile(pytest_plug1="#") testdir.makepyfile(pytest_plug2="#") mod = py.std.new.module("temp") mod.pytest_plugins = ["pytest_plug1", "pytest_plug2"] pluginmanager.consider_module(mod) assert pluginmanager.getplugin("plug1").__name__ == "pytest_plug1" assert pluginmanager.getplugin("plug2").__name__ == "pytest_plug2"
def test_consider_env_plugin_instantiation(self, testdir, monkeypatch): pluginmanager = PluginManager() testdir.syspathinsert() testdir.makepyfile(pytest_xy123="#") monkeypatch.setitem(os.environ, 'PYTEST_PLUGINS', 'xy123') l1 = len(pluginmanager.getplugins()) pluginmanager.consider_env() l2 = len(pluginmanager.getplugins()) assert l2 == l1 + 1 assert pluginmanager.getplugin('pytest_xy123') pluginmanager.consider_env() l3 = len(pluginmanager.getplugins()) assert l2 == l3
def test_consider_module_import_module(self, testdir): mod = py.std.new.module("x") mod.pytest_plugins = "pytest_a" aplugin = testdir.makepyfile(pytest_a="#") pluginmanager = PluginManager() 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_mismatch_arg(self): pp = PluginManager() class hello: def pytest_configure(self, asd): pass excinfo = py.test.raises(pp.Error, "pp.register(hello())")
def test_register_mismatch_method(self): pp = PluginManager() class hello: def pytest_gurgel(self): pass py.test.raises(pp.Error, "pp.register(hello())")
def test_import_plugin_importname(self, testdir): pluginmanager = PluginManager() py.test.raises(ImportError, 'pluginmanager.import_plugin("x.y")') py.test.raises(ImportError, 'pluginmanager.import_plugin("pytest_x.y")') reset = testdir.syspathinsert() pluginname = "pytest_hello" testdir.makepyfile(**{pluginname: ""}) pluginmanager.import_plugin("hello") len1 = len(pluginmanager.getplugins()) pluginmanager.import_plugin("pytest_hello") len2 = len(pluginmanager.getplugins()) assert len1 == len2 plugin1 = pluginmanager.getplugin("pytest_hello") assert plugin1.__name__.endswith('pytest_hello') plugin2 = pluginmanager.getplugin("hello") assert plugin2 is plugin1
def test_register_imported_modules(self): pp = PluginManager() mod = py.std.new.module("x.y.pytest_hello") pp.register(mod) assert pp.isregistered(mod) assert pp.getplugins() == [mod] py.test.raises(AssertionError, "pp.register(mod)") mod2 = py.std.new.module("pytest_hello") #pp.register(mod2) # double registry py.test.raises(AssertionError, "pp.register(mod)") #assert not pp.isregistered(mod2) assert pp.getplugins() == [mod] # does not actually modify plugins
def test_preparse_args(self): pluginmanager = PluginManager() py.test.raises( ImportError, """ pluginmanager.consider_preparse(["xyz", "-p", "hello123"]) """)
def test_consider_conftest_deps(self, testdir): mod = testdir.makepyfile("pytest_plugins='xyz'").pyimport() pp = PluginManager() py.test.raises(ImportError, "pp.consider_conftest(mod)")
def test_registry(self): pp = PluginManager() class A: pass a1, a2 = A(), A() pp.register(a1) assert pp.isregistered(a1) pp.register(a2) assert pp.isregistered(a2) assert pp.getplugins() == [a1, a2] pp.unregister(a1) assert not pp.isregistered(a1) pp.unregister(a2) assert not pp.isregistered(a2)
def test_consider_conftest_deprecated(self, testdir): pp = PluginManager() mod = testdir.makepyfile("class ConftestPlugin: pass").pyimport() call = py.test.raises(ValueError, pp.consider_conftest, mod)
def test_consider_env_fails_to_import(self, monkeypatch): pluginmanager = PluginManager() monkeypatch.setenv('PYTEST_PLUGINS', 'nonexisting', prepend=",") py.test.raises(ImportError, "pluginmanager.consider_env()")