day12_框架一report.py代碼
阿新 • • 發佈:2018-03-21
ros javascrip microsoft report port doc UNC ica table
import os
import time
from conf.settings import REPORT_PATH
class HtmlReport(object):
__style_html = ‘‘‘
<style type="text/css">
body {
font:normal 68% verdana,arial,helvetica;
color:#000000;
}
table tr td, table tr th {
font-size: 68%;
}
table.details tr th{
color: #ffffff;
font-weight: bold;
text-align:center;
background:#2674a6;
}
table.details tr td{
background:#eeeee0;
}
h1 {
margin: 0px 0px 5px; font: 165% verdana,arial,helvetica
}
h2 {
margin-top: 1em; margin-bottom: 0.5em; font: bold 125% verdana,arial,helvetica
}
h3 {
margin-bottom: 0.5em; font: bold 115% verdana,arial,helvetica
}
.Failure {
font-weight:bold; color:red;
}
img
{
border-width: 0px;
}
.expand_link
{
position=absolute;
right: 0px;
width: 27px;
top: 1px;
height: 27px;
}
.page_details
{
display: none;
}
.page_details_expanded
{
display: block;
display/* hide this definition from IE5/6 */: table-row;
}
</style>
<script language="JavaScript">
function show(details_id)
{
var close = ‘page_details‘;
var show = ‘page_details_expanded‘;
if (document.getElementById(details_id).className==close){
document.getElementById(details_id).className = show;
}
else {
document.getElementById(details_id).className = close;
}
}
</script>
‘‘‘
__report_html = ‘‘‘
<!DOCTYPE html>
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接口測試報告</title>
{style}
</head>
<body>
<h1>接口測試報告</h1>
<table width="100%">
<tr>
<td align="left">測試時間: {date}</td>
</tr>
</table>
<hr size="test1">
<h2>測試概況</h2>
<table width="95%" cellspacing="2" cellpadding="5" border="0" class="details" align="center">
<tr valign="top">
<th>用例總數</th><th>通過數量</th><th>失敗數量</th><th>運行時間</th>
</tr>
<tr valign="top" class="">
<td align="center">{all}</td><td align="center">{ok}</td><td align="center">{fail}</td><td align="center">{run_time} s</td>
</tr>
</table>
<hr align="center" width="95%" size="test1">
<h2>接口詳細</h2>
<table width="95%" cellspacing="2" cellpadding="5" border="0" class="details" align="center">
<tr valign="top">
<th>所屬項目</th><th>模塊</th><th>用例描述</th><th>URL</th><th>測試人員</th><th>用例狀態</th><th></th>
</tr>{case_res}</table>
<hr align="center" width="95%" size="test1">
</body>
</html>
‘‘‘
__case_html = ‘‘‘
<tr valign="top" class="">
<td>{project}</td><td align="center">{model}</td><td align="center">{detail}</td><td align="center">{url}</td><td align="center">{tester}</td><td align="center">{status}</td><td align="center"><a href="#" onclick="show(‘page_details_{case_id}‘);">查看接口詳細</a></td>
</tr>
<tr class="page_details" id="page_details_{case_id}">
<td bgcolor="#FF0000" colspan="8">
<div align="center">
<b>請求/返回 "{project}"</b>
<table width="95%" cellspacing="test1" cellpadding="test1" border="0" bgcolor="#2674A6" bordercolor="#000000">
<tr>
<th>請求報文</th><th>返回報文</th>
</tr>
<tr>
<td align="center" style="width :300px;word-break: break-all;"><span>{request}</span></td><td align="center" style="width :300px;word-break: break-all;" ><span>{response}</span></td>
</tr>
</table>
</div>
</td>
</tr>
‘‘‘
def __init__(self,report_dic):
‘‘‘
:param report_dic:生成報告需要用的字典
{
"all": 5,#運行用例數量
"ok": 4,#通過數量
"fail": test1,#失敗數量
"run_time": 100,#運行時間,單位s
"case_res": [{}],#每條用例的執行結果,
case_res:
{
"case_id":"001",#用例id
"project":"易品",#所屬項目
"model":"登錄",#模塊
"detail":"正常登錄",#用例標題
"url":"http://10.165.124.28:8080/q", #請求url
"tester":"牛牛", #測試人員
"status":"通過",#測試結果
"request":"a=test1&b=2",#請求報文
"response":"{‘code‘:200,‘msg‘:‘操作成功‘}"#返回報文
}
}
‘‘‘
self.report_dic = report_dic
def report(self):
res_list_html = ‘‘
res_list = self.report_dic.get(‘case_res‘)
for res in res_list:
res_list_html+=self.__case_html.format(**res)
self.report_dic[‘case_res‘]=res_list_html
self.report_dic[‘style‘] = self.__style_html
self.report_dic[‘date‘] = time.strftime(‘%Y/%m/%d %H:%M:%S‘)
self.__write_file()
return self.file_name
def __write_file(self):
self.file_name = os.path.join(REPORT_PATH,‘{date}_TestReport.html‘.format(date=time.strftime(‘%Y%m%d%H%M%S‘)))
with open(self.file_name,‘w‘,encoding=‘utf-8‘) as fw:
fw.write(self.__report_html.format(**self.report_dic))
if __name__ == ‘__main__‘:
res_list = [
{
"case_id":"1",
"project":"易品",
"model":"登錄",
"detail":"正常登錄",
"url":"http://10.165.124.28:8080/q",
"tester":"牛牛",
"status":"通過",
"request":"a=1&b=2",
"response":"{‘code‘:200,‘msg‘:‘操作成功‘}"
},
{
"case_id":"2",
"project":"易品",
"model":"登錄",
"detail":"正常登錄",
"url":"http://10.165.124.28:8080/q11111",
"tester":"牛牛111",
"status":"通過",
"request":"a=1&b=2",
"response":"{‘code‘:200,‘msg‘:‘操作成功‘}"
},
]
all = {
"all":2, # 總共多少條用例
"ok":1, # 通過的
"fail":1, # 失敗
"run_time":100, # 運行了多久
"case_res":res_list,
"date": time.strftime(‘%Y/%m/%d %H:%M:%S‘) # 什麽時候執行的
}
a = HtmlReport(all)
a.report()
day12_框架一report.py代碼