[py-svn] r56996 - py/branch/event/py/test2/testing

hpk at codespeak.net hpk at codespeak.net
Tue Aug 5 11:24:20 CEST 2008


Author: hpk
Date: Tue Aug  5 11:24:19 2008
New Revision: 56996

Added:
   py/branch/event/py/test2/testing/test_runner_functional.py
Removed:
   py/branch/event/py/test2/testing/test_runner.py
Modified:
   py/branch/event/py/test2/testing/acceptance_test.py
   py/branch/event/py/test2/testing/suptest.py
   py/branch/event/py/test2/testing/test_session.py
Log:
port tests, i.e. move away from mock testing here, too tiresome for refactorings imho


Modified: py/branch/event/py/test2/testing/acceptance_test.py
==============================================================================
--- py/branch/event/py/test2/testing/acceptance_test.py	(original)
+++ py/branch/event/py/test2/testing/acceptance_test.py	Tue Aug  5 11:24:19 2008
@@ -114,3 +114,45 @@
             "%s:3: 3 Skipped, reason: test" %(p3,)
         ])
 
+    def test_looponfailing_looping(self):
+        py.test.skip("thought needed to nicely test --looponfailing")
+        py.test.skip("xxx check events")
+        o = tmpdir.ensure('looponfailing', dir=1) 
+        tfile = o.join('test_looponfailing.py')
+        tfile.write(py.code.Source("""
+            def test_1():
+                assert 1 == 0 
+        """))
+        print py.std.sys.executable
+        config = py.test2.config._reparse(['--looponfailing', str(o)])
+        session = config.initsession()
+        failures = session.main()
+        
+        cls = config._getsessionclass() 
+        out = py.std.Queue.Queue() 
+        session = cls(config, out.put) 
+        pool = py._thread.WorkerPool() 
+        reply = pool.dispatch(session.main)
+        while 1: 
+            s = out.get(timeout=5.0)
+            if s.find('1 failed') != -1: 
+                break 
+            print s
+        else: 
+            py.test2.fail("did not see test_1 failure") 
+        # XXX we would like to have a cleaner way to finish 
+        try: 
+            reply.get(timeout=5.0) 
+        except IOError, e: 
+            assert str(e).lower().find('timeout') != -1 
+
+    def test_invoking_pdb(self):
+        py.test.skip("implement test for interaction with pdb") 
+        l = []
+        def testfunc():
+            raise ValueError(7)
+        def mypdb(runinfo):
+            l.append(runinfo)
+        testrep = self.run(testfunc, pdb=mypdb)
+        assert len(l) == 1
+        assert l[0].excinfo.exconly().find("ValueError: 7") != -1

Modified: py/branch/event/py/test2/testing/suptest.py
==============================================================================
--- py/branch/event/py/test2/testing/suptest.py	(original)
+++ py/branch/event/py/test2/testing/suptest.py	Tue Aug  5 11:24:19 2008
@@ -160,39 +160,37 @@
 # XXX below some code to help with inlining examples 
 #     as source code.  
  
-class TestBaseInlineCollection:
+class InlineCollection:
+    """ helps to collect and run test functions inlining other test functions. """
     def setup_method(self, method):
-        self.tmpdir = tmpdir.join("%s_%s_%s" % 
-            (__name__, self.__class__.__name__, method.__name__))
+        self.tmpdir = py.test2.ensuretemp("%s_%s" % 
+            (self.__class__.__name__, method.__name__))
 
-    def getmodulecol(self, func, funcname="testfunc"):
+    def makeconftest(self, source):
+        self.tmpdir.ensure("__init__.py")
+        path = self.tmpdir.ensure("conftest.py")
+        path.write(py.code.Source(source))
+
+    def getmodulecol(self, func, funcname="test_func"):
         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)
-        return modulecol
+        return self.config.getfsnode(path)
 
-    def makeitem(self, source, funcname="testfunc"):
-        modulecol = self.getmodulecol(source)
+    def getitem(self, source, funcname="test_func"):
+        modulecol = self.getmodulecol(source, funcname=funcname)
         item = modulecol.join(funcname) 
-        assert item is not None, (item, funcname) 
+        assert item is not None, "%r item not found in module:\n%s" %(funcname, source)
         return item 
 
+    def runitem(self, func, funcname="test_func"):
+        item = self.getitem(func, funcname=funcname)
+        runner = self.getrunner()
+        return runner(item)
+
     def getitems(self, source):
         modulecol = self.getmodulecol(source)
         return [modulecol.join(x) for x in modulecol.listdir()]
 
-    def runtestfunc(self, func, funcname="testfunc"):
-        item = self.makeitem(func, funcname=funcname)
-        runner = self.getrunner()
-        return runner(item)
-
-    def exampleusage(self):
-        testrep = self.runtestfunc("""
-            def testfunc():
-                pass
-        """)
-        assert testrep.passed 
-       

Deleted: /py/branch/event/py/test2/testing/test_runner.py
==============================================================================
--- /py/branch/event/py/test2/testing/test_runner.py	Tue Aug  5 11:24:19 2008
+++ (empty file)
@@ -1,244 +0,0 @@
-
-import py
-from py.__.test2 import runner
-from py.__.test2.outcome import Exit
-from py.__.test2 import pypresent
-
-class MockItem:
-    def __init__(self, func):
-        self.func = func
-        self._config = py.test2.config._reparse([])
-
-    def __getstate__(self):
-        return (self._config, )
-    def __setstate__(self, repr):
-        self._config, = repr 
-
-    def _get_collector_trail(self):
-        return "MockItem.trail"
-
-    def repr_path(self):
-        return "MockItem.repr_path"
-
-    def repr_run(self, runinfo):
-        excinfo = runinfo.excinfo
-        if not excinfo:
-            return ("(%s)MockItem.repr_run" %(runinfo.outerr,))
-        else:            
-            assert isinstance(excinfo, py.code.ExceptionInfo)
-            return ("(%s)MockItem.repr_run: %s" %(
-                runinfo.outerr, excinfo.exconly()))
-        
-    def execute(self):
-        self.func() 
-
-class MockCapture:
-    def reset(self):
-        return "out", "err"
-
-class MockSetupState:
-    def prepare(self, item):
-        return
-    def teardown_exact(self, item):
-        pass
-    def teardown_all(self):
-        pass
-
-class RunnerTests:
-    def run(self, func, setupstate=None, getcapture=None, pdb=None):
-        runner = self.getrunner()
-        item = MockItem(func)
-        if not setupstate: setupstate = MockSetupState()
-        if not getcapture: getcapture = MockCapture
-        testrep = runner(item, setupstate, getcapture, pdb=pdb)
-        return testrep
-
-    def test_run_pass(self):
-        def test_func():
-            pass
-        testrep = self.run(test_func)
-        assert testrep.passed
-       
-    def test_run_fail(self):
-        def testfunc():
-            assert 0 
-        testrep = self.run(testfunc)
-        assert not testrep.passed 
-        assert testrep.failed 
-
-    def test_run_skip(self):
-        def testfunc():
-            py.test2.skip("hello")
-        testrep = self.run(testfunc)
-        assert testrep.skipped 
-        assert not testrep.passed
-        assert not testrep.failed
-        assert not testrep.failed_setup
-
-    def test_systemexit_does_not_bail_out(self):
-        def testfunc():
-            raise SystemExit(42)
-        try:
-            testrep = self.run(testfunc)
-        except SystemExit:
-            py.test.fail("runner did not catch SystemExit")
-        assert testrep.failed
-        assert not testrep.failed_setup
-        assert testrep.repr_run.find("SystemExit") != -1
-
-    def test_exit_does_bail_out(self):
-        def testfunc():
-            raise Exit()
-        py.test.raises(Exit, "self.run(testfunc)")
-
-    def test_runner_handles_skip_in_prepare(self):
-        l = []
-        class MySetupState:
-            def prepare(self, item):
-                py.test2.skip("skip")
-        def testfunc(): pass
-        def pythonreprrun(runinfo, title=None): return ""
-        py.magic.patch(pypresent, "python_repr_run", pythonreprrun)
-        try:
-            testrep = self.run(testfunc, setupstate=MySetupState())
-        finally:
-            py.magic.revert(pypresent, "python_repr_run")
-        assert testrep.skipped 
-        assert not testrep.failed
-        assert not testrep.passed
-
-    def test_runnner_handles_setupfailure_in_prepare(self):
-        l = []
-        class MySetupState:
-            def prepare(self, item):
-                raise ValueError(2)
-        def testfunc():
-            pass
-        def pythonreprrun(runinfo, title=None):
-            assert runinfo.excinfo.errisinstance(ValueError)
-            return "pythonreprrun"
-        py.magic.patch(pypresent, "python_repr_run", pythonreprrun)
-        try:
-            testrep = self.run(testfunc, setupstate=MySetupState())
-        finally:
-            py.magic.revert(pypresent, "python_repr_run")
-        assert testrep.failed_setup
-        assert testrep.failed
-        assert not testrep.passed
-        assert testrep.repr_run == "pythonreprrun"
-
-    def test_runner_handles_capture(self):
-        l = []
-        class MyCapture:
-            def __init__(self):
-                l.append("init")
-            def reset(self):
-                l.append("reset")
-                return ":".join(l), ""
-        def testfunc():
-            l.append("run")
-        testrep = self.run(testfunc, getcapture=MyCapture)
-        assert testrep.passed
-        assert testrep.repr_run.find("init:run:reset") != -1
-    
-        def testfuncfail():
-            l.append("run")
-            assert 0
-        l[:] = []
-        testrep = self.run(testfuncfail, getcapture=MyCapture)
-        assert testrep.repr_run.find("init:run:reset") != -1
-        assert testrep.failed
-
-class TestBasicRunner(RunnerTests):
-    def getrunner(self):
-        return runner.basic_run_report
-
-    def test_runner_handles_setupstate(self):
-        l = []
-        class MySetupState:
-            def prepare(self, item):
-                l.append("prepare")
-            def teardown_exact(self, item):
-                l.append("teardown_exact")
-        def testfunc():
-            l.append("run")
-        testrep = self.run(testfunc, setupstate=MySetupState())
-        assert l == ["prepare", "run", "teardown_exact"]
-        assert testrep.passed
-    
-        def testfuncfail():
-            l.append("run")
-            assert 0
-        l[:] = []
-        testrep = self.run(testfuncfail, setupstate=MySetupState())
-        assert l == ["prepare", "run", "teardown_exact"]
-        assert testrep.failed
-
-
-    def test_runnner_handles_setupfailure_in_eager_teardown(self):
-        l = []
-        class MySetupState:
-            def prepare(self, item):
-                pass
-            def teardown_exact(self, item):
-                raise ValueError(17)
-        def testfunc():
-            l.append(0)
-            pass
-        def pythonreprrun(runinfo, title=None):
-            l.append(runinfo)
-        py.magic.patch(pypresent, "python_repr_run", pythonreprrun)
-        try:
-            testrep = self.run(testfunc, setupstate=MySetupState())
-        finally:
-            py.magic.revert(pypresent, "python_repr_run")
-        assert testrep.failed_teardown
-        assert testrep.failed
-        assert not testrep.passed
-        assert len(l) == 2 # means that testfunc didnt run
-        assert l[0] == 0
-        assert l[1].excinfo.errisinstance(ValueError)
-
-    def test_invoking_pdb(self):
-        l = []
-        def testfunc():
-            raise ValueError(7)
-        def mypdb(runinfo):
-            l.append(runinfo)
-        testrep = self.run(testfunc, pdb=mypdb)
-        assert len(l) == 1
-        assert l[0].excinfo.exconly().find("ValueError: 7") != -1
-
-class TestForkRunner(RunnerTests):
-    def setup_class(cls):
-        if not hasattr(py.std.os, 'fork'):
-            py.test.skip("need os.fork()")
-
-    def getrunner(self):
-        return runner.forked_run_report
-
-    def test_exit_does_bail_out(self):
-        def testfunc():
-            raise Exit()
-        py.test.raises(Exit, "self.run(testfunc)")
-
-    def test_suicide(self):
-        def testfunc():
-            import os
-            os.kill(os.getpid(), 15)
-        crashes = []
-        def myreport(item, result):
-            crashes.append((item, result))
-        py.magic.patch(runner, "report_crash", myreport)
-        try:
-            self.run(testfunc)
-        finally:
-            py.magic.revert(runner, "report_crash")
-        assert len(crashes) == 1
-        result = crashes[0][1]
-        assert result.signal == 15
-       
-        testrep = runner.report_crash(*crashes[0])
-        assert testrep.repr_run.find("CRASHED") != -1
-        assert testrep.failed_crashed 
-        assert testrep.failed

Added: py/branch/event/py/test2/testing/test_runner_functional.py
==============================================================================
--- (empty file)
+++ py/branch/event/py/test2/testing/test_runner_functional.py	Tue Aug  5 11:24:19 2008
@@ -0,0 +1,249 @@
+import py
+from py.__.test2.testing.suptest import InlineCollection
+from py.__.test2.runner import basic_run_report, forked_run_report, basic_collect_report
+
+class BaseTests(InlineCollection):
+    def test_passfunction(self):
+        ev = self.runitem("""
+            def test_func():
+                pass
+        """)
+        assert ev.passed 
+        assert not ev.failed
+                
+    def test_failfunction(self):
+        ev = self.runitem("""
+            def test_func():
+                assert 0
+        """)
+        assert not ev.passed 
+        assert not ev.skipped 
+        assert ev.failed 
+        # assert ev.failed.location.lineno == 2
+        # assert ev.failed.location.path
+        # assert ev.failed.reason == "AssertionError: 0"
+        # assert ev.failed.failurerepr 
+
+    def test_skipfunction(self):
+        ev = self.runitem("""
+            import py
+            def test_func():
+                py.test2.skip("hello")
+        """)
+        assert not ev.failed 
+        assert not ev.passed 
+        assert ev.skipped 
+        #assert ev.skipped.reason == "hello"
+        #assert ev.skipped.location.lineno == 3
+        #assert ev.skipped.location.path
+
+    def test_skip_in_setup_function(self):
+        ev = self.runitem("""
+            import py
+            def setup_function(func):
+                py.test2.skip("hello")
+            def test_func():
+                pass
+        """)
+        print ev
+        assert not ev.failed 
+        assert not ev.passed 
+        assert ev.skipped 
+        #assert ev.skipped.reason == "hello"
+        #assert ev.skipped.location.lineno == 3
+        #assert ev.skipped.location.lineno == 3
+
+    def test_failure_in_setup_function(self):
+        ev = self.runitem("""
+            import py
+            def setup_function(func):
+                raise ValueError(42)
+            def test_func():
+                pass
+        """)
+        print ev
+        assert not ev.skipped 
+        assert not ev.passed 
+        assert ev.failed 
+
+    def test_failure_in_teardown_function(self):
+        ev = self.runitem("""
+            import py
+            def teardown_function(func):
+                raise ValueError(42)
+            def test_func():
+                pass
+        """)
+        print ev
+        assert not ev.skipped 
+        assert not ev.passed 
+        assert ev.failed 
+        #assert ev.failed.when == "teardown" 
+        #assert ev.failed.where.lineno == 3
+        #assert ev.failed.entries 
+
+    def test_custom_failure_repr(self):
+        self.makeconftest("""
+            import py
+            class Function(py.test2.collect.Function):
+                def getfailurerepr(self, excinfo):
+                    return "hello" 
+        """)
+        ev = self.runitem("""
+            import py
+            def test_func():
+                assert 0
+        """)
+        assert not ev.skipped 
+        assert not ev.passed 
+        assert ev.failed 
+        #assert ev.failed.when == "execute"
+        #assert ev.failed.where.lineno == 3
+        #assert ev.failed.where.path.basename == "test_func.py" 
+        #assert ev.failed.failurerepr == "hello"
+
+    def test_failure_in_setup_function_ignores_custom_failure_repr(self):
+        self.makeconftest("""
+            import py
+            class Function(py.test2.collect.Function):
+                def getfailurerepr(self, excinfo):
+                    assert 0
+        """)
+        ev = self.runitem("""
+            import py
+            def setup_function(func):
+                raise ValueError(42)
+            def test_func():
+                pass
+        """)
+        print ev
+        assert not ev.skipped 
+        assert not ev.passed 
+        assert ev.failed 
+        #assert ev.failed.when == "setup"
+        #assert ev.failed.where.lineno == 3
+        #assert ev.failed.where.path.basename == "test_func.py" 
+        #assert instanace(ev.failed.failurerepr, PythonFailureRepr)
+
+    def test_capture_in_func(self):
+        ev = self.runitem("""
+            import py
+            def setup_function(func):
+                print >>py.std.sys.stderr, "in setup"
+            def test_func():
+                print "in function"
+                assert 0
+            def teardown_func(func):
+                print "in teardown"
+        """)
+        assert ev.failed 
+        # out, err = ev.failed.outerr
+        # assert out == ['in function\nin teardown\n']
+        # assert err == ['in setup\n']
+        
+    def test_systemexit_does_not_bail_out(self):
+        try:
+            ev = self.runitem("""
+                def test_func():
+                    raise SystemExit(42)
+            """)
+        except SystemExit:
+            py.test.fail("runner did not catch SystemExit")
+        assert ev.failed
+        assert not ev.failed_setup
+        #assert ev.failurerepr 
+
+    def test_exit_propagates(self):
+        from py.__.test2.outcome import Exit
+        try:
+            self.runitem("""
+                from py.__.test2.outcome import Exit
+                def test_func():
+                    raise Exit()
+            """)
+        except Exit:
+            pass
+        else: 
+            py.test.fail("did not raise")
+
+class TestExecutionNonForked(BaseTests):
+    def getrunner(self):
+        def runner(item):
+            return basic_run_report(
+                item, item._setupstate, 
+                item._config._getcapture
+            )
+        return runner
+
+    def test_keyboardinterrupt_propagates(self):
+        from py.__.test2.outcome import Exit
+        try:
+            self.runitem("""
+                def test_func():
+                    raise KeyboardInterrupt("fake")
+            """)
+        except KeyboardInterrupt, e:
+            pass
+        else: 
+            py.test.fail("did not raise")
+
+    def test_invoking_pdb(self):
+        py.test.skip("pdb test needs porting")
+        l = []
+        def testfunc():
+            raise ValueError(7)
+        def mypdb(runinfo):
+            l.append(runinfo)
+        testrep = self.run(testfunc, pdb=mypdb)
+        assert len(l) == 1
+        assert l[0].excinfo.exconly().find("ValueError: 7") != -1
+
+class TestExecutionForked(BaseTests): 
+    def getrunner(self):
+        def runner(item):
+            return forked_run_report(
+                item, item._setupstate, 
+                item._config._getcapture
+            )
+        return runner
+
+    def test_suicide(self):
+        ev = self.runitem("""
+            def test_func():
+                import os
+                os.kill(os.getpid(), 15)
+        """)
+        assert ev.failed_crashed 
+        assert ev.failed
+
+class TestCollectionEvent(InlineCollection):
+    def setup_class(cls): 
+        py.test.skip("xxx")
+
+    def test_collect_result(self):
+        col = self.getmodulecol("""
+            def test_func1():
+                pass
+            class TestClass:
+                pass
+        """)
+        ev = basic_collect_report(col)
+        assert not ev.failed
+        assert not ev.skipped
+        assert ev.passed 
+        res = ev.result 
+        assert len(res) == 2
+        assert l[0].name == "test_func1" 
+        assert l[1].name == "TestClass" 
+
+    def test_skip_at_module_scope(self):
+        col = self.getmodulecol("""
+            import py
+            py.test2.skip("hello")
+            def test_func():
+                pass
+        """)
+        ev = basic_collect_report(col)
+        assert not ev.failed 
+        assert not ev.passed 
+        assert ev.skipped 

Modified: py/branch/event/py/test2/testing/test_session.py
==============================================================================
--- py/branch/event/py/test2/testing/test_session.py	(original)
+++ py/branch/event/py/test2/testing/test_session.py	Tue Aug  5 11:24:19 2008
@@ -280,36 +280,3 @@
         session = config.initsession()
         assert isinstance(session, RemoteTerminalSession)
 
-    def test_looponfailing_loops(self):
-        py.test.skip("thought needed to nicely test --looponfailing")
-        py.test.skip("xxx check events")
-        o = tmpdir.ensure('looponfailing', dir=1) 
-        tfile = o.join('test_looponfailing.py')
-        tfile.write(py.code.Source("""
-            def test_1():
-                assert 1 == 0 
-        """))
-        print py.std.sys.executable
-        config = py.test2.config._reparse(['--looponfailing', str(o)])
-        session = config.initsession()
-        failures = session.main()
-        
-        cls = config._getsessionclass() 
-        out = py.std.Queue.Queue() 
-        session = cls(config, out.put) 
-        pool = py._thread.WorkerPool() 
-        reply = pool.dispatch(session.main)
-        while 1: 
-            s = out.get(timeout=5.0)
-            if s.find('1 failed') != -1: 
-                break 
-            print s
-        else: 
-            py.test2.fail("did not see test_1 failure") 
-        # XXX we would like to have a cleaner way to finish 
-        try: 
-            reply.get(timeout=5.0) 
-        except IOError, e: 
-            assert str(e).lower().find('timeout') != -1 
-
-



More information about the pytest-commit mailing list