[py-svn] r60137 - in py/trunk/py: doc test test/testing

pedronis at codespeak.net pedronis at codespeak.net
Tue Nov 25 17:10:17 CET 2008


Author: pedronis
Date: Tue Nov 25 17:10:16 2008
New Revision: 60137

Modified:
   py/trunk/py/doc/test.txt
   py/trunk/py/test/pycollect.py
   py/trunk/py/test/testing/test_collect.py
Log:
support for explicitly named generative tests with tests and doc addition.



Modified: py/trunk/py/doc/test.txt
==============================================================================
--- py/trunk/py/doc/test.txt	(original)
+++ py/trunk/py/doc/test.txt	Tue Nov 25 17:10:16 2008
@@ -136,6 +136,13 @@
 to get run, notably ``check(42)``, ``check(17)`` and ``check(49)``
 of which the middle one will obviously fail. 
 
+To make it easier to distinguish the generated tests it is possible to specify an explicit name for them, like for example::
+
+    def test_generative(): 
+        for x in (42,17,49): 
+            yield "case %d" % x, check, x 
+
+
 .. _`selection by keyword`: 
 
 selecting tests by keyword 

Modified: py/trunk/py/test/pycollect.py
==============================================================================
--- py/trunk/py/test/pycollect.py	(original)
+++ py/trunk/py/test/pycollect.py	Tue Nov 25 17:10:16 2008
@@ -268,19 +268,27 @@
         self._setupstate.prepare(self) 
         l = []
         for i, x in py.builtin.enumerate(self.obj()): 
-            call, args = self.getcallargs(x)
+            name, call, args = self.getcallargs(x)
             if not callable(call): 
                 raise TypeError("%r yielded non callable test %r" %(self.obj, call,))
-            name = "[%d]" % i  
+            if name is None:
+                name = "[%d]" % i
+            else:
+                name = "['%s']" % name
             l.append(self.Function(name, self, args=args, callobj=call))
         return l
         
     def getcallargs(self, obj):
-        if isinstance(obj, (tuple, list)):
-            call, args = obj[0], obj[1:]
+        if not isinstance(obj, (tuple, list)):
+            obj = (obj,)
+        # explict naming
+        if isinstance(obj[0], basestring):
+            name = obj[0]
+            obj = obj[1:]
         else:
-            call, args = obj, ()
-        return call, args 
+            name = None
+        call, args = obj[0], obj[1:]
+        return name, call, args 
 
 #
 #  Test Items 

Modified: py/trunk/py/test/testing/test_collect.py
==============================================================================
--- py/trunk/py/test/testing/test_collect.py	(original)
+++ py/trunk/py/test/testing/test_collect.py	Tue Nov 25 17:10:16 2008
@@ -152,6 +152,48 @@
         assert gencolitems[0].name == '[0]'
         assert gencolitems[0].obj.func_name == 'func1'
 
+    def test_generative_functions_with_explicit_names(self):
+        modcol = self.getmodulecol("""
+            def func1(arg, arg2): 
+                assert arg == arg2 
+
+            def test_gen(): 
+                yield "one", func1, 17, 3*5
+                yield "two", func1, 42, 6*7
+        """)
+        colitems = modcol.collect()
+        assert len(colitems) == 1
+        gencol = colitems[0]
+        assert isinstance(gencol, py.test.collect.Generator)
+        gencolitems = gencol.collect()
+        assert len(gencolitems) == 2
+        assert isinstance(gencolitems[0], py.test.collect.Function)
+        assert isinstance(gencolitems[1], py.test.collect.Function)
+        assert gencolitems[0].name == "['one']"
+        assert gencolitems[0].obj.func_name == 'func1'
+        assert gencolitems[1].name == "['two']"
+        assert gencolitems[1].obj.func_name == 'func1'        
+
+    def test_generative_methods_with_explicit_names(self): 
+        modcol = self.getmodulecol("""
+            def func1(arg, arg2): 
+                assert arg == arg2 
+            class TestGenMethods: 
+                def test_gen(self): 
+                    yield "m1", func1, 17, 3*5
+                    yield "m2", func1, 42, 6*7
+        """)
+        gencol = modcol.collect()[0].collect()[0].collect()[0]
+        assert isinstance(gencol, py.test.collect.Generator)
+        gencolitems = gencol.collect()
+        assert len(gencolitems) == 2
+        assert isinstance(gencolitems[0], py.test.collect.Function)
+        assert isinstance(gencolitems[1], py.test.collect.Function)
+        assert gencolitems[0].name == "['m1']"
+        assert gencolitems[0].obj.func_name == 'func1'
+        assert gencolitems[1].name == "['m2']"
+        assert gencolitems[1].obj.func_name == 'func1'        
+
     def test_module_assertion_setup(self):
         modcol = self.getmodulecol("pass")
         from py.__.magic import assertion



More information about the pytest-commit mailing list