[svn r63868] move towards having funcarg setup be part of setup()
--HG-- branch : trunk
This commit is contained in:
parent
c6606d9d8f
commit
92a06c3787
|
@ -327,6 +327,7 @@ class Function(FunctionMixin, py.test.collect.Item):
|
||||||
super(Function, self).__init__(name, parent, config=config)
|
super(Function, self).__init__(name, parent, config=config)
|
||||||
self._finalizers = []
|
self._finalizers = []
|
||||||
self._args = args
|
self._args = args
|
||||||
|
self.funcargs = {}
|
||||||
if callobj is not _dummy:
|
if callobj is not _dummy:
|
||||||
self._obj = callobj
|
self._obj = callobj
|
||||||
|
|
||||||
|
@ -348,13 +349,15 @@ class Function(FunctionMixin, py.test.collect.Item):
|
||||||
def runtest(self):
|
def runtest(self):
|
||||||
""" execute the given test function. """
|
""" execute the given test function. """
|
||||||
if not self._deprecated_testexecution():
|
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(
|
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):
|
def setupargs(self):
|
||||||
kwargs = {}
|
if self._args:
|
||||||
if not self._args:
|
# generator case: we don't do anything then
|
||||||
|
pass
|
||||||
|
else:
|
||||||
# standard Python Test function/method case
|
# standard Python Test function/method case
|
||||||
funcobj = self.obj
|
funcobj = self.obj
|
||||||
startindex = getattr(funcobj, 'im_self', None) and 1 or 0
|
startindex = getattr(funcobj, 'im_self', None) and 1 or 0
|
||||||
|
@ -363,16 +366,13 @@ class Function(FunctionMixin, py.test.collect.Item):
|
||||||
if i < startindex:
|
if i < startindex:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
kwargs[argname] = self.lookup_onearg(argname)
|
self.funcargs[argname] = self.lookup_onearg(argname)
|
||||||
except LookupError, e:
|
except LookupError, e:
|
||||||
numdefaults = len(funcobj.func_defaults or ())
|
numdefaults = len(funcobj.func_defaults or ())
|
||||||
if i + numdefaults >= len(argnames):
|
if i + numdefaults >= len(argnames):
|
||||||
continue # continue # seems that our args have defaults
|
continue # continue # seems that our args have defaults
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
else:
|
|
||||||
pass # XXX lookup of arguments for yielded/generated tests as well ?
|
|
||||||
return kwargs
|
|
||||||
|
|
||||||
def lookup_onearg(self, argname):
|
def lookup_onearg(self, argname):
|
||||||
prefix = "pytest_funcarg__"
|
prefix = "pytest_funcarg__"
|
||||||
|
|
|
@ -250,7 +250,7 @@ class TestFunction:
|
||||||
return 42
|
return 42
|
||||||
""")
|
""")
|
||||||
item = testdir.getitem("def test_func(some): pass")
|
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)
|
s = str(exc.value)
|
||||||
assert s.find("something") != -1
|
assert s.find("something") != -1
|
||||||
|
|
||||||
|
@ -260,8 +260,8 @@ class TestFunction:
|
||||||
def pytest_funcarg__some(self, pyfuncitem):
|
def pytest_funcarg__some(self, pyfuncitem):
|
||||||
return pyfuncitem.name
|
return pyfuncitem.name
|
||||||
item.config.pytestplugins.register(Provider())
|
item.config.pytestplugins.register(Provider())
|
||||||
kw = item.lookup_allargs()
|
item.setupargs()
|
||||||
assert len(kw) == 1
|
assert len(item.funcargs) == 1
|
||||||
|
|
||||||
def test_funcarg_lookup_default_gets_overriden(self, testdir):
|
def test_funcarg_lookup_default_gets_overriden(self, testdir):
|
||||||
item = testdir.getitem("def test_func(some=42, other=13): pass")
|
item = testdir.getitem("def test_func(some=42, other=13): pass")
|
||||||
|
@ -269,9 +269,9 @@ class TestFunction:
|
||||||
def pytest_funcarg__other(self, pyfuncitem):
|
def pytest_funcarg__other(self, pyfuncitem):
|
||||||
return pyfuncitem.name
|
return pyfuncitem.name
|
||||||
item.config.pytestplugins.register(Provider())
|
item.config.pytestplugins.register(Provider())
|
||||||
kw = item.lookup_allargs()
|
item.setupargs()
|
||||||
assert len(kw) == 1
|
assert len(item.funcargs) == 1
|
||||||
name, value = kw.popitem()
|
name, value = item.funcargs.popitem()
|
||||||
assert name == "other"
|
assert name == "other"
|
||||||
assert value == item.name
|
assert value == item.name
|
||||||
|
|
||||||
|
@ -283,10 +283,10 @@ class TestFunction:
|
||||||
def pytest_funcarg__other(self, pyfuncitem):
|
def pytest_funcarg__other(self, pyfuncitem):
|
||||||
return 42
|
return 42
|
||||||
item.config.pytestplugins.register(Provider())
|
item.config.pytestplugins.register(Provider())
|
||||||
kw = item.lookup_allargs()
|
item.setupargs()
|
||||||
assert len(kw) == 2
|
assert len(item.funcargs) == 2
|
||||||
assert kw['some'] == "test_func"
|
assert item.funcargs['some'] == "test_func"
|
||||||
assert kw['other'] == 42
|
assert item.funcargs['other'] == 42
|
||||||
|
|
||||||
def test_funcarg_addfinalizer(self, testdir):
|
def test_funcarg_addfinalizer(self, testdir):
|
||||||
item = testdir.getitem("def test_func(some): pass")
|
item = testdir.getitem("def test_func(some): pass")
|
||||||
|
@ -296,9 +296,9 @@ class TestFunction:
|
||||||
pyfuncitem.addfinalizer(lambda: l.append(42))
|
pyfuncitem.addfinalizer(lambda: l.append(42))
|
||||||
return 3
|
return 3
|
||||||
item.config.pytestplugins.register(Provider())
|
item.config.pytestplugins.register(Provider())
|
||||||
kw = item.lookup_allargs()
|
item.setupargs()
|
||||||
assert len(kw) == 1
|
assert len(item.funcargs) == 1
|
||||||
assert kw['some'] == 3
|
assert item.funcargs['some'] == 3
|
||||||
assert len(l) == 0
|
assert len(l) == 0
|
||||||
item.teardown()
|
item.teardown()
|
||||||
assert len(l) == 1
|
assert len(l) == 1
|
||||||
|
@ -318,10 +318,10 @@ class TestFunction:
|
||||||
item1, item2 = testdir.genitems([modcol])
|
item1, item2 = testdir.genitems([modcol])
|
||||||
modcol.setup()
|
modcol.setup()
|
||||||
assert modcol.config.pytestplugins.isregistered(modcol.obj)
|
assert modcol.config.pytestplugins.isregistered(modcol.obj)
|
||||||
kwargs = item1.lookup_allargs()
|
item1.setupargs()
|
||||||
assert kwargs['something'] == "test_method"
|
assert item1.funcargs['something'] == "test_method"
|
||||||
kwargs = item2.lookup_allargs()
|
item2.setupargs()
|
||||||
assert kwargs['something'] == "test_func"
|
assert item2.funcargs['something'] == "test_func"
|
||||||
modcol.teardown()
|
modcol.teardown()
|
||||||
assert not modcol.config.pytestplugins.isregistered(modcol.obj)
|
assert not modcol.config.pytestplugins.isregistered(modcol.obj)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue