From cc452998dc1af441cf3f9c5d0c7287cb5de48c4a Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Sat, 30 May 2009 22:29:56 -0400 Subject: Initial implementation of nose testing. Still some cleanup to do, but this shows the differances between the old and the new. Also left one test failing (unsignificant white space only) to demonstrate what a failing test looks like. --- tests/plugins.py | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 tests/plugins.py (limited to 'tests/plugins.py') diff --git a/tests/plugins.py b/tests/plugins.py new file mode 100644 index 0000000..d0cb7f8 --- /dev/null +++ b/tests/plugins.py @@ -0,0 +1,113 @@ +import traceback +from util import MdSyntaxError +from nose.plugins import Plugin +from nose.plugins.errorclass import ErrorClass, ErrorClassPlugin + +class MdSyntaxErrorPlugin(ErrorClassPlugin): + """ Add MdSyntaxError and ensure proper formatting. """ + mdsyntax = ErrorClass(MdSyntaxError, label='MdSyntaxError', isfailure=True) + enabled = True + + def configure(self, options, conf): + self.conf = conf + + def addError(self, test, err): + """ Ensure other plugins see the error by returning nothing here. """ + pass + + def formatError(self, test, err): + """ Remove unnessecary and unhelpful traceback from error report. """ + et, ev, tb = err + if et.__name__ == 'MdSyntaxError': + return et, ev, '' + return err + + +def escape(html): + """ Escape HTML for display as source within HTML. """ + html = html.replace('&', '&') + html = html.replace('<', '<') + html = html.replace('>', '>') + return html + + +class HtmlOutput(Plugin): + """Output test results as ugly, unstyled html. """ + + name = 'html-output' + score = 2 # run late + enabled = True + + def __init__(self): + super(HtmlOutput, self).__init__() + self.html = [ '', + 'Test output', + '' ] + + def configure(self, options, conf): + self.conf = conf + + def addSuccess(self, test): + self.html.append('ok') + + def addError(self, test, err): + err = self.formatErr(err) + self.html.append('ERROR') + self.html.append('
%s
' % escape(err)) + + def addFailure(self, test, err): + err = self.formatErr(err) + self.html.append('FAIL') + self.html.append('
%s
' % escape(err)) + + 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)]) + for cls in result.errorClasses.keys(): + storage, label, isfail = result.errorClasses[cls] + if isfail: + self.html.append(' %ss=%d' % (label, len(storage))) + self.html.append(')') + else: + self.html.append('OK') + self.html.append('
') + f = open('tmp/test-output.html', 'w') + for l in self.html: + f.write(l) + f.close() + + def formatErr(self, err): + exctype, value, tb = err + return ''.join(traceback.format_exception(exctype, value, tb)) + + def startContext(self, ctx): + try: + n = ctx.__name__ + except AttributeError: + n = str(ctx).replace('<', '').replace('>', '') + self.html.extend(['
', '', n, '']) + try: + path = ctx.__file__.replace('.pyc', '.py') + self.html.extend(['
', path, '
']) + except AttributeError: + pass + + def stopContext(self, ctx): + self.html.append('
') + + def startTest(self, test): + self.html.extend([ '
', + test.shortDescription() or str(test), + '' ]) + + def stopTest(self, test): + self.html.append('
') + -- cgit v1.2.3