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

hpk at codespeak.net hpk at codespeak.net
Fri Aug 15 23:55:59 CEST 2008


Author: hpk
Date: Fri Aug 15 23:55:58 2008
New Revision: 57301

Modified:
   py/branch/event/py/test2/config.py
   py/branch/event/py/test2/dsession/dsession.py
   py/branch/event/py/test2/testing/test_config.py
   py/branch/event/py/test2/testing/test_session.py
Log:
* let DSession also generate ItemStart and CollectionStart events 
* unify tests for DSession and Session
* --collectonly takes precedence before dsession options



Modified: py/branch/event/py/test2/config.py
==============================================================================
--- py/branch/event/py/test2/config.py	(original)
+++ py/branch/event/py/test2/config.py	Fri Aug 15 23:55:58 2008
@@ -177,7 +177,9 @@
 
     def _getsessionname(self):
         """ return default session name as determined from options. """
-        if self.option.looponfailing:
+        if self.option.collectonly:
+            name = 'Session'
+        elif self.option.looponfailing:
             name = 'LooponfailingSession'
         elif self.option.numprocesses or self.option.dist or self.option.executable: 
             name = 'DSession'

Modified: py/branch/event/py/test2/dsession/dsession.py
==============================================================================
--- py/branch/event/py/test2/dsession/dsession.py	(original)
+++ py/branch/event/py/test2/dsession/dsession.py	Fri Aug 15 23:55:58 2008
@@ -181,8 +181,10 @@
         for next in colitems:
             if isinstance(next, Item):
                 senditems.append(next)
+                self.bus.notify(event.ItemStart(next))
             else:
                 ev = basic_collect_report(next)
+                self.bus.notify(event.CollectionStart(next))
                 self.queue.put(ev)
         self.senditems(senditems)
 

Modified: py/branch/event/py/test2/testing/test_config.py
==============================================================================
--- py/branch/event/py/test2/testing/test_config.py	(original)
+++ py/branch/event/py/test2/testing/test_config.py	Fri Aug 15 23:55:58 2008
@@ -197,7 +197,7 @@
         s = eventlog.read()
         assert s.find("TestrunStart") != -1
 
-    def test_implied_lsession(self):
+    def test_implied_dsession(self):
         for x in 'startserver runbrowser rest'.split():
             config = py.test2.config._reparse([self.tmpdir, '--dist', '--%s' % x])
             assert config._getsessionname() == 'DSession'
@@ -218,6 +218,10 @@
         config = py.test2.config._reparse([self.tmpdir, '-f', 
                                           '--dist', '--exec=x'])
         assert config._getsessionname() == 'LooponfailingSession'
+        config = py.test2.config._reparse([self.tmpdir, '-f', '-n3',
+                                          '--dist', '--exec=x', 
+                                          '--collectonly'])
+        assert config._getsessionname() == 'Session'
 
     def test_sessionname_lookup_custom(self):
         self.tmpdir.join("conftest.py").write(py.code.Source("""

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	Fri Aug 15 23:55:58 2008
@@ -56,30 +56,82 @@
         item = dlist[0].items[0]
         assert item.name == "test_one" 
 
-class TestSession:
+class SessionTests(suptest.InlineCollection):
+    def events_from_cmdline(self, *args):
+        paths = [p for p in args if isinstance(p, py.path.local)]
+        if not paths:
+            args = (self.tmpdir,) + args
+        config = self.parseconfig(*args)
+        self.session = config.initsession()
+        self.sorter = suptest.EventSorter(config, self.session)
+        self.session.main()
+        return self.sorter
+
+    def events_from_runsource(self, source, *args):
+        p = self.makepyfile(test_source=source)
+        return self.events_from_cmdline(p, *args)
+
+    def makepyfile(self, *args, **kw):
+        self.tmpdir.ensure('__init__.py')
+        return super(SessionTests, self).makepyfile(*args, **kw)
 
-    def test_terminal(self): 
-        sorter = suptest.events_run_example("filetest.py") 
-        passed, skipped, failed = sorter.countoutcomes()
-        assert failed == 2
-        assert passed == skipped == 0
+    def test_basic_testitem_events(self):
+        tfile = self.makepyfile(test_one="""
+            def test_one(): 
+                pass
+            def test_one_one():
+                assert 0
+            def test_other():
+                raise ValueError(23)
+            def test_two(someargs):
+                pass
+        """)
+        sorter = self.events_from_cmdline(tfile)
+        passed, skipped, failed = sorter.listoutcomes()
+        assert len(skipped) == 0
+        assert len(passed) == 1
+        assert len(failed) == 3  
+        assert failed[0].colitem.name == "test_one_one"
+        assert failed[1].colitem.name == "test_other"
+        assert failed[2].colitem.name == "test_two"
+        itemstarted = sorter.get(event.ItemStart)
+        assert len(itemstarted) == 4
+        colstarted = sorter.get(event.CollectionStart)
+        assert len(colstarted) == 1
+        col = colstarted[0].collector
+        assert isinstance(col, py.test2.collect.Module)
 
-    def test_syntax_error_module(self): 
-        sorter = suptest.events_run_example("syntax_error.py")
+    def test_nested_import_error(self): 
+        tfile = self.makepyfile(test_one="""
+            import import_fails
+            def test_this():
+                assert import_fails.a == 1
+        """, import_fails="""
+            import does_not_work 
+            a = 1
+        """)
+        sorter = self.events_from_cmdline()
         l = sorter.getfailedcollections()
-        assert len(l) == 1
+        assert len(l) == 1 
         out = l[0].outcome.longrepr.reprcrash.message
-        assert out.find(str('syntax_error.py')) != -1
-        assert out.find(str('not python')) != -1
+        assert out.find('does_not_work') != -1 
 
-    def test_exit_first_problem(self): 
-        sorter = suptest.events_run_example('filetest.py', '--exitfirst')
-        passed, skipped, failed = sorter.countoutcomes()
-        assert failed == 1
-        assert passed == skipped == 0
+    def test_raises_output(self): 
+        self.makepyfile(test_one="""
+            import py
+            def test_raises_doesnt():
+                py.test2.raises(ValueError, int, "3")
+        """)
+        sorter = self.events_from_cmdline()
+        passed, skipped, failed = sorter.listoutcomes()
+        assert len(failed) == 1
+        out = failed[0].outcome.longrepr.reprcrash.message
+        if not out.find("DID NOT RAISE") != -1: 
+            print out
+            py.test2.fail("incorrect raises() output") 
 
     def test_generator_yields_None(self): 
-        sorter = suptest.events_from_runsource(""" 
+        sorter = self.events_from_runsource("""
             def test_1():
                 yield None 
         """)
@@ -88,21 +140,77 @@
         i = out.find('TypeError') 
         assert i != -1 
 
-    def test_raises_output(self): 
-        sorter = suptest.events_from_runsource('''
+    def test_syntax_error_module(self): 
+        sorter = self.events_from_runsource("this is really not python")
+        l = sorter.getfailedcollections()
+        assert len(l) == 1
+        out = l[0].outcome.longrepr.reprcrash.message
+        assert out.find(str('not python')) != -1
+
+    def test_exit_first_problem(self): 
+        sorter = self.events_from_runsource("""
+            def test_one(): assert 0
+            def test_two(): assert 0
+        """, '--exitfirst')
+        passed, skipped, failed = sorter.countoutcomes()
+        assert failed == 1
+        assert passed == skipped == 0
+
+    def test_broken_repr(self):
+        self.makepyfile(test_broken="""
             import py
-            def test_raises_doesnt():
-                py.test2.raises(ValueError, int, "3")
-        ''')
+            class BrokenRepr1:
+                foo=0
+                def __repr__(self):
+                    raise Exception("Ha Ha fooled you, I'm a broken repr().")
+            class BrokenRepr2:
+                foo=0
+                def __repr__(self):
+                    raise "Ha Ha fooled you, I'm a broken repr()."
+            
+            class TestBrokenClass:
+                def test_explicit_bad_repr(self):
+                    t = BrokenRepr1()
+                    py.test2.raises(Exception, 'repr(t)')
+                    
+                def test_implicit_bad_repr1(self):
+                    t = BrokenRepr1()
+                    assert t.foo == 1
+
+                def test_implicit_bad_repr2(self):
+                    t = BrokenRepr2()
+                    assert t.foo == 1
+        """)
+        sorter = self.events_from_cmdline()
         passed, skipped, failed = sorter.listoutcomes()
-        assert len(failed) == 1
+        assert len(failed) == 2
         out = failed[0].outcome.longrepr.reprcrash.message
-        if not out.find("DID NOT RAISE") != -1: 
-            print out
-            py.test2.fail("incorrect raises() output") 
+        assert out.find("""[Exception("Ha Ha fooled you, I'm a broken repr().") raised in repr()]""") != -1 #'
+        out = failed[1].outcome.longrepr.reprcrash.message
+        assert out.find("[unknown exception raised in repr()]") != -1
+
+class TestNewSession(SessionTests):
+    def test_pdb_run(self):
+        tfile = self.makepyfile(test_one="""
+            def test_usepdb(): 
+                assert 0
+        """)
+        l = []
+        def mypdb(*args):
+            l.append(args)
+        py.magic.patch(py.__.test2.custompdb, 'post_mortem', mypdb)
+        try:
+            sorter = self.events_from_cmdline('--pdb')
+        finally:
+            py.magic.revert(py.__.test2.custompdb, 'post_mortem')
+        rep = sorter.getreport("test_usepdb")
+        assert rep.failed
+        assert len(l) == 1
+        tb = py.code.Traceback(l[0][0])
+        assert tb[-1].name == "test_usepdb" 
 
     def test_order_of_execution(self): 
-        sorter = suptest.events_from_runsource("""
+        sorter = self.events_from_runsource("""
             l = []
             def test_1():
                 l.append(1)
@@ -126,50 +234,26 @@
         assert passed == 7
         # also test listnames() here ... 
 
-    def test_nested_import_error(self): 
-        tfile = suptest.makeuniquepyfile("""
-            import import_fails
-            def test_this():
-                assert import_fails.a == 1
-        """)
-        tfile.dirpath('import_fails.py').write(py.code.Source("""
-            import does_not_work 
-            a = 1
-        """))
-        sorter = suptest.events_from_cmdline([tfile])
-        l = sorter.getfailedcollections()
-        assert len(l) == 1 
-        out = l[0].outcome.longrepr.reprcrash.message
-        assert out.find('does_not_work') != -1 
-
-    def test_safe_repr(self):
-        sorter = suptest.events_run_example("brokenrepr.py")
-        passed, skipped, failed = sorter.listoutcomes()
-        assert len(failed) == 2
-        out = failed[0].outcome.longrepr.reprcrash.message
-        assert out.find("""[Exception("Ha Ha fooled you, I'm a broken repr().") raised in repr()]""") != -1 #'
-        out = failed[1].outcome.longrepr.reprcrash.message
-        assert out.find("[unknown exception raised in repr()]") != -1
-
     def test_collect_only_with_various_situations(self):
-        p = suptest.makeuniquepyfile("""
-            def test_one():
-                raise ValueError()
+        p = self.makepyfile(
+            test_one="""
+                def test_one():
+                    raise ValueError()
+
+                class TestX:
+                    def test_method_one(self):
+                        pass
 
-            class TestX:
-                def test_method_one(self):
+                class TestY(TestX):
                     pass
-
-            class TestY(TestX):
-                pass
-        """)
-        p.dirpath("test_two.py").write(py.code.Source("""
-            import py
-            py.test.skip('xxx')
-        """))
-        p.dirpath("test_three.py").write("xxxdsadsadsadsa")
-
-        sorter = suptest.events_from_cmdline([p.dirpath(), '--collectonly'])
+            """, 
+            test_two="""
+                import py
+                py.test.skip('xxx')
+            """, 
+            test_three="xxxdsadsadsadsa"
+        )
+        sorter = self.events_from_cmdline('--collectonly')
        
         itemstarted = sorter.get(event.ItemStart)
         assert len(itemstarted) == 3
@@ -183,43 +267,8 @@
         assert len(colfail) == 1
         assert len(colskipped) == 1
 
-    def test_pdb_run(self):
-        tfile = suptest.makeuniquepyfile("""
-            def test_usepdb(): 
-                assert 0
-        """)
-        l = []
-        def mypdb(*args):
-            l.append(args)
-        py.magic.patch(py.__.test2.custompdb, 'post_mortem', mypdb)
-        try:
-            sorter = suptest.events_from_cmdline([tfile, '--pdb'])
-        finally:
-            py.magic.revert(py.__.test2.custompdb, 'post_mortem')
-
-        rep = sorter.getreport("test_usepdb")
-        assert rep.failed
-        assert len(l) == 1
-        tb = py.code.Traceback(l[0][0])
-        assert tb[-1].name == "test_usepdb" 
-
-    def test_basic_testitem_events(self):
-        tfile = suptest.makeuniquepyfile(""" 
-            def test_one(): 
-                pass
-            def test_one_one():
-                assert 0
-            def test_other():
-                raise ValueError(23)
-            def test_two(someargs):
-                pass
-        """)
-        sorter = suptest.events_from_cmdline([tfile])
-        passed, skipped, failed = sorter.listoutcomes()
-        assert len(skipped) == 0
-        assert len(passed) == 1
-        assert len(failed) == 3  
-        assert failed[0].colitem.name == "test_one_one"
-        assert failed[1].colitem.name == "test_other"
-        assert failed[2].colitem.name == "test_two"
-
+class TestNewSessionDSession(SessionTests):
+    def parseconfig(self, *args):
+        args = ('-n1',) + args
+        return SessionTests.parseconfig(self, *args)
+    



More information about the pytest-commit mailing list