bringing docs and funcargs in sync

--HG--
branch : trunk
This commit is contained in:
holger krekel 2009-04-14 19:57:00 +02:00
parent 792dce025c
commit 5e03bdad84
6 changed files with 29 additions and 19 deletions

View File

@ -2,7 +2,6 @@
**funcargs**: powerful and simple test setup **funcargs**: powerful and simple test setup
====================================================== ======================================================
In version 1.0 py.test introduces a new mechanism for setting up test In version 1.0 py.test introduces a new mechanism for setting up test
state for use by Python test functions. It is particularly useful state for use by Python test functions. It is particularly useful
for functional and integration testing but also for unit testing. for functional and integration testing but also for unit testing.
@ -170,9 +169,21 @@ into ``myapp.py``:
def question(self): def question(self):
return 6 * 9 return 6 * 9
You can now run the test with ``py.test test_sample.py``: You can now run the test with ``py.test test_sample.py`` which will
show this failure:
.. sourcecode:: python .. sourcecode:: python
def test_answer(mysetup):
app = mysetup.myapp()
answer = app.question()
> assert answer == 42
E assert 54 == 42
If you are confused as to that the question or answer is
here, please visit here_.
.. _here: http://uncyclopedia.wikia.com/wiki/The_Hitchhiker's_Guide_to_the_Galaxy
.. _`local plugin`: ext.html#local-plugin .. _`local plugin`: ext.html#local-plugin

View File

@ -4,7 +4,7 @@ from py.__.test.dist.nodemanage import NodeManager
class pytest_funcarg__mysetup: class pytest_funcarg__mysetup:
def __init__(self, request): def __init__(self, request):
basetemp = request.config.ensuretemp(request.getfspath().purebasename) basetemp = request.config.ensuretemp(request.getfspath().purebasename)
basetemp = basetemp.mkdir(request.funcname) basetemp = basetemp.mkdir(request.function.__name__)
self.source = basetemp.mkdir("source") self.source = basetemp.mkdir("source")
self.dest = basetemp.mkdir("dest") self.dest = basetemp.mkdir("dest")

View File

@ -42,7 +42,7 @@ class TmpTestdir:
self.request = request self.request = request
# XXX remove duplication with tmpdir plugin # XXX remove duplication with tmpdir plugin
basetmp = request.config.ensuretemp("testdir") basetmp = request.config.ensuretemp("testdir")
name = request.funcname name = request.function.__name__
for i in range(100): for i in range(100):
try: try:
tmpdir = basetmp.mkdir(name + str(i)) tmpdir = basetmp.mkdir(name + str(i))
@ -90,7 +90,7 @@ class TmpTestdir:
items = kwargs.items() items = kwargs.items()
if args: if args:
source = "\n".join(map(str, args)) source = "\n".join(map(str, args))
basename = self.request.funcname basename = self.request.function.__name__
items.insert(0, (basename, source)) items.insert(0, (basename, source))
ret = None ret = None
for name, value in items: for name, value in items:
@ -200,7 +200,7 @@ class TmpTestdir:
return self.config.getfsnode(path) return self.config.getfsnode(path)
def getmodulecol(self, source, configargs=(), withinit=False): def getmodulecol(self, source, configargs=(), withinit=False):
kw = {self.request.funcname: py.code.Source(source).strip()} kw = {self.request.function.__name__: py.code.Source(source).strip()}
path = self.makepyfile(**kw) path = self.makepyfile(**kw)
if withinit: if withinit:
self.makepyfile(__init__ = "#") self.makepyfile(__init__ = "#")

View File

@ -14,7 +14,7 @@ class TmpdirPlugin:
""" """
def pytest_funcarg__tmpdir(self, request): def pytest_funcarg__tmpdir(self, request):
name = request.funcname name = request.function.__name__
return request.config.mktemp(name, numbered=True) return request.config.mktemp(name, numbered=True)
# =============================================================================== # ===============================================================================

View File

@ -399,10 +399,9 @@ class FuncargRequest:
def __init__(self, pyfuncitem, argname): def __init__(self, pyfuncitem, argname):
# XXX make pyfuncitem _pyfuncitem # XXX make pyfuncitem _pyfuncitem
self.pyfuncitem = pyfuncitem self._pyfuncitem = pyfuncitem
self.argname = argname self.argname = argname
self.function = pyfuncitem.obj self.function = pyfuncitem.obj
self.funcname = pyfuncitem.name
self.config = pyfuncitem.config self.config = pyfuncitem.config
self._plugins = self._getplugins() self._plugins = self._getplugins()
self._methods = self.config.pluginmanager.listattr( self._methods = self.config.pluginmanager.listattr(
@ -411,12 +410,12 @@ class FuncargRequest:
) )
def __repr__(self): def __repr__(self):
return "<FuncargRequest %r for %r>" %(self.argname, self.pyfuncitem) return "<FuncargRequest %r for %r>" %(self.argname, self._pyfuncitem)
def _getplugins(self): def _getplugins(self):
plugins = [] plugins = []
current = self.pyfuncitem current = self._pyfuncitem
while not isinstance(current, Module): while not isinstance(current, Module):
current = current.parent current = current.parent
if isinstance(current, (Instance, Module)): if isinstance(current, (Instance, Module)):
@ -430,10 +429,10 @@ class FuncargRequest:
return nextmethod(request=self) return nextmethod(request=self)
def addfinalizer(self, finalizer): def addfinalizer(self, finalizer):
self.pyfuncitem.addfinalizer(finalizer) self._pyfuncitem.addfinalizer(finalizer)
def getfspath(self): def getfspath(self):
return self.pyfuncitem.fspath return self._pyfuncitem.fspath
def _raiselookupfailed(self): def _raiselookupfailed(self):
available = [] available = []
@ -443,7 +442,7 @@ class FuncargRequest:
name = name[len(self._argprefix):] name = name[len(self._argprefix):]
if name not in available: if name not in available:
available.append(name) available.append(name)
metainfo = self.pyfuncitem.repr_metainfo() metainfo = self._pyfuncitem.repr_metainfo()
msg = "funcargument %r not found for: %s" %(self.argname,metainfo.verboseline()) msg = "funcargument %r not found for: %s" %(self.argname,metainfo.verboseline())
msg += "\n available funcargs: %s" %(", ".join(available),) msg += "\n available funcargs: %s" %(", ".join(available),)
raise LookupError(msg) raise LookupError(msg)

View File

@ -16,7 +16,7 @@ class TestFuncargs:
item = testdir.getitem("def test_func(some, other=42): pass") item = testdir.getitem("def test_func(some, other=42): pass")
class Provider: class Provider:
def pytest_funcarg__some(self, request): def pytest_funcarg__some(self, request):
return request.funcname return request.function.__name__
item.config.pluginmanager.register(Provider()) item.config.pluginmanager.register(Provider())
item.setupargs() item.setupargs()
assert len(item.funcargs) == 1 assert len(item.funcargs) == 1
@ -25,7 +25,7 @@ class TestFuncargs:
item = testdir.getitem("def test_func(some=42, other=13): pass") item = testdir.getitem("def test_func(some=42, other=13): pass")
class Provider: class Provider:
def pytest_funcarg__other(self, request): def pytest_funcarg__other(self, request):
return request.funcname return request.function.__name__
item.config.pluginmanager.register(Provider()) item.config.pluginmanager.register(Provider())
item.setupargs() item.setupargs()
assert len(item.funcargs) == 1 assert len(item.funcargs) == 1
@ -37,7 +37,7 @@ class TestFuncargs:
item = testdir.getitem("def test_func(some, other): pass") item = testdir.getitem("def test_func(some, other): pass")
class Provider: class Provider:
def pytest_funcarg__some(self, request): def pytest_funcarg__some(self, request):
return request.funcname return request.function.__name__
def pytest_funcarg__other(self, request): def pytest_funcarg__other(self, request):
return 42 return 42
item.config.pluginmanager.register(Provider()) item.config.pluginmanager.register(Provider())
@ -49,7 +49,7 @@ class TestFuncargs:
def test_funcarg_lookup_modulelevel(self, testdir): def test_funcarg_lookup_modulelevel(self, testdir):
modcol = testdir.getmodulecol(""" modcol = testdir.getmodulecol("""
def pytest_funcarg__something(request): def pytest_funcarg__something(request):
return request.funcname return request.function.__name__
class TestClass: class TestClass:
def test_method(self, something): def test_method(self, something):