[svn r63808] * refinements/renames to new PluginAPI

* have pytest_runner start to use it, passes the main test

--HG--
branch : trunk
This commit is contained in:
hpk 2009-04-07 22:46:50 +02:00
parent 50664c1e17
commit f14fc582e9
8 changed files with 31 additions and 21 deletions

View File

@ -25,8 +25,8 @@ version = "1.0.0b1"
initpkg(__name__, initpkg(__name__,
description = "pylib and py.test: agile development and test support library", description = "pylib and py.test: agile development and test support library",
revision = int('$LastChangedRevision: 63806 $'.split(':')[1][:-1]), revision = int('$LastChangedRevision: 63808 $'.split(':')[1][:-1]),
lastchangedate = '$LastChangedDate: 2009-04-07 22:22:52 +0200 (Tue, 07 Apr 2009) $', lastchangedate = '$LastChangedDate: 2009-04-07 22:46:50 +0200 (Tue, 07 Apr 2009) $',
version = version, version = version,
url = "http://pylib.org", url = "http://pylib.org",
download_url = "http://codespeak.net/py/%s/download.html" % version, download_url = "http://codespeak.net/py/%s/download.html" % version,
@ -57,7 +57,7 @@ initpkg(__name__,
'_com.PyPlugins' : ('./_com.py', 'PyPlugins'), '_com.PyPlugins' : ('./_com.py', 'PyPlugins'),
'_com.MultiCall' : ('./_com.py', 'MultiCall'), '_com.MultiCall' : ('./_com.py', 'MultiCall'),
'_com.pyplugins' : ('./_com.py', 'pyplugins'), '_com.pyplugins' : ('./_com.py', 'pyplugins'),
'_com.MultiAPI' : ('./_com.py', 'MultiAPI'), '_com.PluginAPI' : ('./_com.py', 'PluginAPI'),
# py lib cmdline tools # py lib cmdline tools
'cmdline.pytest' : ('./cmdline/pytest.py', 'main',), 'cmdline.pytest' : ('./cmdline/pytest.py', 'main',),

View File

@ -159,14 +159,16 @@ class PyPlugins:
MultiCall(self.listattr("pyevent"), eventname, args, kwargs).execute() MultiCall(self.listattr("pyevent"), eventname, args, kwargs).execute()
class MultiAPI: class PluginAPI:
def __init__(self, apiclass, plugins, prefix): def __init__(self, apiclass, plugins):
for fullname in vars(apiclass): self._apiclass = apiclass
if fullname[:2] != "__": self._plugins = plugins
assert fullname.startswith(prefix) for name in vars(apiclass):
name = fullname[len(prefix):] if name[:2] != "__":
mm = CallMaker(plugins, fullname) mm = CallMaker(plugins, name)
setattr(self, name, mm) setattr(self, name, mm)
def __repr__(self):
return "<PluginAPI %r %r>" %(self._apiclass, self._plugins)
class CallMaker: class CallMaker:
def __init__(self, plugins, name): def __init__(self, plugins, name):

View File

@ -2,7 +2,7 @@
import py import py
import os import os
from py._com import PyPlugins, MultiCall from py._com import PyPlugins, MultiCall
from py._com import MultiAPI from py._com import PluginAPI
pytest_plugins = "xfail" pytest_plugins = "xfail"
@ -254,15 +254,14 @@ class TestMulticallMaker:
def test_happypath(self): def test_happypath(self):
plugins = PyPlugins() plugins = PyPlugins()
class Api: class Api:
def xyz_hello(self, arg): def hello(self, arg):
pass pass
mcm = MultiAPI(apiclass=Api, plugins=plugins, prefix="xyz_") mcm = PluginAPI(apiclass=Api, plugins=plugins)
assert hasattr(mcm, 'hello') assert hasattr(mcm, 'hello')
assert repr(mcm.hello).find("xyz_hello") != -1 assert repr(mcm.hello).find("hello") != -1
assert not hasattr(mcm, 'xyz_hello')
class Plugin: class Plugin:
def xyz_hello(self, arg): def hello(self, arg):
return arg + 1 return arg + 1
plugins.register(Plugin()) plugins.register(Plugin())
l = mcm.hello(3) l = mcm.hello(3)

View File

@ -42,6 +42,7 @@ class Config(object):
self.pytestplugins = pytestplugins self.pytestplugins = pytestplugins
self._conftest = Conftest(onimport=self._onimportconftest) self._conftest = Conftest(onimport=self._onimportconftest)
self._setupstate = SetupState() self._setupstate = SetupState()
self.api = pytestplugins._getapi()
def _onimportconftest(self, conftestmodule): def _onimportconftest(self, conftestmodule):
self.trace("loaded conftestmodule %r" %(conftestmodule,)) self.trace("loaded conftestmodule %r" %(conftestmodule,))

View File

@ -39,6 +39,10 @@ class PluginHooks:
def pytest_itemrun(self, item, pdb=None): def pytest_itemrun(self, item, pdb=None):
""" run given test item and return test report. """ """ run given test item and return test report. """
def pytest_item_runtest_finished(self, item, excinfo, outerr):
""" called in-process after runtest() returned. """
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# runtest related hooks # runtest related hooks
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------

View File

@ -17,7 +17,7 @@ class DefaultPlugin:
from py.__.test import runner from py.__.test import runner
return runner.ItemTestReport(item, excinfo, when, outerr) return runner.ItemTestReport(item, excinfo, when, outerr)
def pyevent__item_runtest_finished(self, item, excinfo, outerr): def pytest_item_runtest_finished(self, item, excinfo, outerr):
from py.__.test import runner from py.__.test import runner
rep = runner.ItemTestReport(item, excinfo, "execute", outerr) rep = runner.ItemTestReport(item, excinfo, "execute", outerr)
item.config.pytestplugins.notify("itemtestreport", rep) item.config.pytestplugins.notify("itemtestreport", rep)

View File

@ -1,4 +1,5 @@
import py import py
from outcome import Skipped
class RunnerPlugin: class RunnerPlugin:
def pytest_configure(self, config): def pytest_configure(self, config):
@ -15,7 +16,7 @@ class RunnerPlugin:
item.config.pytestplugins.notify("itemsetupreport", rep) item.config.pytestplugins.notify("itemsetupreport", rep)
else: else:
call = item.config.guardedcall(lambda: item.runtest()) call = item.config.guardedcall(lambda: item.runtest())
item.config.mc.pytest_item_runtest_finished( item.config.api.pytest_item_runtest_finished(
item=item, excinfo=call.excinfo, outerr=call.outerr) item=item, excinfo=call.excinfo, outerr=call.outerr)
call = item.config.guardedcall(lambda: self.teardown_exact(item)) call = item.config.guardedcall(lambda: self.teardown_exact(item))
if call.excinfo: if call.excinfo:
@ -203,17 +204,15 @@ class TestSetupState:
assert not hasattr(item.parent.obj, 'x') assert not hasattr(item.parent.obj, 'x')
class TestRunnerPlugin: class TestRunnerPlugin:
disabled = True
def test_pytest_item_setup_and_runtest(self, testdir): def test_pytest_item_setup_and_runtest(self, testdir):
item = testdir.getitem("""def test_func(): pass""") item = testdir.getitem("""def test_func(): pass""")
plugin = RunnerPlugin() plugin = RunnerPlugin()
plugin.pytest_configure(item.config) plugin.pytest_configure(item.config)
sorter = testdir.geteventrecorder(item.config) sorter = testdir.geteventrecorder(item.config)
plugin.pytest_item_setup_and_runtest(item) plugin.pytest_item_setup_and_runtest(item)
rep = sorter.getreport("itemtestreport") rep = sorter.getcall("itemtestreport").rep
assert rep.passed assert rep.passed
class TestSetupEvents: class TestSetupEvents:
disabled = True disabled = True

View File

@ -2,6 +2,7 @@
handling py.test plugins. handling py.test plugins.
""" """
import py import py
from py.__.test.plugin import api
class PytestPlugins(object): class PytestPlugins(object):
def __init__(self, pyplugins=None): def __init__(self, pyplugins=None):
@ -11,6 +12,10 @@ class PytestPlugins(object):
self.MultiCall = self.pyplugins.MultiCall self.MultiCall = self.pyplugins.MultiCall
self._plugins = {} self._plugins = {}
def _getapi(self):
return py._com.PluginAPI(apiclass=api.PluginHooks,
plugins=self.pyplugins)
def register(self, plugin): def register(self, plugin):
self.pyplugins.register(plugin) self.pyplugins.register(plugin)
def unregister(self, plugin): def unregister(self, plugin):