[svn r63868] move towards having funcarg setup be part of setup()

--HG--
branch : trunk
This commit is contained in:
hpk 2009-04-08 19:42:23 +02:00
parent c6606d9d8f
commit 92a06c3787
2 changed files with 26 additions and 26 deletions

View File

@ -327,6 +327,7 @@ class Function(FunctionMixin, py.test.collect.Item):
super(Function, self).__init__(name, parent, config=config)
self._finalizers = []
self._args = args
self.funcargs = {}
if callobj is not _dummy:
self._obj = callobj
@ -348,13 +349,15 @@ class Function(FunctionMixin, py.test.collect.Item):
def runtest(self):
""" execute the given test function. """
if not self._deprecated_testexecution():
kw = self.lookup_allargs()
self.setupargs() # XXX move to setup() / consider funcargs plugin
ret = self.config.api.pytest_pyfunc_call(
pyfuncitem=self, args=self._args, kwargs=kw)
pyfuncitem=self, args=self._args, kwargs=self.funcargs)
def lookup_allargs(self):
kwargs = {}
if not self._args:
def setupargs(self):
if self._args:
# generator case: we don't do anything then
pass
else:
# standard Python Test function/method case
funcobj = self.obj
startindex = getattr(funcobj, 'im_self', None) and 1 or 0
@ -363,16 +366,13 @@ class Function(FunctionMixin, py.test.collect.Item):
if i < startindex:
continue
try:
kwargs[argname] = self.lookup_onearg(argname)
self.funcargs[argname] = self.lookup_onearg(argname)
except LookupError, e:
numdefaults = len(funcobj.func_defaults or ())
if i + numdefaults >= len(argnames):
continue # continue # seems that our args have defaults
else:
raise
else:
pass # XXX lookup of arguments for yielded/generated tests as well ?
return kwargs
def lookup_onearg(self, argname):
prefix = "pytest_funcarg__"

View File

@ -250,7 +250,7 @@ class TestFunction:
return 42
""")
item = testdir.getitem("def test_func(some): pass")
exc = py.test.raises(LookupError, "item.lookup_allargs()")
exc = py.test.raises(LookupError, "item.setupargs()")
s = str(exc.value)
assert s.find("something") != -1
@ -260,8 +260,8 @@ class TestFunction:
def pytest_funcarg__some(self, pyfuncitem):
return pyfuncitem.name
item.config.pytestplugins.register(Provider())
kw = item.lookup_allargs()
assert len(kw) == 1
item.setupargs()
assert len(item.funcargs) == 1
def test_funcarg_lookup_default_gets_overriden(self, testdir):
item = testdir.getitem("def test_func(some=42, other=13): pass")
@ -269,9 +269,9 @@ class TestFunction:
def pytest_funcarg__other(self, pyfuncitem):
return pyfuncitem.name
item.config.pytestplugins.register(Provider())
kw = item.lookup_allargs()
assert len(kw) == 1
name, value = kw.popitem()
item.setupargs()
assert len(item.funcargs) == 1
name, value = item.funcargs.popitem()
assert name == "other"
assert value == item.name
@ -283,10 +283,10 @@ class TestFunction:
def pytest_funcarg__other(self, pyfuncitem):
return 42
item.config.pytestplugins.register(Provider())
kw = item.lookup_allargs()
assert len(kw) == 2
assert kw['some'] == "test_func"
assert kw['other'] == 42
item.setupargs()
assert len(item.funcargs) == 2
assert item.funcargs['some'] == "test_func"
assert item.funcargs['other'] == 42
def test_funcarg_addfinalizer(self, testdir):
item = testdir.getitem("def test_func(some): pass")
@ -296,9 +296,9 @@ class TestFunction:
pyfuncitem.addfinalizer(lambda: l.append(42))
return 3
item.config.pytestplugins.register(Provider())
kw = item.lookup_allargs()
assert len(kw) == 1
assert kw['some'] == 3
item.setupargs()
assert len(item.funcargs) == 1
assert item.funcargs['some'] == 3
assert len(l) == 0
item.teardown()
assert len(l) == 1
@ -318,10 +318,10 @@ class TestFunction:
item1, item2 = testdir.genitems([modcol])
modcol.setup()
assert modcol.config.pytestplugins.isregistered(modcol.obj)
kwargs = item1.lookup_allargs()
assert kwargs['something'] == "test_method"
kwargs = item2.lookup_allargs()
assert kwargs['something'] == "test_func"
item1.setupargs()
assert item1.funcargs['something'] == "test_method"
item2.setupargs()
assert item2.funcargs['something'] == "test_func"
modcol.teardown()
assert not modcol.config.pytestplugins.isregistered(modcol.obj)