fix cached_setup to deal properly for test_functions
with multiple args. closes #50 --HG-- branch : trunk
This commit is contained in:
		
							parent
							
								
									2986c5dc74
								
							
						
					
					
						commit
						98b2300266
					
				|  | @ -1,6 +1,9 @@ | ||||||
| Changes between 1.0.x and 'trunk' | 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 | * merge Jarko's fixes, issue #45 and #46 | ||||||
| 
 | 
 | ||||||
| * add the ability to specify a path for py.lookup to search in | * add the ability to specify a path for py.lookup to search in | ||||||
|  |  | ||||||
|  | @ -388,9 +388,9 @@ managing fixtures across test modules and test runs | ||||||
|     def cached_setup(setup, teardown=None, scope="module", extrakey=None): |     def cached_setup(setup, teardown=None, scope="module", extrakey=None): | ||||||
|         """ cache and return result of calling setup().   |         """ cache and return result of calling setup().   | ||||||
| 
 | 
 | ||||||
|         The scope and the ``extrakey`` determine the cache key.  |         The requested argument name, the scope and the ``extrakey``  | ||||||
|         The scope also determines when teardown(result)  |         determine the cache key.  The scope also determines when  | ||||||
|         will be called.  valid scopes are:  |         teardown(result) will be called.  valid scopes are:  | ||||||
|         scope == 'function': when the single test function run finishes.  |         scope == 'function': when the single test function run finishes.  | ||||||
|         scope == 'module': when tests in a different module are run |         scope == 'module': when tests in a different module are run | ||||||
|         scope == 'session': when tests of the session have run.  |         scope == 'session': when tests of the session have run.  | ||||||
|  |  | ||||||
|  | @ -96,6 +96,7 @@ class FuncargRequest: | ||||||
|             self._plugins.append(self.instance) |             self._plugins.append(self.instance) | ||||||
|         self._funcargs  = self._pyfuncitem.funcargs.copy() |         self._funcargs  = self._pyfuncitem.funcargs.copy() | ||||||
|         self._provider = {} |         self._provider = {} | ||||||
|  |         self._currentarg = None | ||||||
| 
 | 
 | ||||||
|     def _fillfuncargs(self): |     def _fillfuncargs(self): | ||||||
|         argnames = getfuncargnames(self.function) |         argnames = getfuncargnames(self.function) | ||||||
|  | @ -109,16 +110,16 @@ class FuncargRequest: | ||||||
|     def cached_setup(self, setup, teardown=None, scope="module", extrakey=None): |     def cached_setup(self, setup, teardown=None, scope="module", extrakey=None): | ||||||
|         """ cache and return result of calling setup().   |         """ cache and return result of calling setup().   | ||||||
| 
 | 
 | ||||||
|         The scope and the ``extrakey`` determine the cache key.  |         The requested argument name, the scope and the ``extrakey``  | ||||||
|         The scope also determines when teardown(result)  |         determine the cache key.  The scope also determines when  | ||||||
|         will be called.  valid scopes are:  |         teardown(result) will be called.  valid scopes are:  | ||||||
|         scope == 'function': when the single test function run finishes.  |         scope == 'function': when the single test function run finishes.  | ||||||
|         scope == 'module': when tests in a different module are run |         scope == 'module': when tests in a different module are run | ||||||
|         scope == 'session': when tests of the session have run.  |         scope == 'session': when tests of the session have run.  | ||||||
|         """ |         """ | ||||||
|         if not hasattr(self.config, '_setupcache'): |         if not hasattr(self.config, '_setupcache'): | ||||||
|             self.config._setupcache = {} # XXX weakref?  |             self.config._setupcache = {} # XXX weakref?  | ||||||
|         cachekey = (self._getscopeitem(scope), extrakey) |         cachekey = (self._currentarg, self._getscopeitem(scope), extrakey) | ||||||
|         cache = self.config._setupcache |         cache = self.config._setupcache | ||||||
|         try: |         try: | ||||||
|             val = cache[cachekey] |             val = cache[cachekey] | ||||||
|  | @ -146,7 +147,12 @@ class FuncargRequest: | ||||||
|         if not self._provider[argname]: |         if not self._provider[argname]: | ||||||
|             self._raiselookupfailed(argname) |             self._raiselookupfailed(argname) | ||||||
|         funcargprovider = self._provider[argname].pop() |         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 |         return res | ||||||
| 
 | 
 | ||||||
|     def _getscopeitem(self, scope): |     def _getscopeitem(self, scope): | ||||||
|  |  | ||||||
|  | @ -258,6 +258,35 @@ class TestRequestCachedSetup: | ||||||
|         req1.config._setupstate._callfinalizers(item1) |         req1.config._setupstate._callfinalizers(item1) | ||||||
|         assert l == ["setup", "teardown", "setup", "teardown"] |         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): |     def test_request_cached_setup_functional(self, testdir): | ||||||
|         testdir.makepyfile(test_0=""" |         testdir.makepyfile(test_0=""" | ||||||
|             l = [] |             l = [] | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue