import time
class HtmlReport(object):
__style_html = '''
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: table-row;
}
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;
}
}
'''
__report_html = '''
接口测试报告
{style}
接口测试报告
测试时间: {date} |
测试概况
用例总数 | 通过数量 | 失败数量 | 运行时间 |
---|---|---|---|
{all} | {ok} | {fail} | {run_time} s |
接口详细
所属项目 | 模块 | 用例描述 | URL | 测试人员 | 用例状态 |
---|
'''
__case_html = '''
{project}{model}{detail}{url}{tester}{status} 查看接口详细
请求/返回 "{project}"请求报文 | 返回报文 |
---|---|
{request} | {response} |
'''
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()