changeset 328:0bb16d2a5699

Added cool sphinx plugin for creation of unittest result table
author Windel Bouwman
date Tue, 04 Feb 2014 09:01:11 +0100
parents 61c9df5bffce
children 8f6f3ace4e78
files doc/conf.py doc/index.rst doc/unittesttable.py
diffstat 3 files changed, 74 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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.
--- 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
+
--- /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)