add ability to use scope="class" in request.cached_setup() calls

This commit is contained in:
holger krekel 2010-11-20 18:03:18 +01:00
parent 093bef0a08
commit 9a21a81740
5 changed files with 48 additions and 8 deletions

View File

@ -37,6 +37,7 @@ Changes between 1.3.4 and 2.0.0dev0
- fix issue93 stdout/stderr is captured while importing conftest.py - fix issue93 stdout/stderr is captured while importing conftest.py
- fix bug: unittest collected functions now also can have "pytestmark" - fix bug: unittest collected functions now also can have "pytestmark"
applied at class/module level applied at class/module level
- add ability to use "class" level for cached_setup helper
Changes between 1.3.3 and 1.3.4 Changes between 1.3.3 and 1.3.4

View File

@ -626,8 +626,8 @@ class FuncargRequest:
:arg teardown: function receiving a previously setup resource. :arg teardown: function receiving a previously setup resource.
:arg setup: a no-argument function creating a resource. :arg setup: a no-argument function creating a resource.
:arg scope: a string value out of ``function``, ``module`` or :arg scope: a string value out of ``function``, ``class``, ``module``
``session`` indicating the caching lifecycle of the resource. or ``session`` indicating the caching lifecycle of the resource.
:arg extrakey: added to internal caching key of (funcargname, scope). :arg extrakey: added to internal caching key of (funcargname, scope).
""" """
if not hasattr(self.config, '_setupcache'): if not hasattr(self.config, '_setupcache'):
@ -678,10 +678,15 @@ class FuncargRequest:
def _getscopeitem(self, scope): def _getscopeitem(self, scope):
if scope == "function": if scope == "function":
return self._pyfuncitem return self._pyfuncitem
elif scope == "module":
return self._pyfuncitem.getparent(pytest.Module)
elif scope == "session": elif scope == "session":
return None return None
elif scope == "class":
x = self._pyfuncitem.getparent(pytest.Class)
if x is not None:
return x
scope = "module"
if scope == "module":
return self._pyfuncitem.getparent(pytest.Module)
raise ValueError("unknown finalization scope %r" %(scope,)) raise ValueError("unknown finalization scope %r" %(scope,))
def addfinalizer(self, finalizer): def addfinalizer(self, finalizer):

View File

@ -5,7 +5,7 @@ see http://pytest.org for documentation and details
(c) Holger Krekel and others, 2004-2010 (c) Holger Krekel and others, 2004-2010
""" """
__version__ = '2.0.0.dev30' __version__ = '2.0.0.dev31'
__all__ = ['main'] __all__ = ['main']
from _pytest.core import main, UsageError, _preloadplugins from _pytest.core import main, UsageError, _preloadplugins

View File

@ -22,7 +22,7 @@ def main():
name='pytest', name='pytest',
description='py.test: simple powerful testing with Python', description='py.test: simple powerful testing with Python',
long_description = long_description, long_description = long_description,
version='2.0.0.dev30', version='2.0.0.dev31',
url='http://pytest.org', url='http://pytest.org',
license='MIT license', license='MIT license',
platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'], platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],

View File

@ -682,9 +682,9 @@ def test_applymarker(testdir):
class TestRequestCachedSetup: class TestRequestCachedSetup:
def test_request_cachedsetup(self, testdir): def test_request_cachedsetup(self, testdir):
item1,item2 = testdir.getitems(""" item1,item2 = testdir.getitems("""
def test_func1(self, something):
pass
class TestClass: class TestClass:
def test_func1(self, something):
pass
def test_func2(self, something): def test_func2(self, something):
pass pass
""") """)
@ -692,6 +692,7 @@ class TestRequestCachedSetup:
l = ["hello"] l = ["hello"]
def setup(): def setup():
return l.pop() return l.pop()
# cached_setup's scope defaults to 'module'
ret1 = req1.cached_setup(setup) ret1 = req1.cached_setup(setup)
assert ret1 == "hello" assert ret1 == "hello"
ret1b = req1.cached_setup(setup) ret1b = req1.cached_setup(setup)
@ -700,6 +701,39 @@ class TestRequestCachedSetup:
ret2 = req2.cached_setup(setup) ret2 = req2.cached_setup(setup)
assert ret2 == ret1 assert ret2 == ret1
def test_request_cachedsetup_class(self, testdir):
item1, item2, item3, item4 = testdir.getitems("""
def test_func1(self, something):
pass
def test_func2(self, something):
pass
class TestClass:
def test_func1a(self, something):
pass
def test_func2b(self, something):
pass
""")
req1 = funcargs.FuncargRequest(item2)
l = ["hello2", "hello"]
def setup():
return l.pop()
# module level functions setup with scope=class
# automatically turn "class" to "module" scope
ret1 = req1.cached_setup(setup, scope="class")
assert ret1 == "hello"
req2 = funcargs.FuncargRequest(item2)
ret2 = req2.cached_setup(setup, scope="class")
assert ret2 == "hello"
req3 = funcargs.FuncargRequest(item3)
ret3a = req3.cached_setup(setup, scope="class")
ret3b = req3.cached_setup(setup, scope="class")
assert ret3a == ret3b == "hello2"
req4 = funcargs.FuncargRequest(item4)
ret4 = req4.cached_setup(setup, scope="class")
assert ret4 == ret3a
def test_request_cachedsetup_extrakey(self, testdir): def test_request_cachedsetup_extrakey(self, testdir):
item1 = testdir.getitem("def test_func(): pass") item1 = testdir.getitem("def test_func(): pass")
req1 = funcargs.FuncargRequest(item1) req1 = funcargs.FuncargRequest(item1)