[py-svn] pytest commit d633f6bed7af: add ability to use scope="class" in request.cached_setup() calls

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Sat Nov 20 18:05:14 CET 2010


# HG changeset patch -- Bitbucket.org
# Project pytest
# URL http://bitbucket.org/hpk42/pytest/overview
# User holger krekel <holger at merlinux.eu>
# Date 1290272598 -3600
# Node ID d633f6bed7afd820ddc0c6e560f5b63669ab1e60
# Parent  ddcf34bc8c80790e66689a13ae1d23fb8461a4ac
add ability to use scope="class" in request.cached_setup() calls

--- a/pytest.py
+++ b/pytest.py
@@ -5,7 +5,7 @@ see http://pytest.org for documentation 
 
 (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

--- 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'],

--- 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):

--- 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)

--- 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



More information about the pytest-commit mailing list