forked from felipecruz/exemplos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08_06_custom_output.py
65 lines (53 loc) · 1.5 KB
/
08_06_custom_output.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import unittest
HTML = '''\
<html>
<head>
<title>unittest output</title>
</head>
<body>
<table>
{}
</table>
</body>
</html>'''
OK_TD = '<tr><td style="color: green;">{}</td></tr>'
ERR_TD = '<tr><td style="color: red;">{}</td></tr>'
class HTMLTestResult(unittest.TestResult):
def __init__(self, runner):
unittest.TestResult.__init__(self)
self.runner = runner
self.infos = []
self.current = {}
def newTest(self):
self.infos.append(self.current)
self.current = {}
def startTest(self, test):
self.current['id'] = test.id()
def addSuccess(self, test):
self.current['result'] = 'ok'
self.newTest()
def addError(self, test, err):
self.current['result'] = 'error'
self.newTest()
def addFailure(self, test, err):
self.current['result'] = 'fail'
self.newTest()
def addSkip(self, test, err):
self.current['result'] = 'skipped'
self.current['reason'] = err
self.newTest()
class HTMLTestRunner:
def run(self, test):
result = HTMLTestResult(self)
test.run(result)
table = ''
for item in result.infos:
if item['result'] == 'ok':
table += OK_TD.format(item['id'])
else:
table += ERR_TD.format(item['id'])
print(HTML.format(table))
return result
if __name__ == "__main__":
suite = unittest.TestLoader().discover('.')
HTMLTestRunner().run(suite)