"""This is a very basic example of a plugin that controls all test
output. In this case, it formats the output as ugly unstyled html.
Upgrading this plugin into one that uses a template and css to produce
nice-looking, easily-modifiable html output is left as an exercise for
the reader who would like to see his or her name in the nose AUTHORS file.
"""
import traceback
from nose.plugins import Plugin
class HtmlOutput(Plugin):
"""Output test results as ugly, unstyled html.
"""
name = 'html-output'
def __init__(self):
super(HtmlOutput, self).__init__()
self.html = [ '
',
'Test output',
'' ]
def addSuccess(self, test, capt):
self.html.append('ok')
def addSkip(self, test):
self.html.append('SKIPPED')
def addDeprecated(self, test):
self.html.append('DEPRECATED')
def addError(self, test, err, capt):
err = self.formatErr(err)
self.html.append('ERROR')
self.html.append('%s
' % err)
if capt:
self.html.append('%s
' % capt)
def addFailure(self, test, err, capt, tb_info):
err = self.formatErr(err)
self.html.append('FAIL')
self.html.append('%s
' % err)
if tb_info:
self.html.append('%s
' % tb_info)
if capt:
self.html.append('%s
' % capt)
def finalize(self, result):
self.html.append('')
self.html.append("Ran %d test%s" %
(result.testsRun, result.testsRun != 1 and "s" or ""))
self.html.append('
')
self.html.append('')
if not result.wasSuccessful():
self.html.extend(['FAILED ( ',
'failures=%d ' % len(result.failures),
'errors=%d' % len(result.errors),
')'])
else:
self.html.append('OK')
self.html.append('
')
# print >> sys.stderr, self.html
for l in self.html:
self.stream.writeln(l)
def formatErr(self, err):
exctype, value, tb = err
return traceback.format_exception(exctype, value, tb)
def setOutputStream(self, stream):
# grab for own use
self.stream = stream
# return dummy stream
class dummy:
def write(self, *arg):
pass
def writeln(self, *arg):
pass
d = dummy()
return d
def startTest(self, test):
self.html.extend([ '',
test.shortDescription() or str(test),
'' ])
def stopTest(self, test):
self.html.append('
')