details:   http://hg.sympy.org/sympy/rev/88b14691ea51
changeset: 1775:88b14691ea51
user:      Ondrej Certik <[EMAIL PROTECTED]>
date:      Wed Oct 08 17:36:02 2008 +0200
description:
pytest.py: implements raises, XFail, XPass, skip and other py.test things

If "import py" succeeds, the py.test is used. If it fails, our own
implementation is used instead. This allows our testsuite not to depend on
py.test.

Signed-off-by: Ondrej Certik <[EMAIL PROTECTED]>
Signed-off-by: Riccardo Gori <[EMAIL PROTECTED]>

diffs (203 lines):

diff -r cac51147f4f8 -r 88b14691ea51 sympy/utilities/pytest.py
--- a/sympy/utilities/pytest.py Wed Oct 08 17:36:02 2008 +0200
+++ b/sympy/utilities/pytest.py Wed Oct 08 17:36:02 2008 +0200
@@ -3,90 +3,129 @@
 # XXX this should be integrated into py.test
 # XXX but we can't force everyone to install py-lib trunk
 
-# tested with py-lib 0.9.0
-from py.__.test.outcome import Outcome, Passed, Failed, Skipped
-from py.__.test.terminal.terminal import TerminalSession
-from time import time as now
+import sys
 
-__all__ = ['XFAIL']
+try:
+    import py
+    disabled = False
+except ImportError:
+    disabled = True
 
-class XFail(Outcome):
-    pass
+def raises(ExpectedException, code):
+    assert isinstance(code, str)
+    frame = sys._getframe(1)
+    loc = frame.f_locals.copy()
+    try:
+        exec code in frame.f_globals, loc
+    except ExpectedException:
+        return
+    raise Exception("DID NOT RAISE")
 
-class XPass(Outcome):
-    pass
+if disabled:
+    class XFail(Exception):
+        pass
 
-TerminalSession.typemap[XFail] = 'f'
-TerminalSession.typemap[XPass] = 'X'
+    class XPass(Exception):
+        pass
 
-TerminalSession.namemap[XFail] = 'XFAIL'
-TerminalSession.namemap[XPass] = '*** XPASS ***'
+    class Skipped(Exception):
+        pass
 
+    def XFAIL(func):
+        def wrapper():
+            try:
+                func()
+            except Exception:
+                raise XFail()
+            raise XPass()
+        return wrapper
 
-def footer(self, colitems):
-    super(TerminalSession, self).footer(colitems)
-    self.endtime = now()
-    self.out.line()
-    self.skippedreasons()
-    self.failures()
-    self.xpasses()
-    self.summaryline()
+    def skip(str):
+        raise Skipped(str)
+else:
+    # tested with py-lib 0.9.0
+    from py.__.test.outcome import Outcome, Passed, Failed, Skipped
+    from py.__.test.terminal.terminal import TerminalSession
+    from time import time as now
+    from py.test import skip
 
+    __all__ = ['XFAIL']
 
-def xpasses(self):
-    """report unexpectedly passed tests"""
-    texts = {}
-    for colitem, outcome in self.getitemoutcomepairs(XPass):
-        raisingtb = self.getlastvisible(outcome.excinfo.traceback)
-        fn = raisingtb.frame.code.path
-        lineno = raisingtb.lineno
-        #d = texts.setdefault(outcome.excinfo.exconly(), {})
-        d = texts.setdefault(outcome.msg, {})
-        d[(fn,lineno)] = outcome
+    class XFail(Outcome):
+        pass
 
-    if texts:
+    class XPass(Outcome):
+        pass
+
+    TerminalSession.typemap[XFail] = 'f'
+    TerminalSession.typemap[XPass] = 'X'
+
+    TerminalSession.namemap[XFail] = 'XFAIL'
+    TerminalSession.namemap[XPass] = '*** XPASS ***'
+
+
+    def footer(self, colitems):
+        super(TerminalSession, self).footer(colitems)
+        self.endtime = now()
         self.out.line()
-        self.out.sep('_', '*** XPASS ***')
-        for text, dict in texts.items():
-            #for (fn, lineno), outcome in dict.items():
-            #    self.out.line('Skipped in %s:%d' %(fn, lineno+1))
-            #self.out.line("reason: %s" % text)
-            self.out.line("%s" % text)
+        self.skippedreasons()
+        self.failures()
+        self.xpasses()
+        self.summaryline()
+
+
+    def xpasses(self):
+        """report unexpectedly passed tests"""
+        texts = {}
+        for colitem, outcome in self.getitemoutcomepairs(XPass):
+            raisingtb = self.getlastvisible(outcome.excinfo.traceback)
+            fn = raisingtb.frame.code.path
+            lineno = raisingtb.lineno
+            #d = texts.setdefault(outcome.excinfo.exconly(), {})
+            d = texts.setdefault(outcome.msg, {})
+            d[(fn,lineno)] = outcome
+
+        if texts:
             self.out.line()
+            self.out.sep('_', '*** XPASS ***')
+            for text, dict in texts.items():
+                #for (fn, lineno), outcome in dict.items():
+                #    self.out.line('Skipped in %s:%d' %(fn, lineno+1))
+                #self.out.line("reason: %s" % text)
+                self.out.line("%s" % text)
+                self.out.line()
 
-def summaryline(self):
-    outlist = []
-    sum = 0
-    for typ in Passed, XPass, XFail, Failed, Skipped:
-        l = self.getitemoutcomepairs(typ)
-        if l:
-            outlist.append('%d %s' % (len(l), typ.__name__.lower()))
-        sum += len(l)
-    elapsed = self.endtime-self.starttime
-    status = "%s" % ", ".join(outlist)
-    self.out.sep('=', 'tests finished: %s in %4.2f seconds' %
-                     (status, elapsed))
+    def summaryline(self):
+        outlist = []
+        sum = 0
+        for typ in Passed, XPass, XFail, Failed, Skipped:
+            l = self.getitemoutcomepairs(typ)
+            if l:
+                outlist.append('%d %s' % (len(l), typ.__name__.lower()))
+            sum += len(l)
+        elapsed = self.endtime-self.starttime
+        status = "%s" % ", ".join(outlist)
+        self.out.sep('=', 'tests finished: %s in %4.2f seconds' %
+                         (status, elapsed))
 
-    # SymPy specific
-    if self.getitemoutcomepairs(Failed):
-        self.out.line('DO *NOT* COMMIT!')
+        # SymPy specific
+        if self.getitemoutcomepairs(Failed):
+            self.out.line('DO *NOT* COMMIT!')
 
-TerminalSession.footer  = footer
-TerminalSession.xpasses = xpasses
-TerminalSession.summaryline = summaryline
+    TerminalSession.footer  = footer
+    TerminalSession.xpasses = xpasses
+    TerminalSession.summaryline = summaryline
 
-def XFAIL(func):
-    """XFAIL decorator"""
-    def func_wrapper():
-        try:
-            func()
-        except Outcome:
-            raise   # pass-through test outcome
-        except:
-            raise XFail('XFAIL: %s' % func.func_name)
-        else:
-            raise XPass('XPASS: %s' % func.func_name)
+    def XFAIL(func):
+        """XFAIL decorator"""
+        def func_wrapper():
+            try:
+                func()
+            except Outcome:
+                raise   # pass-through test outcome
+            except:
+                raise XFail('XFAIL: %s' % func.func_name)
+            else:
+                raise XPass('XPASS: %s' % func.func_name)
 
-    return func_wrapper
-
-
+        return func_wrapper

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sympy-commits" group.
To post to this group, send email to sympy-commits@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sympy-commits?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to