python习题:封装好的测试报告(report.py)

import time

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="1">
<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="1">
<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="1">
</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="#" οnclick="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="1" cellpadding="1" 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": 1,#失败数量
"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=1&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()
def __write_file(self):
with open('{date}_TestReport.html'.format(date=time.strftime('%Y%m%d%H%M%S')),'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':'操作成功'}"
}
]

all = {
"all":5,
"ok":4,
"fail":1,
"run_time":100,
"case_res":res_list,
"date": time.strftime('%Y/%m/%d %H:%M:%S')
}

a = HtmlReport(all)
a.report()

转载于:https://www.cnblogs.com/blackbird0423/p/8543327.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值