2 # GLib Testing Framework Utility -*- Mode: python; -*-
3 # Copyright (C) 2007 Imendio AB
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2 of the License, or (at your option) any later version.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the
18 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 # Boston, MA 02111-1307, USA.
20 import sys, re, xml.dom.minidom
21 pkginstall_configvars = {
22 #@PKGINSTALL_CONFIGVARS_IN24LINES@ # configvars are substituted upon script installation
26 def find_child (node, child_name):
27 for child in node.childNodes:
28 if child.nodeName == child_name:
31 def list_children (node, child_name):
33 for child in node.childNodes:
34 if child.nodeName == child_name:
37 def find_node (node, name = None):
38 if not node or node.nodeName == name or not name:
40 for child in node.childNodes:
41 c = find_node (child, name)
45 def node_as_text (node, name = None):
47 node = find_node (node, name)
52 for child in node.childNodes:
53 txt += node_as_text (child)
55 def attribute_as_text (node, aname, node_name = None):
56 node = find_node (node, node_name)
59 attr = node.attributes.get (aname, '')
60 if hasattr (attr, 'value'):
65 def html_indent_string (n):
66 uncollapsible_space = ' ' # HTML won't compress alternating sequences of ' ' and ' '
68 for i in range (0, (n + 1) / 2):
69 string += uncollapsible_space
72 # TestBinary object, instantiated per test binary in the log file
74 def __init__ (self, name):
78 self.success_cases = 0
79 self.skipped_cases = 0
83 # base class to handle processing/traversion of XML nodes
87 def trampoline (self, node):
90 self.handle_text (node)
92 try: method = getattr (self, 'handle_' + re.sub ('[^a-zA-Z0-9]', '_', name))
97 return self.process_recursive (name, node)
98 def process_recursive (self, node_name, node):
99 self.process_children (node)
100 def process_children (self, node):
102 for child in node.childNodes:
103 self.trampoline (child)
106 # test report reader, this class collects some statistics and merges duplicate test binary runs
107 class ReportReader (TreeProcess):
109 TreeProcess.__init__ (self)
110 self.binary_names = []
112 self.last_binary = None
114 def binary_list (self):
116 for name in self.binary_names:
117 lst += [ self.binaries[name] ]
121 def handle_info (self, node):
122 dn = find_child (node, 'package')
123 self.info['package'] = node_as_text (dn)
124 dn = find_child (node, 'version')
125 self.info['version'] = node_as_text (dn)
126 def handle_testcase (self, node):
127 self.last_binary.testcases += [ node ]
128 result = attribute_as_text (node, 'result', 'status')
129 if result == 'success':
130 self.last_binary.success_cases += 1
131 if bool (int (attribute_as_text (node, 'skipped') + '0')):
132 self.last_binary.skipped_cases += 1
133 def handle_text (self, node):
135 def handle_testbinary (self, node):
136 path = node.attributes.get ('path', None).value
137 if self.binaries.get (path, -1) == -1:
138 self.binaries[path] = TestBinary (path)
139 self.binary_names += [ path ]
140 self.last_binary = self.binaries[path]
141 dn = find_child (node, 'duration')
142 dur = node_as_text (dn)
143 try: dur = float (dur)
146 self.last_binary.duration += dur
147 bin = find_child (node, 'binary')
149 self.last_binary.file = attribute_as_text (bin, 'file')
150 rseed = find_child (node, 'random-seed')
152 self.last_binary.random_seed = node_as_text (rseed)
153 self.process_children (node)
155 # HTML report generation class
156 class ReportWriter (TreeProcess):
157 # Javascript/CSS snippet to toggle element visibility
159 <style type="text/css" media="screen">
161 .HiddenSection { display: none; }
163 <script language="javascript" type="text/javascript"><!--
164 function toggle_display (parentid, tagtype, idmatch, keymatch) {
165 ptag = document.getElementById (parentid);
166 tags = ptag.getElementsByTagName (tagtype);
167 for (var i = 0; i < tags.length; i++) {
169 var key = tag.getAttribute ("keywords");
170 if (tag.id.indexOf (idmatch) == 0 && key && key.match (keymatch)) {
171 if (tag.className.indexOf ("HiddenSection") >= 0)
172 tag.className = "VisibleSection";
174 tag.className = "HiddenSection";
178 message_array = Array();
179 function view_testlog (wname, file, random_seed, tcase, msgtitle, msgid) {
180 txt = message_array[msgid];
181 var w = window.open ("", // URI
183 "resizable,scrollbars,status,width=790,height=400");
184 var doc = w.document;
185 doc.write ("<h2>File: " + file + "</h2>\n");
186 doc.write ("<h3>Case: " + tcase + "</h3>\n");
187 doc.write ("<strong>Random Seed:</strong> <code>" + random_seed + "</code> <br /><br />\n");
188 doc.write ("<strong>" + msgtitle + "</strong><br />\n");
191 doc.write ("</pre>\n");
192 doc.write ("<a href=\'javascript:window.close()\'>Close Window</a>\n");
197 def __init__ (self, info, binary_list):
198 TreeProcess.__init__ (self)
200 self.binaries = binary_list
203 self.total_tcounter = 0
204 self.total_fcounter = 0
205 self.total_duration = 0
206 self.indent_depth = 0
208 def oprint (self, message):
209 sys.stdout.write (message)
211 self.lastchar = message[-1]
212 def handle_info (self):
213 self.oprint ('<h3>Package: %(package)s, version: %(version)s</h3>\n' % self.info)
214 def handle_text (self, node):
215 self.oprint (node.nodeValue)
216 def handle_testcase (self, node, binary):
217 skipped = bool (int (attribute_as_text (node, 'skipped') + '0'))
219 return # skipped tests are uninteresting for HTML reports
220 path = attribute_as_text (node, 'path')
221 duration = node_as_text (node, 'duration')
222 result = attribute_as_text (node, 'result', 'status')
224 'success': 'bgcolor="lightgreen"',
225 'failed': 'bgcolor="red"',
227 if result != 'success':
228 duration = '-' # ignore bogus durations
229 self.oprint ('<tr id="b%u_t%u_" keywords="%s all" class="HiddenSection">\n' % (self.bcounter, self.tcounter, result))
230 self.oprint ('<td>%s %s</td> <td align="right">%s</td> \n' % (html_indent_string (4), path, duration))
231 perflist = list_children (node, 'performance')
232 if result != 'success':
233 rlist = list_children (node, 'error')
236 txt += node_as_text (enode)
237 if txt and txt[-1] != '\n':
239 txt = re.sub (r'"', r'\\"', txt)
240 txt = re.sub (r'\n', r'\\n', txt)
241 txt = re.sub (r'&', r'&', txt)
242 txt = re.sub (r'<', r'<', txt)
243 self.oprint ('<script language="javascript" type="text/javascript">message_array["b%u_t%u_"] = "%s";</script>\n' % (self.bcounter, self.tcounter, txt))
244 self.oprint ('<td align="center"><a href="javascript:view_testlog (\'%s\', \'%s\', \'%s\', \'%s\', \'Output:\', \'b%u_t%u_\')">Details</a></td>\n' %
245 ('TestResultWindow', binary.file, binary.random_seed, path, self.bcounter, self.tcounter))
248 for perf in perflist:
249 pmin = bool (int (attribute_as_text (perf, 'minimize')))
250 pmax = bool (int (attribute_as_text (perf, 'maximize')))
251 pval = float (attribute_as_text (perf, 'value'))
252 txt = node_as_text (perf)
253 txt = re.sub (r'&', r'&', txt)
254 txt = re.sub (r'<', r'>', txt)
255 txt = '<strong>Performance(' + (pmin and '<em>minimized</em>' or '<em>maximized</em>') + '):</strong> ' + txt.strip() + '<br />\n'
256 txt = re.sub (r'"', r'\\"', txt)
257 txt = re.sub (r'\n', r'\\n', txt)
258 presults += [ (pval, txt) ]
260 ptxt = ''.join ([e[1] for e in presults])
261 self.oprint ('<script language="javascript" type="text/javascript">message_array["b%u_t%u_"] = "%s";</script>\n' % (self.bcounter, self.tcounter, ptxt))
262 self.oprint ('<td align="center"><a href="javascript:view_testlog (\'%s\', \'%s\', \'%s\', \'%s\', \'Test Results:\', \'b%u_t%u_\')">Details</a></td>\n' %
263 ('TestResultWindow', binary.file, binary.random_seed, path, self.bcounter, self.tcounter))
265 self.oprint ('<td align="center">-</td>\n')
266 self.oprint ('<td align="right" %s>%s</td>\n' % (rcolor, result))
267 self.oprint ('</tr>\n')
269 self.total_tcounter += 1
270 self.total_fcounter += result != 'success'
271 def handle_binary (self, binary):
274 self.total_duration += binary.duration
275 self.oprint ('<tr><td><strong>%s</strong></td><td align="right">%f</td> <td align="center">\n' % (binary.name, binary.duration))
276 erlink, oklink = ('', '')
277 real_cases = len (binary.testcases) - binary.skipped_cases
278 if binary.success_cases < real_cases:
279 erlink = 'href="javascript:toggle_display (\'ResultTable\', \'tr\', \'b%u_\', \'failed\')"' % self.bcounter
280 if binary.success_cases:
281 oklink = 'href="javascript:toggle_display (\'ResultTable\', \'tr\', \'b%u_\', \'success\')"' % self.bcounter
283 self.oprint ('<a %s>ER</a>\n' % erlink)
284 self.oprint ('<a %s>OK</a>\n' % oklink)
285 self.oprint ('</td>\n')
286 perc = binary.success_cases * 100.0 / real_cases
288 100 : 'bgcolor="lightgreen"',
290 }.get (int (perc), 'bgcolor="yellow"')
291 self.oprint ('<td align="right" %s>%.2f%%</td>\n' % (pcolor, perc))
292 self.oprint ('</tr>\n')
294 self.oprint ('Empty\n')
295 self.oprint ('</td>\n')
296 self.oprint ('</tr>\n')
297 for tc in binary.testcases:
298 self.handle_testcase (tc, binary)
299 def handle_totals (self):
301 self.oprint ('<td><strong>Totals:</strong> %u Binaries, %u Tests, %u Failed, %u Succeeded</td>' %
302 (self.bcounter, self.total_tcounter, self.total_fcounter, self.total_tcounter - self.total_fcounter))
303 self.oprint ('<td align="right">%f</td>\n' % self.total_duration)
304 self.oprint ('<td align="center">-</td>\n')
305 perc = (self.total_tcounter - self.total_fcounter) * 100.0 / self.total_tcounter
307 100 : 'bgcolor="lightgreen"',
309 }.get (int (perc), 'bgcolor="yellow"')
310 self.oprint ('<td align="right" %s>%.2f%%</td>\n' % (pcolor, perc))
311 self.oprint ('</tr>\n')
313 self.oprint ('<html><head>\n')
314 self.oprint ('<title>GTester Unit Test Report</title>\n')
315 self.oprint (self.cssjs)
316 self.oprint ('</head>\n')
317 self.oprint ('<body>\n')
318 self.oprint ('<h2>GTester Unit Test Report</h2>\n')
320 self.oprint ('<table id="ResultTable" width="100%" border="1">\n<tr>\n')
321 self.oprint ('<th>Program / Testcase </th>\n')
322 self.oprint ('<th style="width:8em">Duration (sec)</th>\n')
323 self.oprint ('<th style="width:5em">View</th>\n')
324 self.oprint ('<th style="width:5em">Result</th>\n')
325 self.oprint ('</tr>\n')
326 for tb in self.binaries:
327 self.handle_binary (tb)
329 self.oprint ('</table>\n')
330 self.oprint ('</body>\n')
331 self.oprint ('</html>\n')
333 # main program handling
334 def parse_files_and_args ():
335 from sys import argv, stdin
337 arg_iter = sys.argv[1:].__iter__()
338 rest = len (sys.argv) - 1
341 if arg == '--help' or arg == '-h':
344 elif arg == '--version' or arg == '-v':
348 files = files + [ arg ]
351 def print_help (with_help = True):
353 print "gtester-report (GLib utils) version", pkginstall_configvars.get ('glib-version', '0.0-uninstalled')
356 print "Usage: %s [OPTIONS] <gtester-log.xml>" % os.path.basename (sys.argv[0])
357 print "Generate HTML reports from the XML log files generated by gtester."
359 print " --help, -h print this help message"
360 print " --version, -v print version info"
363 from sys import argv, stdin
364 files = parse_files_and_args()
368 xd = xml.dom.minidom.parse (files[0])
371 rw = ReportWriter (rr.get_info(), rr.binary_list())
374 if __name__ == '__main__':