"""
A plugin for nosetests that will write out test results to results.html. The
code is adapted from the example html output plugin at
https://github.com/nose-devs/nose/blob/master/examples/html_plugin/htmlplug.py
and the pyunit Html test runner at
http://tungwaiyip.info/software/HTMLTestRunner.html
Original HTMLTestRunner License:
------------------------------------------------------------------------
Copyright (c) 2004-2007, Wai Yip Tung
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name Wai Yip Tung nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import datetime
import os
import traceback
from nose.plugins import Plugin
from xml.sax import saxutils
import version
__version__ = version.__version__
class TemplateData(object):
"""
Define a HTML template for report customerization and generation.
Overall structure of an HTML report
HTML
+------------------------+
| |
|
"""
# -------------------- The end of the Template class -------------------
class HtmlOutput(Plugin):
"""Output test results in html."""
name = 'html-output'
score = 2 # run late
def __init__(self):
super(HtmlOutput, self).__init__()
self.success_count = 0
self.failure_count = 0
self.error_count = 0
self.result = []
def options(self, parser, env=os.environ):
super(HtmlOutput, self).options(parser, env)
parser.add_option("--html-out-file", action="store",
default=env.get('NOSE_HTML_OUT_FILE', 'results.html'),
dest="html_file",
metavar="FILE",
help="Produce results in the specified HTML file.")
def configure(self, options, conf):
super(HtmlOutput, self).configure(options, conf)
self.html_file = None
if options.html_file:
self.html_file = options.html_file
def begin(self):
self.startTime = datetime.datetime.now()
def addSuccess(self, test):
self.success_count += 1
output = test.shortDescription()
if output is None:
output = test.id()
self.result.append((0, test, output, ''))
def addError(self, test, err):
self.error_count += 1
_exc_str = self.formatErr(err)
output = test.shortDescription()
if output is None:
output = test.id()
self.result.append((2, test, output, _exc_str))
def addFailure(self, test, err):
self.failure_count += 1
_exc_str = self.formatErr(err)
output = test.shortDescription()
if output is None:
output = test.id()
self.result.append((1, test, output, _exc_str))
def formatErr(self, err):
exctype, value, tb = err
return ''.join(traceback.format_exception(exctype, value, tb))
def setOutputStream(self, stream):
# grab for Monitoring
self.stream = stream
def report(self, stream):
self.stopTime = datetime.datetime.now()
report_attrs = self._getReportAttributes()
generator = 'html-output-plugin %s' % __version__
heading = self._generate_heading(report_attrs)
report = self._generate_report()
ending = self._generate_ending()
output = TemplateData.HTML_TMPL % dict(
title = saxutils.escape(TemplateData.DEFAULT_TITLE),
generator = generator,
stylesheet = TemplateData.STYLESHEET_TMPL,
heading = heading,
report = report,
ending = ending,
)
if self.html_file:
html_file = open(self.html_file, 'w')
html_file.write(output.encode('utf8'))
else:
stream.write(output.encode('utf8'))
def _getReportAttributes(self):
"""Return report attributes as a list of (name, value)."""
startTime = str(self.startTime)[:19]
duration = str(self.stopTime - self.startTime)
status = []
if self.success_count:
status.append('Pass %s' % self.success_count)
if self.failure_count:
status.append('Failure %s' % self.failure_count)
if self.error_count:
status.append('Error %s' % self.error_count )
if status:
status = ' '.join(status)
else:
status = 'none'
return [
('Start Time', startTime),
('Duration', duration),
('Status', status),
]
def _generate_heading(self, report_attrs):
a_lines = []
for name, value in report_attrs:
line = TemplateData.HEADING_ATTRIBUTE_TMPL % dict(
name = saxutils.escape(name),
value = saxutils.escape(value),
)
a_lines.append(line)
heading = TemplateData.HEADING_TMPL % dict(
title = saxutils.escape(TemplateData.DEFAULT_TITLE),
parameters = ''.join(a_lines),
description = saxutils.escape(TemplateData.DEFAULT_DESCRIPTION),
)
return heading
def _generate_report(self):
rows = []
sortedResult = self._sortResult(self.result)
for cid, (cls, cls_results) in enumerate(sortedResult):
# subtotal for a class
np = nf = ne = 0
for n,t,o,e in cls_results:
if n == 0: np += 1
elif n == 1: nf += 1
else: ne += 1
# format class description
if cls.__module__ == "__main__":
name = cls.__name__
else:
name = "%s.%s" % (cls.__module__, cls.__name__)
doc = cls.__doc__ and cls.__doc__.split("\n")[0] or ""
desc = doc and '%s: %s' % (name, doc) or name
row = TemplateData.REPORT_CLASS_TMPL % dict(
style = ne > 0 and 'errorClass' or nf > 0 and 'failClass' or 'passClass',
desc = desc,
count = np+nf+ne,
Pass = np,
fail = nf,
error = ne,
cid = 'c%s' % (cid+1),
)
rows.append(row)
for tid, (n,t,o,e) in enumerate(cls_results):
self._generate_report_test(rows, cid, tid, n, t, o, e)
report = TemplateData.REPORT_TMPL % dict(
test_list = ''.join(rows),
count = str(self.success_count + self.failure_count + self.error_count),
Pass = str(self.success_count),
fail = str(self.failure_count),
error = str(self.error_count),
)
return report
def _sortResult(self, result_list):
# unittest does not seems to run in any particular order.
# Here at least we want to group them together by class.
rmap = {}
classes = []
for n,t,o,e in result_list:
cls = t.test.__class__
if not rmap.has_key(cls):
rmap[cls] = []
classes.append(cls)
rmap[cls].append((n,t,o,e))
r = [(cls, rmap[cls]) for cls in classes]
return r
def _generate_report_test(self, rows, cid, tid, n, t, o, e):
# e.g. 'pt1.1', 'ft1.1', etc
has_output = bool(o or e)
tid = (n == 0 and 'p' or 'f') + 't%s.%s' % (cid+1,tid+1)
name = t.id().split('.')[-1]
doc = t.shortDescription() or ""
desc = doc and ('%s: %s' % (name, doc)) or name
tmpl = has_output and TemplateData.REPORT_TEST_WITH_OUTPUT_TMPL or TemplateData.REPORT_TEST_NO_OUTPUT_TMPL
# Comments below from the original source project.
# TODO: clean this up within the context of a nose plugin.
# o and e should be byte string because they are collected from stdout and stderr?
if isinstance(o,str):
# TODO: some problem with 'string_escape': it escape \n and mess up formating
# uo = unicode(o.encode('string_escape'))
uo = o.decode('latin-1')
else:
uo = o
if isinstance(e,str):
# TODO: some problem with 'string_escape': it escape \n and mess up formating
# ue = unicode(e.encode('string_escape'))
ue = e.decode('latin-1')
else:
ue = e
script = TemplateData.REPORT_TEST_OUTPUT_TMPL % dict(
id = tid,
output = saxutils.escape(uo+ue),
)
row = tmpl % dict(
tid = tid,
Class = (n == 0 and 'hiddenRow' or 'none'),
style = n == 2 and 'errorCase' or (n == 1 and 'failCase' or 'none'),
desc = desc,
script = script,
status = TemplateData.STATUS[n],
)
rows.append(row)
if not has_output:
return
def _generate_ending(self):
return TemplateData.ENDING_TMPL