[svn r63905] * have pytest pluginmanager do multicalls for registration

* more renaming

--HG--
branch : trunk
This commit is contained in:
hpk 2009-04-09 17:03:58 +02:00
parent e75b1b9ee6
commit f8fc229917
5 changed files with 81 additions and 70 deletions

View File

@ -75,11 +75,9 @@ class Registry:
def register(self, plugin): def register(self, plugin):
assert not isinstance(plugin, str) assert not isinstance(plugin, str)
self.call_each("pytest_plugin_registered", plugin)
self._plugins.append(plugin) self._plugins.append(plugin)
def unregister(self, plugin): def unregister(self, plugin):
self.call_each("pytest_plugin_unregistered", plugin)
self._plugins.remove(plugin) self._plugins.remove(plugin)
def isregistered(self, plugin): def isregistered(self, plugin):

View File

@ -14,10 +14,25 @@ class PluginHooks:
and all plugins and initial conftest files been loaded. and all plugins and initial conftest files been loaded.
``config`` provides access to all such configuration values. ``config`` provides access to all such configuration values.
""" """
def pytest_unconfigure(self, config): def pytest_unconfigure(self, config):
""" called before test process is exited. """ called before test process is exited.
""" """
# ------------------------------------------------------------------------------
# test Session related hooks
# ------------------------------------------------------------------------------
def pytest_testrunstart(self):
""" whole test run starts. """
def pytest_testrunfinish(self, exitstatus, excrepr=None):
""" whole test run finishes. """
def pytest_deselected(self, items):
""" collected items that were deselected (by keyword). """
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# collection hooks # collection hooks
@ -100,30 +115,27 @@ class PluginHooks:
def pytest_plugin_unregistered(self, plugin): def pytest_plugin_unregistered(self, plugin):
""" a py lib plugin got unregistered. """ """ a py lib plugin got unregistered. """
def pytest_internalerror(self, excrepr):
""" called for internal errors. """
def pytest_trace(self, category, msg):
""" called for debug info. """
# ------------------------------------------------------------------------------
# distributed testing
# ------------------------------------------------------------------------------
def pytest_testnodeready(self, node): def pytest_testnodeready(self, node):
""" Test Node is ready to operate. """ """ Test Node is ready to operate. """
def pytest_testnodedown(self, node, error): def pytest_testnodedown(self, node, error):
""" Test Node is down. """ """ Test Node is down. """
def pytest_testrunstart(self):
""" whole test run starts. """
def pytest_testrunfinish(self, exitstatus, excrepr=None):
""" whole test run finishes. """
def pytest_deselected(self, items):
""" collected items that were deselected (by keyword). """
def pytest_rescheduleitems(self, items): def pytest_rescheduleitems(self, items):
""" reschedule Items from a node that went down. """ """ reschedule Items from a node that went down. """
def pytest_looponfailinfo(self, failreports, rootdirs): def pytest_looponfailinfo(self, failreports, rootdirs):
""" info for repeating failing tests. """ """ info for repeating failing tests. """
def pytest_internalerror(self, excrepr):
""" called for internal errors. """
def pytest_trace(self, category, msg):
""" called for debug info. """

View File

@ -21,9 +21,6 @@ class PytesterPlugin:
tmptestdir = TmpTestdir(pyfuncitem) tmptestdir = TmpTestdir(pyfuncitem)
return tmptestdir return tmptestdir
#def pytest_funcarg__EventRecorder(self, pyfuncitem):
# return EventRecorder
def pytest_funcarg__eventrecorder(self, pyfuncitem): def pytest_funcarg__eventrecorder(self, pyfuncitem):
evrec = EventRecorder(py._com.comregistry) evrec = EventRecorder(py._com.comregistry)
pyfuncitem.addfinalizer(lambda: evrec.comregistry.unregister(evrec)) pyfuncitem.addfinalizer(lambda: evrec.comregistry.unregister(evrec))

View File

@ -17,9 +17,13 @@ class PluginManager(object):
registry=self.comregistry) registry=self.comregistry)
def register(self, plugin): def register(self, plugin):
self.api.pytest_plugin_registered(plugin=plugin)
self.comregistry.register(plugin) self.comregistry.register(plugin)
def unregister(self, plugin): def unregister(self, plugin):
self.api.pytest_plugin_unregistered(plugin=plugin)
self.comregistry.unregister(plugin) self.comregistry.unregister(plugin)
def isregistered(self, plugin): def isregistered(self, plugin):
return self.comregistry.isregistered(plugin) return self.comregistry.isregistered(plugin)
@ -63,7 +67,7 @@ class PluginManager(object):
if modname in self.impname2plugin: if modname in self.impname2plugin:
return return
mod = importplugin(modname) mod = importplugin(modname)
plugin = registerplugin(self.comregistry.register, mod, clsname) plugin = registerplugin(self.register, mod, clsname)
self.impname2plugin[modname] = plugin self.impname2plugin[modname] = plugin
self.consider_module(mod) self.consider_module(mod)
# #

View File

@ -4,22 +4,22 @@ from py.__.test.pluginmanager import registerplugin, importplugin
class TestBootstrapping: class TestBootstrapping:
def test_consider_env_fails_to_import(self, monkeypatch): def test_consider_env_fails_to_import(self, monkeypatch):
plugins = PluginManager() pluginmanager = PluginManager()
monkeypatch.setitem(os.environ, 'PYTEST_PLUGINS', 'nonexistingmodule') monkeypatch.setitem(os.environ, 'PYTEST_PLUGINS', 'nonexistingmodule')
py.test.raises(ImportError, "plugins.consider_env()") py.test.raises(ImportError, "pluginmanager.consider_env()")
def test_consider_env_plugin_instantiation(self, testdir, monkeypatch): def test_consider_env_plugin_instantiation(self, testdir, monkeypatch):
plugins = PluginManager() pluginmanager = PluginManager()
testdir.syspathinsert() testdir.syspathinsert()
testdir.makepyfile(pytest_xy123="class Xy123Plugin: pass") testdir.makepyfile(pytest_xy123="class Xy123Plugin: pass")
monkeypatch.setitem(os.environ, 'PYTEST_PLUGINS', 'xy123') monkeypatch.setitem(os.environ, 'PYTEST_PLUGINS', 'xy123')
l1 = len(plugins.getplugins()) l1 = len(pluginmanager.getplugins())
plugins.consider_env() pluginmanager.consider_env()
l2 = len(plugins.getplugins()) l2 = len(pluginmanager.getplugins())
assert l2 == l1 + 1 assert l2 == l1 + 1
assert plugins.getplugin('pytest_xy123') assert pluginmanager.getplugin('pytest_xy123')
plugins.consider_env() pluginmanager.consider_env()
l3 = len(plugins.getplugins()) l3 = len(pluginmanager.getplugins())
assert l2 == l3 assert l2 == l3
def test_pluginmanager_ENV_startup(self, testdir, monkeypatch): def test_pluginmanager_ENV_startup(self, testdir, monkeypatch):
@ -38,9 +38,9 @@ class TestBootstrapping:
extra = result.stdout.fnmatch_lines(["*1 passed in*"]) extra = result.stdout.fnmatch_lines(["*1 passed in*"])
def test_import_plugin_importname(self, testdir): def test_import_plugin_importname(self, testdir):
plugins = PluginManager() pluginmanager = PluginManager()
py.test.raises(ImportError, 'plugins.import_plugin("x.y")') py.test.raises(ImportError, 'pluginmanager.import_plugin("x.y")')
py.test.raises(ImportError, 'plugins.import_plugin("pytest_x.y")') py.test.raises(ImportError, 'pluginmanager.import_plugin("pytest_x.y")')
reset = testdir.syspathinsert() reset = testdir.syspathinsert()
pluginname = "pytest_hello" pluginname = "pytest_hello"
@ -48,41 +48,41 @@ class TestBootstrapping:
class HelloPlugin: class HelloPlugin:
pass pass
"""}) """})
plugins.import_plugin("hello") pluginmanager.import_plugin("hello")
len1 = len(plugins.getplugins()) len1 = len(pluginmanager.getplugins())
plugins.import_plugin("pytest_hello") pluginmanager.import_plugin("pytest_hello")
len2 = len(plugins.getplugins()) len2 = len(pluginmanager.getplugins())
assert len1 == len2 assert len1 == len2
plugin1 = plugins.getplugin("pytest_hello") plugin1 = pluginmanager.getplugin("pytest_hello")
assert plugin1.__class__.__name__ == 'HelloPlugin' assert plugin1.__class__.__name__ == 'HelloPlugin'
plugin2 = plugins.getplugin("hello") plugin2 = pluginmanager.getplugin("hello")
assert plugin2 is plugin1 assert plugin2 is plugin1
def test_consider_module(self, testdir): def test_consider_module(self, testdir):
plugins = PluginManager() pluginmanager = PluginManager()
testdir.syspathinsert() testdir.syspathinsert()
testdir.makepyfile(pytest_plug1="class Plug1Plugin: pass") testdir.makepyfile(pytest_plug1="class Plug1Plugin: pass")
testdir.makepyfile(pytest_plug2="class Plug2Plugin: pass") testdir.makepyfile(pytest_plug2="class Plug2Plugin: pass")
mod = py.std.new.module("temp") mod = py.std.new.module("temp")
mod.pytest_plugins = ["pytest_plug1", "pytest_plug2"] mod.pytest_plugins = ["pytest_plug1", "pytest_plug2"]
plugins.consider_module(mod) pluginmanager.consider_module(mod)
assert plugins.getplugin("plug1").__class__.__name__ == "Plug1Plugin" assert pluginmanager.getplugin("plug1").__class__.__name__ == "Plug1Plugin"
assert plugins.getplugin("plug2").__class__.__name__ == "Plug2Plugin" assert pluginmanager.getplugin("plug2").__class__.__name__ == "Plug2Plugin"
def test_consider_module_import_module(self, testdir): def test_consider_module_import_module(self, testdir):
mod = py.std.new.module("x") mod = py.std.new.module("x")
mod.pytest_plugins = "pytest_a" mod.pytest_plugins = "pytest_a"
aplugin = testdir.makepyfile(pytest_a="""class APlugin: pass""") aplugin = testdir.makepyfile(pytest_a="""class APlugin: pass""")
plugins = PluginManager() pluginmanager = PluginManager()
sorter = testdir.geteventrecorder(plugins) sorter = testdir.geteventrecorder(pluginmanager)
#syspath.prepend(aplugin.dirpath()) #syspath.prepend(aplugin.dirpath())
py.std.sys.path.insert(0, str(aplugin.dirpath())) py.std.sys.path.insert(0, str(aplugin.dirpath()))
plugins.consider_module(mod) pluginmanager.consider_module(mod)
call = sorter.getcall("plugin_registered") call = sorter.getcall(pluginmanager.api.pytest_plugin_registered.name)
assert call.plugin.__class__.__name__ == "APlugin" assert call.plugin.__class__.__name__ == "APlugin"
# check that it is not registered twice # check that it is not registered twice
plugins.consider_module(mod) pluginmanager.consider_module(mod)
l = sorter.getcalls("plugin_registered") l = sorter.getcalls("plugin_registered")
assert len(l) == 1 assert len(l) == 1
@ -202,26 +202,26 @@ class TestPytestPluginInteractions:
# lower level API # lower level API
def test_getfirst(self): def test_getfirst(self):
plugins = PluginManager() pluginmanager = PluginManager()
class My1: class My1:
x = 1 x = 1
assert plugins.getfirst("x") is None assert pluginmanager.getfirst("x") is None
plugins.register(My1()) pluginmanager.register(My1())
assert plugins.getfirst("x") == 1 assert pluginmanager.getfirst("x") == 1
def test_call_each(self): def test_call_each(self):
plugins = PluginManager() pluginmanager = PluginManager()
class My: class My:
def method(self, arg): def method(self, arg):
pass pass
plugins.register(My()) pluginmanager.register(My())
py.test.raises(TypeError, 'plugins.call_each("method")') py.test.raises(TypeError, 'pluginmanager.call_each("method")')
l = plugins.call_each("method", arg=42) l = pluginmanager.call_each("method", arg=42)
assert l == [] assert l == []
py.test.raises(TypeError, 'plugins.call_each("method", arg=42, s=13)') py.test.raises(TypeError, 'pluginmanager.call_each("method", arg=42, s=13)')
def test_call_firstresult(self): def test_call_firstresult(self):
plugins = PluginManager() pluginmanager = PluginManager()
class My1: class My1:
def method(self): def method(self):
pass pass
@ -231,22 +231,22 @@ class TestPytestPluginInteractions:
class My3: class My3:
def method(self): def method(self):
return None return None
assert plugins.call_firstresult("method") is None assert pluginmanager.call_firstresult("method") is None
assert plugins.call_firstresult("methodnotexists") is None assert pluginmanager.call_firstresult("methodnotexists") is None
plugins.register(My1()) pluginmanager.register(My1())
assert plugins.call_firstresult("method") is None assert pluginmanager.call_firstresult("method") is None
plugins.register(My2()) pluginmanager.register(My2())
assert plugins.call_firstresult("method") == True assert pluginmanager.call_firstresult("method") == True
plugins.register(My3()) pluginmanager.register(My3())
assert plugins.call_firstresult("method") == True assert pluginmanager.call_firstresult("method") == True
def test_listattr(self): def test_listattr(self):
plugins = PluginManager() pluginmanager = PluginManager()
class My2: class My2:
x = 42 x = 42
plugins.register(My2()) pluginmanager.register(My2())
assert not plugins.listattr("hello") assert not pluginmanager.listattr("hello")
assert plugins.listattr("x") == [42] assert pluginmanager.listattr("x") == [42]
@py.test.mark(xfail="implement setupcall") @py.test.mark(xfail="implement setupcall")
def test_call_setup_participants(self, testdir): def test_call_setup_participants(self, testdir):