-
Notifications
You must be signed in to change notification settings - Fork 11
/
local_report.py
316 lines (296 loc) · 10.7 KB
/
local_report.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/env python
# encoding: utf-8
"""
@project:auto_test_api
@author:cloudy
@site:
@file:local_report.py
@date:2017/10/16 11:39
"""
import os
from datetime import datetime
REPORT_HEAD_TMPL = """
<!DOCTYPE html>
<html>
<head>
<title>API测试报告</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 Shim 和 Respond.js 用于让 IE8 支持 HTML5元素和媒体查询 -->
<!-- 注意: 如果通过 file:// 引入 Respond.js 文件,则该文件无法起效果 -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
<style type="text/css">
.hidden-detail,.hidden-tr{
display:none;
}
</style>
</head>
<body>
"""
REPORT_HEAD_INFO_TMPL = """
<div class="row">
<div class="col-md-4 col-md-offset-4">
<h1 class="text-center">测试报告</h1>
<table class="table table-hover table-condensed">
<tbody>
<tr>
<td>开始时间</td>
<td>%(start_time)s</td>
</tr>
<tr>
<td>结束时间</td>
<td>%(end_time)s</td>
</tr>
<tr>
<td>用例总数</td>
<td >%(all_count)s</td>
</tr>
<tr>
<td>成功数量</td>
<td>%(success_count)s</td>
</tr>
<tr>
<td>跳过数量</td>
<td>%(skip_count)s</td>
</tr>
<tr>
<td>失败数量</td>
<td style="color:red">%(failure_count)s</td>
</tr>
<tr>
<td>成功率</td>
<td style="color:red">%(success_percent)s%%</td>
</tr>
</tbody>
</table>
</div>
</div>
"""
REPORT_MAIN_TMPL = """
<div class="row" style="margin:20px">
<div >
<div class="btn-group" role="group" aria-label="...">
<button type="button" id="check-all" class="btn btn-primary">所有用例</button>
<button type="button" id="check-success" class="btn btn-success">成功用例</button>
<button type="button" id="check-danger" class="btn btn-danger">失败用例</button>
<button type="button" id="check-warning" class="btn btn-warning">跳过用例</button>
</div>
<div class="btn-group" role="group" aria-label="...">
<button type="button" id="check-request" class="btn btn-primary">查看用例请求信息</button>
<button type="button" id="hidden-request" class="btn btn-success">隐藏用例请求信息</button>
</div>
<table class="table table-hover table-condensed table-bordered" style="word-wrap:break-word; word-break:break-all;">
<thead>
<tr>
<td>分类</td>
<td>功能模块</td>
<td>功能点</td>
<td>用例名称</td>
<td>有效</td>
<td>调试</td>
<td>地址</td>
<td>请求方法</td>
<td>数据位置</td>
<td>数据类型</td>
<td>检查点</td>
<td>检查内容</td>
<td width="50%%">请求数据</td>
<td>结果</td>
<td>操作</td>
</tr>
</thead>
<tbody>
%(all_lines_str)s
</tbody>
</table>
</div>
</div>
"""
REPORT_CASE_LINE_TMPL = """
<tr class="case-tr %(clazz)s">
<td>%(category)s</td>
<td>%(module)s</td>
<td>%(function)s</td>
<td>%(case_name)s</td>
<td>%(active)s</td>
<td>%(case_debug)s</td>
<td>%(address)s</td>
<td>%(request_method)s</td>
<td>%(data_source)s</td>
<td>%(data_type)s</td>
<td>%(check_point)s</td>
<td>%(check_content)s</td>
<td width="30%%">%(request_data)s</td>
<td>%(result)s</td>
<td>
<button type="button" class="btn btn-primary display-detail btn-sm">查看请求响应信息</button>
</td>
</tr>
<tr class="request-info hidden-detail"><td>请求:</td><td colspan="14">%(req_data)s</td></tr>
<tr class="request-info hidden-detail"><td>响应:</td><td colspan="14">%(response_text)s</td></tr>
"""
REPORT_FOOTER_TMPL = """
<script src="https://code.jquery.com/jquery.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(".display-detail").click(function(e){
var currentBtn = e.currentTarget;
var currentTr = currentBtn.parentNode.parentNode;
if($(currentBtn).text()=="查看请求响应信息"){
$(currentTr).next("tr").removeClass('hidden-detail');
$(currentTr).next("tr").next("tr").removeClass('hidden-detail');
$(currentBtn).text("隐藏请求响应信息");
}else{
$(currentTr).next("tr").addClass('hidden-detail');
$(currentTr).next("tr").next("tr").addClass('hidden-detail');
$(currentBtn).text("查看请求响应信息");
};
});
$("#check-danger").click(function(e){
$(".case-tr").removeClass("hidden-tr");
$(".success").addClass("hidden-tr");
$(".warning").addClass("hidden-tr");
$(".request-info").addClass("hidden-detail");
});
$("#check-warning").click(function(e){
$(".case-tr").removeClass("hidden-tr");
$(".success").addClass("hidden-tr");
$(".danger").addClass("hidden-tr");
$(".request-info").addClass("hidden-detail");
});
$("#check-success").click(function(e){
$(".case-tr").removeClass("hidden-tr");
$(".warning").addClass("hidden-tr");
$(".danger").addClass("hidden-tr");
$(".request-info").addClass("hidden-detail");
});
$("#check-all").click(function(e){
$(".case-tr").removeClass("hidden-tr");
$(".request-info").addClass("hidden-detail");
});
$("#check-request").click(function(e){
$(".case-tr").each(function(){
if ($(this).hasClass("hidden-tr") == false){
$(this).next("tr").removeClass('hidden-detail');
$(this).next("tr").next("tr").removeClass('hidden-detail');
}
});
});
$("#hidden-request").click(function(e){
$(".request-info").addClass("hidden-detail");
});
</script>
</body>
</html>
"""
class LocalReport(object):
"""
生成本地报告
"""
def __init__(self):
self.report_path = os.path.join(os.getcwd() , "local_report")
if not os.path.exists(self.report_path):
os.makedirs(self.report_path)
self.success_count = 0
self.skip_count = 0
self.failure_count = 0
self.success_percent = 0
self.start_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.end_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.all_lines_trs = ""
self.report_head_info = ""
def gen_case_line(self, line={}):
"""
根据用例明细生成html用例结果信息
:param line:
:return:
"""
case_info = line.case_info
if line.result == "success":
self.success_count += 1
clazz = "success"
elif line.result == "skip":
self.skip_count += 1
clazz = "warning"
else:
self.failure_count += 1
clazz = "danger"
self.success_percent = self.success_count*100 / (self.success_count + self.failure_count + self.skip_count)
response_text = line.response.text
try:
req_data = line.req_data.encode('utf-8').decode('unicode_escape')
except:
req_data = line.req_data
self.all_lines_trs += REPORT_CASE_LINE_TMPL % dict(
category=case_info.get('case_category', ""),
module=case_info.get('case_module', ""),
function=case_info.get('case_function', ""),
address=case_info.get("case_address", ""),
data_source=case_info.get("case_data_location"),
data_type=case_info.get("case_data_type"),
active=case_info.get("case_active", u"未知"),
check_content=case_info.get("case_check_content", ""),
check_point=case_info.get("case_check_point", ""),
case_name=case_info.get('case_name', ""),
case_debug=case_info.get("case_debug", u"未知"),
request_method=case_info.get("case_method", u"未知"),
request_data=case_info.get("case_request_data", ""),
result=line.result,
clazz=clazz,
response_text=response_text,
req_data=req_data,
)
def gen_case_lines(self, lines=[]):
"""
根据用例明细生成html用例结果信息
:param lines:
:return:
"""
for line in lines:
self.all_lines_trs += REPORT_CASE_LINE_TMPL % dict(
category=line.category,
module=line.module,
function=line.function,
case_name=line.case_name,
condition=line.condition,
step=line.step,
result=line.result,
)
if line.result == "success":
self.success_count += 1
elif line.result == "skip":
self.skip_count += 1
else:
self.failure_count += 1
def gen_report_info(self):
"""
报告头部生成
:return:
"""
self.report_head_info = REPORT_HEAD_INFO_TMPL % dict(
success_count=self.success_count,
skip_count=self.skip_count,
failure_count=self.failure_count,
success_percent=self.success_percent,
start_time=self.start_time,
end_time=self.end_time,
all_count=self.failure_count+self.success_count+self.skip_count
)
def save(self, file_name):
"""
保存文件
:return:
"""
self.end_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
report = os.path.join(self.report_path, file_name)
print report
fp = open(report, "w")
fp.write(REPORT_HEAD_TMPL)
fp.write(self.report_head_info)
fp.write(REPORT_MAIN_TMPL % dict(all_lines_str=self.all_lines_trs))
fp.write(REPORT_FOOTER_TMPL)
fp.close()