fix cached_setup to deal properly for test_functions

with multiple args.  closes #50

--HG--
branch : trunk
This commit is contained in:
holger krekel 2009-09-30 12:52:40 +02:00
parent 2986c5dc74
commit 98b2300266
4 changed files with 46 additions and 8 deletions

View File

@ -1,6 +1,9 @@
Changes between 1.0.x and 'trunk'
=====================================
* fix issue50 - cached_setup now caches more to expectations
for test functions with multiple arguments.
* merge Jarko's fixes, issue #45 and #46
* add the ability to specify a path for py.lookup to search in

View File

@ -388,9 +388,9 @@ managing fixtures across test modules and test runs
def cached_setup(setup, teardown=None, scope="module", extrakey=None):
""" cache and return result of calling setup().
The scope and the ``extrakey`` determine the cache key.
The scope also determines when teardown(result)
will be called. valid scopes are:
The requested argument name, the scope and the ``extrakey``
determine the cache key. The scope also determines when
teardown(result) will be called. valid scopes are:
scope == 'function': when the single test function run finishes.
scope == 'module': when tests in a different module are run
scope == 'session': when tests of the session have run.

View File

@ -96,6 +96,7 @@ class FuncargRequest:
self._plugins.append(self.instance)
self._funcargs = self._pyfuncitem.funcargs.copy()
self._provider = {}
self._currentarg = None
def _fillfuncargs(self):
argnames = getfuncargnames(self.function)
@ -109,16 +110,16 @@ class FuncargRequest:
def cached_setup(self, setup, teardown=None, scope="module", extrakey=None):
""" cache and return result of calling setup().
The scope and the ``extrakey`` determine the cache key.
The scope also determines when teardown(result)
will be called. valid scopes are:
The requested argument name, the scope and the ``extrakey``
determine the cache key. The scope also determines when
teardown(result) will be called. valid scopes are:
scope == 'function': when the single test function run finishes.
scope == 'module': when tests in a different module are run
scope == 'session': when tests of the session have run.
"""
if not hasattr(self.config, '_setupcache'):
self.config._setupcache = {} # XXX weakref?
cachekey = (self._getscopeitem(scope), extrakey)
cachekey = (self._currentarg, self._getscopeitem(scope), extrakey)
cache = self.config._setupcache
try:
val = cache[cachekey]
@ -146,7 +147,12 @@ class FuncargRequest:
if not self._provider[argname]:
self._raiselookupfailed(argname)
funcargprovider = self._provider[argname].pop()
self._funcargs[argname] = res = funcargprovider(request=self)
oldarg = self._currentarg
self._currentarg = argname
try:
self._funcargs[argname] = res = funcargprovider(request=self)
finally:
self._currentarg = oldarg
return res
def _getscopeitem(self, scope):

View File

@ -258,6 +258,35 @@ class TestRequestCachedSetup:
req1.config._setupstate._callfinalizers(item1)
assert l == ["setup", "teardown", "setup", "teardown"]
def test_request_cached_setup_two_args(self, testdir):
testdir.makepyfile("""
def pytest_funcarg__arg1(request):
return request.cached_setup(lambda: 42)
def pytest_funcarg__arg2(request):
return request.cached_setup(lambda: 17)
def test_two_different_setups(arg1, arg2):
assert arg1 != arg2
""")
result = testdir.runpytest("-v")
result.stdout.fnmatch_lines([
"*1 passed*"
])
def test_request_cached_setup_getfuncargvalue(self, testdir):
testdir.makepyfile("""
def pytest_funcarg__arg1(request):
arg1 = request.getfuncargvalue("arg2")
return request.cached_setup(lambda: arg1 + 1)
def pytest_funcarg__arg2(request):
return request.cached_setup(lambda: 10)
def test_two_funcarg(arg1):
assert arg1 == 11
""")
result = testdir.runpytest("-v")
result.stdout.fnmatch_lines([
"*1 passed*"
])
def test_request_cached_setup_functional(self, testdir):
testdir.makepyfile(test_0="""
l = []