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.
22 import sys
, re
, xml
.dom
.minidom
26 from subunit
import iso8601
27 from testtools
.content
import Content
, ContentType
28 mime_utf8
= ContentType('text', 'plain', {'charset': 'utf8'})
33 pkginstall_configvars
= {
34 #@PKGINSTALL_CONFIGVARS_IN24LINES@ # configvars are substituted upon script installation
38 def find_child (node
, child_name
):
39 for child
in node
.childNodes
:
40 if child
.nodeName
== child_name
:
43 def list_children (node
, child_name
):
45 for child
in node
.childNodes
:
46 if child
.nodeName
== child_name
:
49 def find_node (node
, name
= None):
50 if not node
or node
.nodeName
== name
or not name
:
52 for child
in node
.childNodes
:
53 c
= find_node (child
, name
)
57 def node_as_text (node
, name
= None):
59 node
= find_node (node
, name
)
64 for child
in node
.childNodes
:
65 txt
+= node_as_text (child
)
67 def attribute_as_text (node
, aname
, node_name
= None):
68 node
= find_node (node
, node_name
)
71 attr
= node
.attributes
.get (aname
, '')
72 if hasattr (attr
, 'value'):
77 def html_indent_string (n
):
78 uncollapsible_space
= ' ' # HTML won't compress alternating sequences of ' ' and ' '
80 for i
in range (0, (n
+ 1) / 2):
81 string
+= uncollapsible_space
84 # TestBinary object, instantiated per test binary in the log file
86 def __init__ (self
, name
):
90 self
.success_cases
= 0
91 self
.skipped_cases
= 0
95 # base class to handle processing/traversion of XML nodes
99 def trampoline (self
, node
):
102 self
.handle_text (node
)
104 try: method
= getattr (self
, 'handle_' + re
.sub ('[^a-zA-Z0-9]', '_', name
))
105 except: method
= None
109 return self
.process_recursive (name
, node
)
110 def process_recursive (self
, node_name
, node
):
111 self
.process_children (node
)
112 def process_children (self
, node
):
114 for child
in node
.childNodes
:
115 self
.trampoline (child
)
118 # test report reader, this class collects some statistics and merges duplicate test binary runs
119 class ReportReader (TreeProcess
):
121 TreeProcess
.__init
__ (self
)
122 self
.binary_names
= []
124 self
.last_binary
= None
126 def binary_list (self
):
128 for name
in self
.binary_names
:
129 lst
+= [ self
.binaries
[name
] ]
133 def handle_info (self
, node
):
134 dn
= find_child (node
, 'package')
135 self
.info
['package'] = node_as_text (dn
)
136 dn
= find_child (node
, 'version')
137 self
.info
['version'] = node_as_text (dn
)
138 dn
= find_child (node
, 'revision')
140 self
.info
['revision'] = node_as_text (dn
)
141 def handle_testcase (self
, node
):
142 self
.last_binary
.testcases
+= [ node
]
143 result
= attribute_as_text (node
, 'result', 'status')
144 if result
== 'success':
145 self
.last_binary
.success_cases
+= 1
146 if bool (int (attribute_as_text (node
, 'skipped') + '0')):
147 self
.last_binary
.skipped_cases
+= 1
148 def handle_text (self
, node
):
150 def handle_testbinary (self
, node
):
151 path
= node
.attributes
.get ('path', None).value
152 if self
.binaries
.get (path
, -1) == -1:
153 self
.binaries
[path
] = TestBinary (path
)
154 self
.binary_names
+= [ path
]
155 self
.last_binary
= self
.binaries
[path
]
156 dn
= find_child (node
, 'duration')
157 dur
= node_as_text (dn
)
158 try: dur
= float (dur
)
161 self
.last_binary
.duration
+= dur
162 bin
= find_child (node
, 'binary')
164 self
.last_binary
.file = attribute_as_text (bin
, 'file')
165 rseed
= find_child (node
, 'random-seed')
167 self
.last_binary
.random_seed
= node_as_text (rseed
)
168 self
.process_children (node
)
171 class ReportWriter(object):
172 """Base class for reporting."""
174 def __init__(self
, binary_list
):
175 self
.binaries
= binary_list
177 def _error_text(self
, node
):
178 """Get a string representing the error children of node."""
179 rlist
= list_children(node
, 'error')
182 txt
+= node_as_text (enode
)
183 if txt
and txt
[-1] != '\n':
188 class HTMLReportWriter(ReportWriter
):
189 # Javascript/CSS snippet to toggle element visibility
191 <style type="text/css" media="screen">
193 .HiddenSection { display: none; }
195 <script language="javascript" type="text/javascript"><!--
196 function toggle_display (parentid, tagtype, idmatch, keymatch) {
197 ptag = document.getElementById (parentid);
198 tags = ptag.getElementsByTagName (tagtype);
199 for (var i = 0; i < tags.length; i++) {
201 var key = tag.getAttribute ("keywords");
202 if (tag.id.indexOf (idmatch) == 0 && key && key.match (keymatch)) {
203 if (tag.className.indexOf ("HiddenSection") >= 0)
204 tag.className = "VisibleSection";
206 tag.className = "HiddenSection";
210 message_array = Array();
211 function view_testlog (wname, file, random_seed, tcase, msgtitle, msgid) {
212 txt = message_array[msgid];
213 var w = window.open ("", // URI
215 "resizable,scrollbars,status,width=790,height=400");
216 var doc = w.document;
217 doc.write ("<h2>File: " + file + "</h2>\n");
218 doc.write ("<h3>Case: " + tcase + "</h3>\n");
219 doc.write ("<strong>Random Seed:</strong> <code>" + random_seed + "</code> <br /><br />\n");
220 doc.write ("<strong>" + msgtitle + "</strong><br />\n");
223 doc.write ("</pre>\n");
224 doc.write ("<a href=\'javascript
:window
.close()\'>Close Window
</a
>\n");
229 def __init__ (self, info, binary_list):
230 ReportWriter.__init__(self, binary_list)
234 self.total_tcounter = 0
235 self.total_fcounter = 0
236 self.total_duration = 0
237 self.indent_depth = 0
239 def oprint (self, message):
240 sys.stdout.write (message)
242 self.lastchar = message[-1]
243 def handle_info (self):
244 self.oprint ('<h3>Package: %(package)s, version: %(version)s</h3>\n' % self.info)
245 if self.info['revision']:
246 self.oprint ('<h5>Report generated from: %(revision)s</h5>\n' % self.info)
247 def handle_text (self, node):
248 self.oprint (node.nodeValue)
249 def handle_testcase (self, node, binary):
250 skipped = bool (int (attribute_as_text (node, 'skipped') + '0'))
252 return # skipped tests are uninteresting for HTML reports
253 path = attribute_as_text (node, 'path')
254 duration = node_as_text (node, 'duration')
255 result = attribute_as_text (node, 'result', 'status')
257 'success': 'bgcolor="lightgreen
"',
258 'failed': 'bgcolor="red
"',
260 if result != 'success':
261 duration = '-' # ignore bogus durations
262 self.oprint ('<tr id="b
%u_t
%u_" keywords="%s all
" class="HiddenSection
">\n' % (self.bcounter, self.tcounter, result))
263 self.oprint ('<td>%s %s</td> <td align="right
">%s</td> \n' % (html_indent_string (4), path, duration))
264 perflist = list_children (node, 'performance')
265 if result != 'success':
266 txt = self._error_text(node)
267 txt = re.sub (r'"', r'\\"', txt)
268 txt = re.sub (r'\n', r'\\n', txt)
269 txt = re.sub (r'&', r'&', txt)
270 txt = re.sub (r'<', r'<', txt)
271 self.oprint ('<script language="javascript
" type="text
/javascript
">message_array["b
%u_t
%u_"] = "%s";</script>\n' % (self.bcounter, self.tcounter, txt))
272 self.oprint ('<td align="center
"><a href="javascript
:view_testlog (\'%s\', \'%s\', \'%s\', \'%s\', \'Output
:\', \'b
%u_t
%u_\')">Details</a></td>\n' %
273 ('TestResultWindow', binary.file, binary.random_seed, path, self.bcounter, self.tcounter))
276 for perf in perflist:
277 pmin = bool (int (attribute_as_text (perf, 'minimize')))
278 pmax = bool (int (attribute_as_text (perf, 'maximize')))
279 pval = float (attribute_as_text (perf, 'value'))
280 txt = node_as_text (perf)
281 txt = re.sub (r'&', r'&', txt)
282 txt = re.sub (r'<', r'>', txt)
283 txt = '<strong>Performance(' + (pmin and '<em>minimized</em>' or '<em>maximized</em>') + '):</strong> ' + txt.strip() + '<br />\n'
284 txt = re.sub (r'"', r'\\"', txt)
285 txt = re.sub (r'\n', r'\\n', txt)
286 presults += [ (pval, txt) ]
288 ptxt = ''.join ([e[1] for e in presults])
289 self.oprint ('<script language="javascript
" type="text
/javascript
">message_array["b
%u_t
%u_"] = "%s";</script>\n' % (self.bcounter, self.tcounter, ptxt))
290 self.oprint ('<td align="center
"><a href="javascript
:view_testlog (\'%s\', \'%s\', \'%s\', \'%s\', \'Test Results
:\', \'b
%u_t
%u_\')">Details</a></td>\n' %
291 ('TestResultWindow', binary.file, binary.random_seed, path, self.bcounter, self.tcounter))
293 self.oprint ('<td align="center
">-</td>\n')
294 self.oprint ('<td align="right
" %s>%s</td>\n' % (rcolor, result))
295 self.oprint ('</tr>\n')
297 self.total_tcounter += 1
298 self.total_fcounter += result != 'success'
299 def handle_binary (self, binary):
302 self.total_duration += binary.duration
303 self.oprint ('<tr><td><strong>%s</strong></td><td align="right
">%f</td> <td align="center
">\n' % (binary.name, binary.duration))
304 erlink, oklink = ('', '')
305 real_cases = len (binary.testcases) - binary.skipped_cases
306 if binary.success_cases < real_cases:
307 erlink = 'href="javascript
:toggle_display (\'ResultTable
\', \'tr
\', \'b
%u_\', \'failed
\')"' % self.bcounter
308 if binary.success_cases:
309 oklink = 'href="javascript
:toggle_display (\'ResultTable
\', \'tr
\', \'b
%u_\', \'success
\')"' % self.bcounter
311 self.oprint ('<a %s>ER</a>\n' % erlink)
312 self.oprint ('<a %s>OK</a>\n' % oklink)
313 self.oprint ('</td>\n')
314 perc = binary.success_cases * 100.0 / real_cases
316 100 : 'bgcolor="lightgreen
"',
318 }.get (int (perc), 'bgcolor="yellow
"')
319 self.oprint ('<td align="right
" %s>%.2f%%</td>\n' % (pcolor, perc))
320 self.oprint ('</tr>\n')
322 self.oprint ('Empty\n')
323 self.oprint ('</td>\n')
324 self.oprint ('</tr>\n')
325 for tc in binary.testcases:
326 self.handle_testcase (tc, binary)
327 def handle_totals (self):
329 self.oprint ('<td><strong>Totals:</strong> %u Binaries, %u Tests, %u Failed, %u Succeeded</td>' %
330 (self.bcounter, self.total_tcounter, self.total_fcounter, self.total_tcounter - self.total_fcounter))
331 self.oprint ('<td align="right
">%f</td>\n' % self.total_duration)
332 self.oprint ('<td align="center
">-</td>\n')
333 if self.total_tcounter != 0:
334 perc = (self.total_tcounter - self.total_fcounter) * 100.0 / self.total_tcounter
338 100 : 'bgcolor="lightgreen
"',
340 }.get (int (perc), 'bgcolor="yellow
"')
341 self.oprint ('<td align="right
" %s>%.2f%%</td>\n' % (pcolor, perc))
342 self.oprint ('</tr>\n')
344 self.oprint ('<html><head>\n')
345 self.oprint ('<title>GTester Unit Test Report</title>\n')
346 self.oprint (self.cssjs)
347 self.oprint ('</head>\n')
348 self.oprint ('<body>\n')
349 self.oprint ('<h2>GTester Unit Test Report</h2>\n')
351 self.oprint ('<table id="ResultTable
" width="100%" border="1">\n<tr>\n')
352 self.oprint ('<th>Program / Testcase </th>\n')
353 self.oprint ('<th style="width
:8em
">Duration (sec)</th>\n')
354 self.oprint ('<th style="width
:5em
">View</th>\n')
355 self.oprint ('<th style="width
:5em
">Result</th>\n')
356 self.oprint ('</tr>\n')
357 for tb in self.binaries:
358 self.handle_binary (tb)
360 self.oprint ('</table>\n')
361 self.oprint ('</body>\n')
362 self.oprint ('</html>\n')
365 class SubunitWriter(ReportWriter):
366 """Reporter to output a subunit stream."""
369 reporter = subunit.TestProtocolClient(sys.stdout)
370 for binary in self.binaries:
371 for tc in binary.testcases:
372 test = GTestCase(tc, binary)
376 class GTestCase(object):
377 """A representation of a gtester test result as a pyunit TestCase."""
379 def __init__(self, case, binary):
380 """Create a GTestCase for case `case` from binary program `binary`."""
382 self._binary = binary
383 # the name of the case - e.g. /dbusmenu/glib/objects/menuitem/props_boolstr
384 self._path = attribute_as_text(self._case, 'path')
387 """What test is this? Returns the gtester path for the testcase."""
390 def _get_details(self):
391 """Calculate a details dict for the test - attachments etc."""
393 result = attribute_as_text(self._case, 'result', 'status')
394 details['filename'] = Content(mime_utf8, lambda:[self._binary.file])
395 details['random_seed'] = Content(mime_utf8,
396 lambda:[self._binary.random_seed])
397 if self._get_outcome() == 'addFailure':
398 # Extract the error details. Skips have no details because its not
399 # skip like unittest does, instead the runner just bypasses N test.
400 txt = self._error_text(self._case)
401 details['error'] = Content(mime_utf8, lambda:[txt])
402 if self._get_outcome() == 'addSuccess':
403 # Sucessful tests may have performance metrics.
404 perflist = list_children(self._case, 'performance')
407 for perf in perflist:
408 pmin = bool (int (attribute_as_text (perf, 'minimize')))
409 pmax = bool (int (attribute_as_text (perf, 'maximize')))
410 pval = float (attribute_as_text (perf, 'value'))
411 txt = node_as_text (perf)
412 txt = 'Performance(' + (pmin and 'minimized' or 'maximized'
413 ) + '): ' + txt.strip() + '\n'
414 presults += [(pval, txt)]
416 perf_details = [e[1] for e in presults]
417 details['performance'] = Content(mime_utf8, lambda:perf_details)
420 def _get_outcome(self):
421 if int(attribute_as_text(self._case, 'skipped') + '0'):
423 outcome = attribute_as_text(self._case, 'result', 'status')
424 if outcome == 'success':
429 def run(self, result):
430 time = datetime.datetime.utcnow().replace(tzinfo=iso8601.Utc())
432 result.startTest(self)
434 outcome = self._get_outcome()
435 details = self._get_details()
436 # Only provide a duration IFF outcome == 'addSuccess' - the main
437 # parser claims bogus results otherwise: in that case emit time as
439 if outcome == 'addSuccess':
440 duration = float(node_as_text(self._case, 'duration'))
441 duration = duration * 1000000
442 timedelta = datetime.timedelta(0, 0, duration)
443 time = time + timedelta
445 getattr(result, outcome)(self, details=details)
447 result.stopTest(self)
451 # main program handling
453 """Parse program options.
455 :return: An options object and the program arguments.
457 parser = optparse.OptionParser()
458 parser.version = pkginstall_configvars.get ('glib-version', '0.0-uninstalled')
459 parser.usage = "%prog
[OPTIONS
] <gtester
-log
.xml
>"
460 parser.description = "Generate HTML reports
from the XML log files generated by gtester
."
461 parser.epilog = "gtester
-report (GLib utils
) version
%s."% (parser.version,)
462 parser.add_option("-v
", "--version
", action="store_true
", dest="version
", default=False,
463 help="Show program version
.")
464 parser.add_option("-s
", "--subunit
", action="store_true
", dest="subunit
", default=False,
465 help="Output subunit
[See https
://launchpad
.net
/subunit
/"
466 " Needs python
-subunit
]")
467 options, files = parser.parse_args()
472 parser.error("Must supply a log
file to parse
.")
473 if options.subunit and subunit is None:
474 parser.error("python
-subunit
is not installed
.")
475 return options, files
479 options, files = parse_opts()
482 xd = xml.dom.minidom.parse (files[0])
485 if not options.subunit:
486 HTMLReportWriter(rr.get_info(), rr.binary_list()).printout()
488 SubunitWriter(rr.get_info(), rr.binary_list()).printout()
491 if __name__ == '__main__':