comparison doc/unittesttable.py @ 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
children
comparison
equal deleted inserted replaced
327:61c9df5bffce 328:0bb16d2a5699
1 from docutils import nodes
2 from sphinx.util.compat import Directive
3 import unittest
4
5
6 class RecordingTestResult(unittest.TestResult):
7 def __init__(self):
8 super().__init__()
9 self.successfull_tests = []
10 self.failed_tests = []
11 self.skipped_tests = []
12
13 def addSuccess(self, tst):
14 super().addSuccess(tst)
15 self.successfull_tests.append(tst)
16
17 def addFailure(self, tst, err):
18 super().addFailure(tst, err)
19 self.failed_tests.append(tst)
20 def addError(self, tst, err):
21 super().addError(tst, err)
22 self.failed_tests.append(tst)
23 def addSkip(self, tst, reason):
24 super().addSkip(tst, reason)
25 self.skipped_tests.append(tst)
26
27 def makeTable(rows):
28 # Buildup table:
29 table = nodes.table()
30 group = nodes.tgroup()
31 group += nodes.colspec('', colwidth=100)
32 group += nodes.colspec('', colwidth=50)
33 table += group
34 tbody = nodes.tbody('')
35 group += tbody
36 for c1, c2 in rows:
37 row = nodes.row('')
38 row += nodes.entry('', nodes.paragraph('', c1))
39 row += nodes.entry('', nodes.paragraph('', c2))
40 tbody += row
41 return table
42
43 class TestResults(Directive):
44 required_arguments = 1
45 optional_arguments = 0
46 option_spec = {}
47 has_content = False
48 node_class = None
49
50 def run(self):
51 # Run unit test:
52 tl = unittest.TestLoader()
53 result = RecordingTestResult()
54 tests = tl.discover('../test')
55 tests.run(result)
56 para = nodes.paragraph()
57 para += nodes.Text('Unit test results:')
58 res = []
59 for s in result.successfull_tests:
60 res.append((str(s), 'OK'))
61 for s in result.skipped_tests:
62 res.append((str(s), 'SKIP'))
63 for s in result.failed_tests:
64 res.append((str(s), 'FAIL'))
65 table = makeTable(res)
66
67 return [para, table]
68
69 def setup(app):
70 app.add_directive('testresults', TestResults)