diff --git a/CHANGELOG b/CHANGELOG index 115b02a72..520e4c93c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -37,6 +37,7 @@ Changes between 1.3.4 and 2.0.0dev0 - fix issue93 stdout/stderr is captured while importing conftest.py - fix bug: unittest collected functions now also can have "pytestmark" applied at class/module level +- add ability to use "class" level for cached_setup helper Changes between 1.3.3 and 1.3.4 diff --git a/_pytest/python.py b/_pytest/python.py index d347aab9e..6f4777f68 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -626,8 +626,8 @@ class FuncargRequest: :arg teardown: function receiving a previously setup resource. :arg setup: a no-argument function creating a resource. - :arg scope: a string value out of ``function``, ``module`` or - ``session`` indicating the caching lifecycle of the resource. + :arg scope: a string value out of ``function``, ``class``, ``module`` + or ``session`` indicating the caching lifecycle of the resource. :arg extrakey: added to internal caching key of (funcargname, scope). """ if not hasattr(self.config, '_setupcache'): @@ -678,10 +678,15 @@ class FuncargRequest: def _getscopeitem(self, scope): if scope == "function": return self._pyfuncitem - elif scope == "module": - return self._pyfuncitem.getparent(pytest.Module) elif scope == "session": 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,)) def addfinalizer(self, finalizer): diff --git a/pytest.py b/pytest.py index c987946e1..8cc9ec5fa 100644 --- a/pytest.py +++ b/pytest.py @@ -5,7 +5,7 @@ see http://pytest.org for documentation and details (c) Holger Krekel and others, 2004-2010 """ -__version__ = '2.0.0.dev30' +__version__ = '2.0.0.dev31' __all__ = ['main'] from _pytest.core import main, UsageError, _preloadplugins diff --git a/setup.py b/setup.py index 35fed15b5..5af4732ee 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ def main(): name='pytest', description='py.test: simple powerful testing with Python', long_description = long_description, - version='2.0.0.dev30', + version='2.0.0.dev31', url='http://pytest.org', license='MIT license', platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'], diff --git a/testing/test_python.py b/testing/test_python.py index 185925500..82f02d826 100644 --- a/testing/test_python.py +++ b/testing/test_python.py @@ -682,9 +682,9 @@ def test_applymarker(testdir): class TestRequestCachedSetup: def test_request_cachedsetup(self, testdir): item1,item2 = testdir.getitems(""" + def test_func1(self, something): + pass class TestClass: - def test_func1(self, something): - pass def test_func2(self, something): pass """) @@ -692,6 +692,7 @@ class TestRequestCachedSetup: l = ["hello"] def setup(): return l.pop() + # cached_setup's scope defaults to 'module' ret1 = req1.cached_setup(setup) assert ret1 == "hello" ret1b = req1.cached_setup(setup) @@ -700,6 +701,39 @@ class TestRequestCachedSetup: ret2 = req2.cached_setup(setup) 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): item1 = testdir.getitem("def test_func(): pass") req1 = funcargs.FuncargRequest(item1)