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

hpk at codespeak.net hpk at codespeak.net
Tue Feb 19 18:25:48 CET 2008


Author: hpk
Date: Tue Feb 19 18:25:48 2008
New Revision: 51648

Modified:
   py/branch/event/py/test2/collect.py
   py/branch/event/py/test2/executor.py
   py/branch/event/py/test2/present.py
   py/branch/event/py/test2/testing/suptest.py
   py/branch/event/py/test2/testing/test_present.py
   py/branch/event/py/test2/testing/test_session.py
Log:
* test items can now return their own failure representation
  via repr_failure(excinfo)
* shift test helper to become general


Modified: py/branch/event/py/test2/collect.py
==============================================================================
--- py/branch/event/py/test2/collect.py	(original)
+++ py/branch/event/py/test2/collect.py	Tue Feb 19 18:25:48 2008
@@ -25,6 +25,7 @@
 """ 
 from __future__ import generators 
 import py
+from present import FuncPresenter
 
 sysex = (KeyboardInterrupt, SystemExit, GeneratorExit)
 
@@ -449,6 +450,12 @@
         if meth is not None: 
             return meth(self.obj) 
 
+    def repr_failure(self, excinfo):
+        """ return a textual failure representation for this item. """ 
+        p = FuncPresenter(self._config) 
+        p.repr_failure(self, excinfo)
+        return p.stringio.getvalue()
+
 class Generator(FunctionMixin, PyCollectorMixin, Collector): 
     def listdir(self): 
         self._prepare()

Modified: py/branch/event/py/test2/executor.py
==============================================================================
--- py/branch/event/py/test2/executor.py	(original)
+++ py/branch/event/py/test2/executor.py	Tue Feb 19 18:25:48 2008
@@ -32,9 +32,7 @@
         else:
             testrep.failed = True
         testrep.exconly = excinfo.exconly()
-        p = present.Presenter(self.config)
-        p.repr_failure(self.item, excinfo)
-        testrep.repr_failure = p.stringio.getvalue()
+        testrep.repr_failure = self.item.repr_failure(excinfo)
 
     def execute(self, capture=True):
         testrep = repevent.ItemTestReport(self.item._get_collector_trail())

Modified: py/branch/event/py/test2/present.py
==============================================================================
--- py/branch/event/py/test2/present.py	(original)
+++ py/branch/event/py/test2/present.py	Tue Feb 19 18:25:48 2008
@@ -43,15 +43,13 @@
     except (TypeError, ValueError):
         return str(v)
 
-class Presenter(object):
-    """ Class used for presentation of various objects,
-    sharing common output style
-    """
+class FuncPresenter(object):
+    """ presenting information about failing Functions and Generators. """ 
     def __init__(self, config, out=None):
         self.config = config
         if out is None:
             self.stringio = py.std.StringIO.StringIO()
-            out = getout(self.stringio)
+            out = getout(self.stringio) 
         assert hasattr(out, 'write'), out
         self.out = out
 
@@ -133,10 +131,7 @@
 
     def repr_failure(self, item, excinfo):
         traceback = self.filtertraceback(item, excinfo.traceback) 
-        if excinfo.errisinstance(RuntimeError):
-            recursionindex = traceback.recursionindex()
-        else:
-            recursionindex = None
+        recursionindex = traceback.recursionindex()
 
         repr_tb = getattr(self, "repr_tb_" + self.config.option.tbstyle)
         repr_tb(item, excinfo, traceback, recursionindex)

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 Feb 19 18:25:48 2008
@@ -117,3 +117,14 @@
     print "created test file", p
     p.dirpath("__init__.py").ensure() 
     return p
+
+def getfailing(source):
+    tfile = makeuniquepyfile(source)
+    sorter = events_from_cmdline([tfile])
+    # get failure base info 
+    failevents = sorter.get(repevent.ItemFinish)
+    assert len(failevents) == 1
+    item = failevents[0].item 
+    excinfo = failevents[0].excinfo 
+    return item, excinfo 
+

Modified: py/branch/event/py/test2/testing/test_present.py
==============================================================================
--- py/branch/event/py/test2/testing/test_present.py	(original)
+++ py/branch/event/py/test2/testing/test_present.py	Tue Feb 19 18:25:48 2008
@@ -40,22 +40,9 @@
     def setup_class(cls):
         cls.tmpdir = py.test2.ensuretemp(cls.__name__) 
 
-    def getconfig(self, args=()):
-        return py.test2.config._reparse([self.tmpdir] + list(args))
-
     def getpresenter(self, cmdlinearg=""):
-        config = self.getconfig(cmdlinearg.split())
-        return present.Presenter(config)
-
-    def getfailing(self, source):
-        tfile = suptest.makeuniquepyfile(source)
-        sorter = suptest.events_from_cmdline([tfile])
-        # get failure base info 
-        failevents = sorter.get(repevent.ItemFinish)
-        assert len(failevents) == 1
-        item = failevents[0].item 
-        excinfo = failevents[0].excinfo 
-        return item, excinfo 
+        config = py.test2.config._reparse([self.tmpdir, cmdlinearg])
+        return present.FuncPresenter(config)
 
     def gentest(self, check, **options):
         print "using config options", options
@@ -103,7 +90,7 @@
             assert result.find(key) != -1 
 
     def test_repr_failure_simple(self):
-        item, excinfo = self.getfailing("""
+        item, excinfo = suptest.getfailing("""
             def test_one():
                 # failingsourcemarker 
                 assert 42 == 43
@@ -118,7 +105,7 @@
         assert re.search(r">.*assert 42 == 43", s) 
 
     def test_repr_failure_recursive_funcs(self):
-        item, excinfo = self.getfailing("""
+        item, excinfo = suptest.getfailing("""
             def rec2(x):
                 return rec1(x+1)
             def rec1(x):
@@ -140,7 +127,7 @@
         self.gentest(check, showlocals=True)
 
     def test_repr_failing_setup(self):
-        item, excinfo = self.getfailing("""
+        item, excinfo = suptest.getfailing("""
             def setup_module(mod):
                 xyz 
             def test_one():
@@ -156,3 +143,19 @@
         self.gentest(check, tbstyle="short")
         self.gentest(check, fulltrace=True)
         self.gentest(check, showlocals=True)
+
+    def test_repr_failing_generator(self):
+        py.test.skip("XXX fix generator repr")
+        item, excinfo = suptest.getfailing("""
+            def test_gen():
+                def check(x):
+                    assert x
+                yield check, 0
+        """)
+        s = item.repr_failure(excinfo)
+        lines = s.split("\n")
+        assert lines[1].find("test_gen[0] -> check(0)") != -1
+        assert lines[2].find("def check(x):") != -1
+        assert 0
+        
+

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 Feb 19 18:25:48 2008
@@ -1,6 +1,6 @@
 import py
 from py.__.test2 import repevent 
-import suptest 
+import suptest, setupdata
 
 def setup_module(mod):
     mod.tmpdir = py.test.ensuretemp(mod.__name__) 
@@ -266,3 +266,20 @@
         testrep = sorter.getreport("test_two")
         assert testrep.stdout.startswith("failed") 
         assert not testrep.stderr
+
+    def test_function_repr_failure(self):
+        o = setupdata.getexamplefile("filetest.py")
+        conftest = o.dirpath('conftest.py')
+        conftest.write(py.code.Source("""
+            import py
+            l = []
+            class Function(py.test2.collect.Function): 
+                def repr_failure(self, excinfo):
+                    return "custom_repr %s" % self.name 
+        """))
+        sorter = suptest.events_from_cmdline([o])
+        sorter.assertoutcome(failed=2)
+        rep = sorter.getreport("test_one")
+        assert rep.repr_failure == "custom_repr test_one"
+        rep = sorter.getreport("test_method_one")
+        assert rep.repr_failure == "custom_repr test_method_one"



More information about the pytest-commit mailing list