forked from buweiqiang/AutotestCornerstone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTMLTestRunnerUTX.py
176 lines (151 loc) · 5.8 KB
/
HTMLTestRunnerUTX.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#! /usr/bin/env python
# -*- coding: UTF-8 -*-
import time
import os
import io
import datetime
import sys
import unittest
result_data = dict()
result_data['testResult'] = []
current_class_name = ""
STATUS = {
0: 'Pass',
1: 'Fail',
2: 'Error',
3: 'Skip',
}
class _TestResult(unittest.TestResult):
def __init__(self, verbosity=1):
super().__init__(verbosity)
self.outputBuffer = io.StringIO()
self.raw_stdout = None
self.raw_stderr = None
self.success_count = 0
self.failure_count = 0
self.skip_count = 0
self.error_count = 0
self.verbosity = verbosity
self.result = []
self._case_start_time = 0
self._case_run_time = 0
def startTest(self, test):
self._case_start_time = time.time()
super().startTest(test)
self.raw_stdout = sys.stdout
self.raw_stderr = sys.stderr
sys.stdout = self.outputBuffer
sys.stderr = self.outputBuffer
def complete_output(self):
self._case_run_time = time.time() - self._case_start_time
if self.raw_stdout:
sys.stdout = self.raw_stdout
sys.stderr = self.raw_stderr
result = self.outputBuffer.getvalue()
self.outputBuffer.seek(0)
self.outputBuffer.truncate()
return result
def stopTest(self, test):
self.complete_output()
def addSuccess(self, test):
self.success_count += 1
super().addSuccess(test)
output = self.complete_output()
self.result.append((0, test, output, '', self._case_run_time))
def addError(self, test, err):
self.error_count += 1
super().addError(test, err)
_, _exc_str = self.errors[-1]
output = self.complete_output()
self.result.append((2, test, output, _exc_str, self._case_run_time))
def addSkip(self, test, reason):
self.skip_count += 1
super().addSkip(test, reason)
self.result.append((3, test, "", "", 0.0))
def addFailure(self, test, err):
self.failure_count += 1
super().addFailure(test, err)
_, _exc_str = self.failures[-1]
output = self.complete_output()
self.result.append((1, test, output, _exc_str, self._case_run_time))
class HTMLTestRunner3(object):
def __init__(self, stream=sys.stdout, verbosity=1, title=None, description=None):
self.stream = stream
self.verbosity = verbosity
self.title = title
self.description = description
self.start_time = datetime.datetime.now()
self.stop_time = None
def run(self, test):
result = _TestResult(self.verbosity)
test(result)
self.stop_time = datetime.datetime.now()
self.analyze_test_result(result)
# log.info('Time Elapsed: {}'.format(self.stop_time - self.start_time))
from report_style import style_3
style_3.build_report(self.stream, result_data)
return result
@staticmethod
def sort_result(case_results):
rmap = {}
classes = []
for n, t, o, e, run_time in case_results:
cls = t.__class__
if cls not in rmap:
rmap[cls] = []
classes.append(cls)
rmap[cls].append((n, t, o, e, run_time))
r = [(cls, rmap[cls]) for cls in classes]
return r
def analyze_test_result(self, result):
result_data["reportName"] = self.title
result_data["beginTime"] = str(self.start_time)[:19]
result_data["totalTime"] = str(self.stop_time - self.start_time)
sorted_result = self.sort_result(result.result)
for cid, (cls, cls_results) in enumerate(sorted_result):
pass_num = fail_num = error_num = skip_num = 0
for case_state, *_ in cls_results:
if case_state == 0:
pass_num += 1
elif case_state == 1:
fail_num += 1
elif case_state == 2:
error_num += 1
else:
skip_num += 1
name = "{}.{}".format(cls.__module__, cls.__name__)
global current_class_name
current_class_name = name
for tid, (state_id, t, o, e, run_time) in enumerate(cls_results):
name = t.id().split('.')[-1]
doc = t.shortDescription() or ""
case_data = dict()
case_data['className'] = current_class_name
case_data['methodName'] = name
case_data['spendTime'] = "{:.2}S".format(run_time)
case_data['description'] = doc
case_data['log'] = o + e
if STATUS[state_id] == "Pass":
case_data['status'] = "成功"
if STATUS[state_id] == "Fail":
case_data['status'] = "失败"
if STATUS[state_id] == "Error":
case_data['status'] = "错误"
if STATUS[state_id] == "Skip":
case_data['status'] = "跳过"
result_data['testResult'].append(case_data)
result_data["testPass"] = result.success_count
result_data["testAll"] = result.success_count + result.failure_count + result.error_count + result.skip_count
result_data["testFail"] = result.failure_count
result_data["testSkip"] = result.skip_count
result_data["testError"] = result.error_count
class HTMLTestRunner4(HTMLTestRunner3):
def run(self, test):
result = _TestResult(self.verbosity)
test(result)
self.stop_time = datetime.datetime.now()
self.analyze_test_result(result)
# log.info('Time Elapsed: {}'.format(self.stop_time - self.start_time))
from report_style import style_4
style_4.build_report(self.stream, result_data)
return result