move tests to test_funcargs.py and start with new request object tests

--HG--
branch : trunk
This commit is contained in:
holger krekel 2009-04-14 02:23:42 +02:00
parent 04aec935bd
commit 70840f605e
4 changed files with 121 additions and 86 deletions

View File

@ -79,8 +79,8 @@ class PluginManager(object):
for x in self.comregistry.listattr(attrname): for x in self.comregistry.listattr(attrname):
return x return x
def listattr(self, attrname, plugins=None): def listattr(self, attrname, plugins=None, extra=()):
return self.comregistry.listattr(attrname, plugins=plugins) return self.comregistry.listattr(attrname, plugins=plugins, extra=extra)
def call_firstresult(self, *args, **kwargs): def call_firstresult(self, *args, **kwargs):
return self.comregistry.call_firstresult(*args, **kwargs) return self.comregistry.call_firstresult(*args, **kwargs)

View File

@ -410,6 +410,20 @@ class Function(FunctionMixin, py.test.collect.Item):
def __ne__(self, other): def __ne__(self, other):
return not self == other return not self == other
def getrequest(self, argname):
return FuncargRequest(pyfuncitem=self, argname=argname)
# DEPRECATED
#from py.__.test.plugin.pytest_doctest import DoctestFile class FuncargRequest:
def __init__(self, pyfuncitem, argname):
self.pyfuncitem = pyfuncitem
self.argname = argname
funcargname = "pytest_funcarg__" + str(argname)
pm = self.pyfuncitem.config.pluginmanager
extra = []
current = pyfuncitem
while not isinstance(current, Module):
current = current.parent
if isinstance(current, (Instance, Module)):
extra.insert(0, current.obj)
self._methods = pm.listattr(funcargname, extra=extra)

View File

@ -0,0 +1,103 @@
import py
class TestFuncargs:
def test_funcarg_lookupfails(self, testdir):
testdir.makeconftest("""
class ConftestPlugin:
def pytest_funcarg__something(self, pyfuncitem):
return 42
""")
item = testdir.getitem("def test_func(some): pass")
exc = py.test.raises(LookupError, "item.setupargs()")
s = str(exc.value)
assert s.find("something") != -1
def test_funcarg_lookup_default(self, testdir):
item = testdir.getitem("def test_func(some, other=42): pass")
class Provider:
def pytest_funcarg__some(self, pyfuncitem):
return pyfuncitem.name
item.config.pluginmanager.register(Provider())
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")
class Provider:
def pytest_funcarg__other(self, pyfuncitem):
return pyfuncitem.name
item.config.pluginmanager.register(Provider())
item.setupargs()
assert len(item.funcargs) == 1
name, value = item.funcargs.popitem()
assert name == "other"
assert value == item.name
def test_funcarg_basic(self, testdir):
item = testdir.getitem("def test_func(some, other): pass")
class Provider:
def pytest_funcarg__some(self, pyfuncitem):
return pyfuncitem.name
def pytest_funcarg__other(self, pyfuncitem):
return 42
item.config.pluginmanager.register(Provider())
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")
l = []
class Provider:
def pytest_funcarg__some(self, pyfuncitem):
pyfuncitem.addfinalizer(lambda: l.append(42))
return 3
item.config.pluginmanager.register(Provider())
item.setupargs()
assert len(item.funcargs) == 1
assert item.funcargs['some'] == 3
assert len(l) == 0
item.teardown()
assert len(l) == 1
assert l[0] == 42
def test_funcarg_lookup_modulelevel(self, testdir):
modcol = testdir.getmodulecol("""
def pytest_funcarg__something(pyfuncitem):
return pyfuncitem.name
class TestClass:
def test_method(self, something):
pass
def test_func(something):
pass
""")
item1, item2 = testdir.genitems([modcol])
modcol.setup()
assert modcol.config.pluginmanager.isregistered(modcol.obj)
item1.setupargs()
assert item1.funcargs['something'] == "test_method"
item2.setupargs()
assert item2.funcargs['something'] == "test_func"
modcol.teardown()
assert not modcol.config.pluginmanager.isregistered(modcol.obj)
class TestRequest:
def test_request_contains_funcargs_methods(self, testdir):
modcol = testdir.getmodulecol("""
def pytest_funcarg__something(request):
pass
class TestClass:
def pytest_funcarg__something(self, request):
pass
def test_method(self, something):
pass
""")
item1, = testdir.genitems([modcol])
assert item1.name == "test_method"
methods = item1.getrequest("something")._methods
assert len(methods) == 2
method1, method2 = methods
assert not hasattr(method1, 'im_self')
assert method2.im_self is not None

View File

@ -243,88 +243,6 @@ class TestFunction:
assert f1 == f1_b assert f1 == f1_b
assert not f1 != f1_b assert not f1 != f1_b
def test_funcarg_lookupfails(self, testdir):
testdir.makeconftest("""
class ConftestPlugin:
def pytest_funcarg__something(self, pyfuncitem):
return 42
""")
item = testdir.getitem("def test_func(some): pass")
exc = py.test.raises(LookupError, "item.setupargs()")
s = str(exc.value)
assert s.find("something") != -1
def test_funcarg_lookup_default(self, testdir):
item = testdir.getitem("def test_func(some, other=42): pass")
class Provider:
def pytest_funcarg__some(self, pyfuncitem):
return pyfuncitem.name
item.config.pluginmanager.register(Provider())
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")
class Provider:
def pytest_funcarg__other(self, pyfuncitem):
return pyfuncitem.name
item.config.pluginmanager.register(Provider())
item.setupargs()
assert len(item.funcargs) == 1
name, value = item.funcargs.popitem()
assert name == "other"
assert value == item.name
def test_funcarg_basic(self, testdir):
item = testdir.getitem("def test_func(some, other): pass")
class Provider:
def pytest_funcarg__some(self, pyfuncitem):
return pyfuncitem.name
def pytest_funcarg__other(self, pyfuncitem):
return 42
item.config.pluginmanager.register(Provider())
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")
l = []
class Provider:
def pytest_funcarg__some(self, pyfuncitem):
pyfuncitem.addfinalizer(lambda: l.append(42))
return 3
item.config.pluginmanager.register(Provider())
item.setupargs()
assert len(item.funcargs) == 1
assert item.funcargs['some'] == 3
assert len(l) == 0
item.teardown()
assert len(l) == 1
assert l[0] == 42
def test_funcarg_lookup_modulelevel(self, testdir):
modcol = testdir.getmodulecol("""
def pytest_funcarg__something(pyfuncitem):
return pyfuncitem.name
class TestClass:
def test_method(self, something):
pass
def test_func(something):
pass
""")
item1, item2 = testdir.genitems([modcol])
modcol.setup()
assert modcol.config.pluginmanager.isregistered(modcol.obj)
item1.setupargs()
assert item1.funcargs['something'] == "test_method"
item2.setupargs()
assert item2.funcargs['something'] == "test_func"
modcol.teardown()
assert not modcol.config.pluginmanager.isregistered(modcol.obj)
class TestSorting: class TestSorting:
def test_check_equality_and_cmp_basic(self, testdir): def test_check_equality_and_cmp_basic(self, testdir):
modcol = testdir.getmodulecol(""" modcol = testdir.getmodulecol("""