[py-svn] r56681 - in py/branch/event/py/test2: . testing

hpk at codespeak.net hpk at codespeak.net
Sun Jul 20 13:47:12 CEST 2008


Author: hpk
Date: Sun Jul 20 13:47:11 2008
New Revision: 56681

Modified:
   py/branch/event/py/test2/executor.py
   py/branch/event/py/test2/testing/test_executor.py
Log:
inline executor tests to make them more readable


Modified: py/branch/event/py/test2/executor.py
==============================================================================
--- py/branch/event/py/test2/executor.py	(original)
+++ py/branch/event/py/test2/executor.py	Sun Jul 20 13:47:11 2008
@@ -8,7 +8,8 @@
 import py.__.test2.custompdb
 
 class RunExecutor(object):
-    """ Same as in executor, but just running run
+    """ 
+        executes a test item and fills in a ItemTestReport. 
     """
     def __init__(self, item): 
         self.item = item

Modified: py/branch/event/py/test2/testing/test_executor.py
==============================================================================
--- py/branch/event/py/test2/testing/test_executor.py	(original)
+++ py/branch/event/py/test2/testing/test_executor.py	Sun Jul 20 13:47:11 2008
@@ -1,55 +1,125 @@
 
 import py
 from py.__.test2.executor import RunExecutor, ForkExecutor, ApigenExecutor
-from py.__.test2.rsession.testing.basetest import BasicRsessionTest
 
+def setup_module(mod):
+    mod.tmpdir = py.test.ensuretemp(mod.__name__)
 
-class TestExecutor(BasicRsessionTest):
+class TestExecutor:
     Executor = RunExecutor 
 
-    def getexecutor(self, examplename):
-        funcitem = self.getfunc(examplename) 
-        return self.Executor(funcitem)
+    def setup_method(self, method):
+        self.tmpdir = tmpdir.join("%s_%s" % 
+            (self.__class__.__name__, method.__name__))
+
+    def makeitem(self, func, funcname="testfunc"):
+        funcname = getattr(func, '__name__', funcname)
+        self.tmpdir.ensure("__init__.py")
+        path = self.tmpdir.ensure(funcname + ".py")
+        path.write(py.code.Source(func))
+        self.config = py.test2.config._reparse([path.dirpath()])
+        modulecol = self.config._getcollector(path)
+        item = modulecol.join(funcname) 
+        assert item is not None, (item, funcname) 
+        return item 
+
+    def exectestfunc(self, func, funcname="testfunc"):
+        item = self.makeitem(func, funcname=funcname)
+        executor = self.getexec(item)
+        return executor.execute()
 
-    def exrun(self, examplename):
-        ex = self.getexecutor(examplename) 
-        return ex.execute()
+    def getexec(self, item):
+        return self.Executor(item)
          
-    def test_run_executor(self):
-        testrep = self.exrun("passed")
+    def test_run_executor_pass(self):
+        testrep = self.exectestfunc("""
+            def testfunc():
+                pass
+        """)
         assert testrep.passed 
+       
+    def test_run_executor_fail(self):
+        testrep = self.exectestfunc("""
+            def testfunc():
+                assert 0 
+        """)
+        assert testrep.failed 
 
-        for name in 'failed', 'skipped':
-            testrep = self.exrun(name)
-            assert getattr(testrep, name) 
-            assert not hasattr(testrep, 'signal')
-            #assert testrep._excinfo 
+    def test_run_executor_skip(self):
+        testrep = self.exectestfunc("""
+            import py
+            def testfunc():
+                py.test2.skip("hello")
+        """)
+        assert testrep.skipped 
 
     def test_run_executor_capture_stdout(self):
-        testrep = self.exrun("print")
+        testrep = self.exectestfunc("""
+            def testfunc():
+                print "samfing"
+        """)
         assert testrep.outerr[0][1] == "samfing\n"
         assert not testrep.outerr[1][1]
 
     def test_run_executor_capture_stderr(self):
-        testrep = self.exrun("printerr")
-        assert testrep.outerr[1][1] == "samfing\n"
+        testrep = self.exectestfunc("""
+            import sys
+            def testfunc():
+                print >>sys.stderr, "samfong"
+        """)
+        assert testrep.outerr[1][1] == "samfong\n"
         assert not testrep.outerr[0][1]
 
-    def test_box_executor_printfailing(self):
-        testrep = self.exrun("printfail") 
+    def test_executor_print_with_assertion_failed(self):
+        testrep = self.exectestfunc("""
+            import sys
+            def testfunc():
+                print "samfing elz"
+                print >>sys.stderr, "sure"
+                assert 0
+        """)
         assert not testrep.passed
         assert testrep.failed 
-        assert testrep.outerr[0][1].find("samfing elz") != -1 
-        assert not testrep.outerr[1][1]
+        assert testrep.outerr[0][1] == "samfing elz\n"
+        assert testrep.outerr[1][1] == "sure\n"
 
-    def test_executor_explicit_Failed(self):
-        testrep = self.exrun("explicitfail")
-        assert not testrep.passed 
+    def test_executor_print_with_explicit_fail(self):
+        testrep = self.exectestfunc("""
+            import py, sys
+            def testfunc():
+                print "samfing elz"
+                print >>sys.stderr, "sure"
+                py.test.fail()
+        """)
+        assert not testrep.passed
         assert testrep.failed 
+        assert testrep.outerr[0][1] == "samfing elz\n"
+        assert testrep.outerr[1][1] == "sure\n"
 
-    def test_executor_raises_fails(self):
-        testrep = self.exrun("raisesfails") 
+    def test_executor_print_raises_fail(self):
+        testrep = self.exectestfunc("""
+            import py, sys
+            def testfunc():
+                print "samfing elz"
+                print >>sys.stderr, "sure"
+                py.test2.raises(ValueError, "pass")
+        """)
+        assert not testrep.passed
         assert testrep.failed 
+        assert testrep.outerr[0][1] == "samfing elz\n"
+        assert testrep.outerr[1][1] == "sure\n"
+
+    def test_executor_setupfailure(self):
+        py.test.skip("xxx")
+        testrep = self.exectestfunc("""
+            def setup_module(mod):
+                print "setupmodule"
+                raise ValueError(10)
+            def testfunc():
+                pass
+        """)
+        assert not testrep.passed
+        assert testrep.setupfailure 
 
 class TestForkExecutor(TestExecutor):
     def setup_class(cls):
@@ -57,6 +127,17 @@
             py.test.skip("need os.fork()")
     Executor = ForkExecutor
 
+    def test_suicide(self):
+        py.test.skip("XXX")
+        testrep = self.exectestfunc("""
+            import os
+            def testfunc():
+                os.kill(15, os.getpid())
+        """)
+        assert testrep.signal == 15
+        assert not testrep.passed
+        assert testrep.failed 
+
 class TestApigenExecutor(TestExecutor):
     Executor = ApigenExecutor
 
@@ -75,6 +156,9 @@
         funcitem = self.getfunc(examplename) 
         return self.Executor(funcitem, tracer=Tracer())
 
+    def getexec(self, item, Tracer=Tracer):
+        return self.Executor(item, tracer=Tracer())
+
     def test_apigen_executor_tracing_hook(self):
         tmpdir = py.test2.ensuretemp("apigen_executor")
         tmpdir.ensure("__init__.py")



More information about the pytest-commit mailing list