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
113 def binary_list (self):
115 for name in self.binary_names:
116 lst += [ self.binaries[name] ]
118 def handle_testcase (self, node):
119 self.last_binary.testcases += [ node ]
120 result = attribute_as_text (node, 'result', 'status')
121 if result == 'success':
122 self.last_binary.success_cases += 1
123 if bool (int (attribute_as_text (node, 'skipped') + '0')):
124 self.last_binary.skipped_cases += 1
125 def handle_text (self, node):
127 def handle_testbinary (self, node):
128 path = node.attributes.get ('path', None).value
129 if self.binaries.get (path, -1) == -1:
130 self.binaries[path] = TestBinary (path)
131 self.binary_names += [ path ]
132 self.last_binary = self.binaries[path]
133 dn = find_child (node, 'duration')
134 dur = node_as_text (dn)
135 try: dur = float (dur)
138 self.last_binary.duration += dur
139 bin = find_child (node, 'binary')
141 self.last_binary.file = attribute_as_text (bin, 'file')
142 rseed = find_child (node, 'random-seed')
144 self.last_binary.random_seed = node_as_text (rseed)
145 self.process_children (node)
147 # HTML report generation class
148 class ReportWriter (TreeProcess):
149 # Javascript/CSS snippet to toggle element visibility
151 <style type="text/css" media="screen">
153 .HiddenSection { display: none; }
155 <script language="javascript" type="text/javascript"><!--
156 function toggle_display (parentid, tagtype, idmatch, keymatch) {
157 ptag = document.getElementById (parentid);
158 tags = ptag.getElementsByTagName (tagtype);
159 for (var i = 0; i < tags.length; i++) {
161 var key = tag.getAttribute ("keywords");
162 if (tag.id.indexOf (idmatch) == 0 && key && key.match (keymatch)) {
163 if (tag.className.indexOf ("HiddenSection") >= 0)
164 tag.className = "VisibleSection";
166 tag.className = "HiddenSection";
170 message_array = Array();
171 function view_testlog (wname, file, random_seed, tcase, msgtitle, msgid) {
172 txt = message_array[msgid];
173 var w = window.open ("", // URI
175 "resizable,scrollbars,status,width=790,height=400");
176 var doc = w.document;
177 doc.write ("<h2>File: " + file + "</h2>\n");
178 doc.write ("<h3>Case: " + tcase + "</h3>\n");
179 doc.write ("<strong>Random Seed:</strong> <code>" + random_seed + "</code> <br /><br />\n");
180 doc.write ("<strong>" + msgtitle + "</strong><br />\n");
183 doc.write ("</pre>\n");
184 doc.write ("<a href=\'javascript:window.close()\'>Close Window</a>\n");
189 def __init__ (self, binary_list):
190 TreeProcess.__init__ (self)
191 self.binaries = binary_list
194 self.total_tcounter = 0
195 self.total_fcounter = 0
196 self.total_duration = 0
197 self.indent_depth = 0
199 def oprint (self, message):
200 sys.stdout.write (message)
202 self.lastchar = message[-1]
203 def handle_text (self, node):
204 self.oprint (node.nodeValue)
205 def handle_testcase (self, node, binary):
206 skipped = bool (int (attribute_as_text (node, 'skipped') + '0'))
208 return # skipped tests are uninteresting for HTML reports
209 path = attribute_as_text (node, 'path')
210 duration = node_as_text (node, 'duration')
211 result = attribute_as_text (node, 'result', 'status')
213 'success': 'bgcolor="lightgreen"',
214 'failed': 'bgcolor="red"',
216 if result != 'success':
217 duration = '-' # ignore bogus durations
218 self.oprint ('<tr id="b%u_t%u_" keywords="%s all" class="HiddenSection">\n' % (self.bcounter, self.tcounter, result))
219 self.oprint ('<td>%s %s</td> <td align="right">%s</td> \n' % (html_indent_string (4), path, duration))
220 perflist = list_children (node, 'performance')
221 if result != 'success':
222 rlist = list_children (node, 'error')
225 txt += node_as_text (enode)
226 if txt and txt[-1] != '\n':
228 txt = re.sub (r'"', r'\\"', txt)
229 txt = re.sub (r'\n', r'\\n', txt)
230 txt = re.sub (r'&', r'&', txt)
231 txt = re.sub (r'<', r'<', txt)
232 self.oprint ('<script language="javascript" type="text/javascript">message_array["b%u_t%u_"] = "%s";</script>\n' % (self.bcounter, self.tcounter, txt))
233 self.oprint ('<td align="center"><a href="javascript:view_testlog (\'%s\', \'%s\', \'%s\', \'%s\', \'Output:\', \'b%u_t%u_\')">Details</a></td>\n' %
234 ('TestResultWindow', binary.file, binary.random_seed, path, self.bcounter, self.tcounter))
237 for perf in perflist:
238 pmin = bool (int (attribute_as_text (perf, 'minimize')))
239 pmax = bool (int (attribute_as_text (perf, 'maximize')))
240 pval = float (attribute_as_text (perf, 'value'))
241 txt = node_as_text (perf)
242 txt = re.sub (r'&', r'&', txt)
243 txt = re.sub (r'<', r'>', txt)
244 txt = '<strong>Performance(' + (pmin and '<em>minimized</em>' or '<em>maximized</em>') + '):</strong> ' + txt.strip() + '<br />\n'
245 txt = re.sub (r'"', r'\\"', txt)
246 txt = re.sub (r'\n', r'\\n', txt)
247 presults += [ (pval, txt) ]
249 ptxt = ''.join ([e[1] for e in presults])
250 self.oprint ('<script language="javascript" type="text/javascript">message_array["b%u_t%u_"] = "%s";</script>\n' % (self.bcounter, self.tcounter, ptxt))
251 self.oprint ('<td align="center"><a href="javascript:view_testlog (\'%s\', \'%s\', \'%s\', \'%s\', \'Test Results:\', \'b%u_t%u_\')">Details</a></td>\n' %
252 ('TestResultWindow', binary.file, binary.random_seed, path, self.bcounter, self.tcounter))
254 self.oprint ('<td align="center">-</td>\n')
255 self.oprint ('<td align="right" %s>%s</td>\n' % (rcolor, result))
256 self.oprint ('</tr>\n')
258 self.total_tcounter += 1
259 self.total_fcounter += result != 'success'
260 def handle_binary (self, binary):
263 self.total_duration += binary.duration
264 self.oprint ('<tr><td><strong>%s</strong></td><td align="right">%f</td> <td align="center">\n' % (binary.name, binary.duration))
265 erlink, oklink = ('', '')
266 real_cases = len (binary.testcases) - binary.skipped_cases
267 if binary.success_cases < real_cases:
268 erlink = 'href="javascript:toggle_display (\'ResultTable\', \'tr\', \'b%u_\', \'failed\')"' % self.bcounter
269 if binary.success_cases:
270 oklink = 'href="javascript:toggle_display (\'ResultTable\', \'tr\', \'b%u_\', \'success\')"' % self.bcounter
271 self.oprint ('<a %s>ER</a>\n' % erlink)
272 self.oprint ('<a %s>OK</a>\n' % oklink)
273 self.oprint ('</td>\n')
274 perc = binary.success_cases * 100.0 / real_cases
276 100 : 'bgcolor="lightgreen"',
278 }.get (int (perc), 'bgcolor="yellow"')
279 self.oprint ('<td align="right" %s>%.2f%%</td>\n' % (pcolor, perc))
280 self.oprint ('</tr>\n')
281 for tc in binary.testcases:
282 self.handle_testcase (tc, binary)
283 def handle_totals (self):
285 self.oprint ('<td><strong>Totals:</strong> %u Binaries, %u Tests, %u Failed, %u Succeeded</td>' %
286 (self.bcounter, self.total_tcounter, self.total_fcounter, self.total_tcounter - self.total_fcounter))
287 self.oprint ('<td align="right">%f</td>\n' % self.total_duration)
288 self.oprint ('<td align="center">-</td>\n')
289 perc = (self.total_tcounter - self.total_fcounter) * 100.0 / self.total_tcounter
291 100 : 'bgcolor="lightgreen"',
293 }.get (int (perc), 'bgcolor="yellow"')
294 self.oprint ('<td align="right" %s>%.2f%%</td>\n' % (pcolor, perc))
295 self.oprint ('</tr>\n')
297 self.oprint ('<html><head>\n')
298 self.oprint ('<title>GTester Unit Test Report</title>\n')
299 self.oprint (self.cssjs)
300 self.oprint ('</head>\n')
301 self.oprint ('<body>\n')
302 self.oprint ('<h2>GTester Unit Test Report</h2>\n')
303 self.oprint ('<table id="ResultTable" width="100%" border="1">\n<tr>\n')
304 self.oprint ('<th>Program / Testcase </th>\n')
305 self.oprint ('<th style="width:8em">Duration (sec)</th>\n')
306 self.oprint ('<th style="width:5em">View</th>\n')
307 self.oprint ('<th style="width:5em">Result</th>\n')
308 self.oprint ('</tr>\n')
309 for tb in self.binaries:
310 self.handle_binary (tb)
312 self.oprint ('</table>\n')
313 self.oprint ('</body>\n')
314 self.oprint ('</html>\n')
316 # main program handling
317 def parse_files_and_args ():
318 from sys import argv, stdin
320 arg_iter = sys.argv[1:].__iter__()
321 rest = len (sys.argv) - 1
324 if arg == '--help' or arg == '-h':
327 elif arg == '--version' or arg == '-v':
331 files = files + [ arg ]
334 def print_help (with_help = True):
336 print "gtester-report (GLib utils) version", pkginstall_configvars.get ('glib-version', '0.0-uninstalled')
339 print "Usage: %s [OPTIONS] <gtester-log.xml>" % os.path.basename (sys.argv[0])
340 print "Generate HTML reports from the XML log files generated by gtester."
342 print " --help, -h print this help message"
343 print " --version, -v print version info"
346 from sys import argv, stdin
347 files = parse_files_and_args()
351 xd = xml.dom.minidom.parse (files[0])
354 rw = ReportWriter (rr.binary_list())
357 if __name__ == '__main__':