# HG changeset patch # User Windel Bouwman # Date 1391500871 -3600 # Node ID 0bb16d2a5699cd9bfd4ca44856386c85d833c366 # Parent 61c9df5bffcef37b0dacb037cba47e25d1984191 Added cool sphinx plugin for creation of unittest result table diff -r 61c9df5bffce -r 0bb16d2a5699 doc/conf.py --- a/doc/conf.py Sat Feb 01 17:21:21 2014 +0100 +++ b/doc/conf.py Tue Feb 04 09:01:11 2014 +0100 @@ -18,6 +18,7 @@ # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. sys.path.insert(0, os.path.abspath('../python')) +sys.path.insert(0, os.path.abspath('.')) # -- General configuration ----------------------------------------------------- @@ -26,7 +27,7 @@ # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. -extensions = ['sphinx.ext.graphviz', 'sphinx.ext.autodoc'] +extensions = ['sphinx.ext.graphviz', 'sphinx.ext.autodoc', 'unittesttable'] # 'sphinx.ext.inheritance_diagram'] # Add any paths that contain templates here, relative to this directory. diff -r 61c9df5bffce -r 0bb16d2a5699 doc/index.rst --- a/doc/index.rst Sat Feb 01 17:21:21 2014 +0100 +++ b/doc/index.rst Tue Feb 04 09:01:11 2014 +0100 @@ -16,3 +16,5 @@ .. include:: ../readme.rst +.. testresults:: ../test + diff -r 61c9df5bffce -r 0bb16d2a5699 doc/unittesttable.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/doc/unittesttable.py Tue Feb 04 09:01:11 2014 +0100 @@ -0,0 +1,70 @@ +from docutils import nodes +from sphinx.util.compat import Directive +import unittest + + +class RecordingTestResult(unittest.TestResult): + def __init__(self): + super().__init__() + self.successfull_tests = [] + self.failed_tests = [] + self.skipped_tests = [] + + def addSuccess(self, tst): + super().addSuccess(tst) + self.successfull_tests.append(tst) + + def addFailure(self, tst, err): + super().addFailure(tst, err) + self.failed_tests.append(tst) + def addError(self, tst, err): + super().addError(tst, err) + self.failed_tests.append(tst) + def addSkip(self, tst, reason): + super().addSkip(tst, reason) + self.skipped_tests.append(tst) + +def makeTable(rows): + # Buildup table: + table = nodes.table() + group = nodes.tgroup() + group += nodes.colspec('', colwidth=100) + group += nodes.colspec('', colwidth=50) + table += group + tbody = nodes.tbody('') + group += tbody + for c1, c2 in rows: + row = nodes.row('') + row += nodes.entry('', nodes.paragraph('', c1)) + row += nodes.entry('', nodes.paragraph('', c2)) + tbody += row + return table + +class TestResults(Directive): + required_arguments = 1 + optional_arguments = 0 + option_spec = {} + has_content = False + node_class = None + + def run(self): + # Run unit test: + tl = unittest.TestLoader() + result = RecordingTestResult() + tests = tl.discover('../test') + tests.run(result) + para = nodes.paragraph() + para += nodes.Text('Unit test results:') + res = [] + for s in result.successfull_tests: + res.append((str(s), 'OK')) + for s in result.skipped_tests: + res.append((str(s), 'SKIP')) + for s in result.failed_tests: + res.append((str(s), 'FAIL')) + table = makeTable(res) + + return [para, table] + +def setup(app): + app.add_directive('testresults', TestResults)