Debian Codegraph @

tools/pytest.py

raw symbols SCIP
1 """py.test hacks to support XFAIL/XPASS"""
2
3
4
5
6
7 # XXX this should be integrated into py.test
8 # XXX but we can't force everyone to install py-lib trunk
9
10 import sys
11 try:
12 # functools is not available in Python 2.4
13 import functools
14 except ImportError:
15 has_functools = False
16 else:
17 has_functools = True
18
19 try:
20 # tested with py-lib 0.9.0
21 from py.__.test.outcome import Outcome, Passed, Failed, Skipped
22 from py.__.test.terminal.terminal import TerminalSession
23 from py.test import skip
24 USE_PYTEST = True
25 except ImportError:
26 USE_PYTEST = False
27
28 def raises(ExpectedException, code):
29 assert isinstance(code, str)
30 frame = sys._getframe(1)
31 loc = frame.f_locals.copy()
32 try:
33 exec(code, frame.f_globals, loc)
34 except ExpectedException:
35 return
36 raise AssertionError("DID NOT RAISE")
37
38 if not USE_PYTEST:
39 class XFail(Exception):
40 pass
41
42 class XPass(Exception):
43 pass
44
45 class Skipped(Exception):
46 pass
47
48 def XFAIL(func):
49 def wrapper():
50 try:
51 func()
52 except Exception:
53 raise XFail()
54 raise XPass()
55 if has_functools:
56 wrapper = functools.update_wrapper(wrapper, func)
57 return wrapper
58
59 def skip(str):
60 raise Skipped(str)
61 else:
62 from time import time as now
63
64 __all__ = ['XFAIL']
65
66 class XFail(Outcome):
67 pass
68
69 class XPass(Outcome):
70 pass
71
72 TerminalSession.typemap[XFail] = 'f'
73 TerminalSession.typemap[XPass] = 'X'
74
75 TerminalSession.namemap[XFail] = 'XFAIL'
76 TerminalSession.namemap[XPass] = '*** XPASS ***'
77
78
79 def footer(self, colitems):
80 super(TerminalSession, self).footer(colitems)
81 self.endtime = now()
82 self.out.line()
83 self.skippedreasons()
84 self.failures()
85 self.xpasses()
86 self.summaryline()
87
88
89 def xpasses(self):
90 """report unexpectedly passed tests"""
91 texts = {}
92 for colitem, outcome in self.getitemoutcomepairs(XPass):
93 raisingtb = self.getlastvisible(outcome.excinfo.traceback)
94 fn = raisingtb.frame.code.path
95 lineno = raisingtb.lineno
96 #d = texts.setdefault(outcome.excinfo.exconly(), {})
97 d = texts.setdefault(outcome.msg, {})
98 d[(fn,lineno)] = outcome
99
100 if texts:
101 self.out.line()
102 self.out.sep('_', '*** XPASS ***')
103 for text, dict in texts.items():
104 #for (fn, lineno), outcome in dict.items():
105 # self.out.line('Skipped in %s:%d' %(fn, lineno+1))
106 #self.out.line("reason: %s" % text)
107 self.out.line("%s" % text)
108 self.out.line()
109
110 def summaryline(self):
111 outlist = []
112 sum = 0
113 for typ in Passed, XPass, XFail, Failed, Skipped:
114 l = self.getitemoutcomepairs(typ)
115 if l:
116 outlist.append('%d %s' % (len(l), typ.__name__.lower()))
117 sum += len(l)
118 elapsed = self.endtime-self.starttime
119 status = "%s" % ", ".join(outlist)
120 self.out.sep('=', 'tests finished: %s in %4.2f seconds' %
121 (status, elapsed))
122
123 # SymPy specific
124 if self.getitemoutcomepairs(Failed):
125 self.out.line('DO *NOT* COMMIT!')
126
127 TerminalSession.footer = footer
128 TerminalSession.xpasses = xpasses
129 TerminalSession.summaryline = summaryline
130
131 def XFAIL(func):
132 """XFAIL decorator"""
133 def func_wrapper():
134 try:
135 func()
136 except Outcome:
137 raise # pass-through test outcome
138 except:
139 raise XFail('XFAIL: %s' % func.__name__)
140 else:
141 raise XPass('XPASS: %s' % func.__name__)
142
143 if has_functools:
144 func_wrapper = functools.update_wrapper(func_wrapper, func)
145 return func_wrapper