python生成接口自动化测试报告模版

泪湿孤枕 提交于 2019-11-26 11:35:34

1:准备html模版

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>接口自动化</title>
</head>
<body style="margin-top: 20px; font-style: '微软雅黑';">

<table border="1" cellspacing="0" cellpadding="5" width="100%" align="center">
    <tr bgcolor="bisque" style="font-style: 30px">

        <td height="60" colspan="5">
            自动化测试报告:&test_data
            
        </td>
    </tr>
    <tr style="font-style: 20px">
        <td width="20%">被测版本:&version</td>
        <td width="15%">成功:&pass</td>
        <td width="15%">失败:&fail</td>
        <td width="15%">错误:&error</td>
        <td width="30%">最后时间:&lasttime</td>
    </tr>
</table>
<p></p>

<table border="1" cellspacing="0" cellpadding="5" width="100%" align="center">
    <tr height="40" bgcolor="darkseagreen">
        <td width="7%">记录编号:</td>
        <td width="10%">记录编号:</td>
        <td width="9%">记录编号:</td>
        <td width="7%">记录编号:</td>
        <td width="20%">记录编号:</td>
        <td width="7%">记录编号:</td>
        <td width="15%">记录编号:</td>
        <td width="15%">记录编号:</td>
        <td width="10%">记录编号:</td>
        <td width="10%">记录编号:</td>
    </tr>
    &test_result

</table>
</body>
</html>

 打开模版,数据替换

'''
Created on Aug 8, 2019

@author: liliang
'''
import os
import time
class MyClass():
    '''
    classdocs
    '''
    def __init__(self):
        pass
    def open_file(self):
        tempfile=os.path.abspath("..")+"/demo/2.html"
        tem=open(tempfile, mode='r', encoding='utf-8').read()
        return tem
    def write_file(self,file,test_data,version,passnum,faillnum,errornum,lasttime,all_data):
        file=file.replace("&test_data",test_data)
        file=file.replace("&version",version)
        file=file.replace('&pass',passnum)
        file=file.replace('&fail',faillnum)
        file=file.replace('&error',errornum)
        file=file.replace('&lasttime',lasttime)
        content=''
        for result in all_data:
            content+="<tr height=40>"
            content+="<td width='7%%'>%s</d>" %str(result[0])
            content+="<td width='7%%'>%s</d>" %str(result[1])
            content+="<td width='7%%'>%s</d>" %str(result[2])
            content+="<td width='7%%'>%s</d>" %result[3]
            content+="<td width='7%%'>%s</d>" %result[4]
            content+="<td width='7%%'>%s</d>" %result[5]
            content+="<td width='7%%'>%s</d>" %result[6]
            content+="<td width='7%%'>%s</d>" %result[7]
            content+="<td width='7%%'>%s</d>" %result[8]
            content+="<td width='7%%'>%s</d>" %result[9]
            content+="</tr>"
        file=file.replace('&test_result',content)
        file_folder=os.path.abspath("..")+"/demo/"
        file_name=file_folder+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+".html"
        report_file=open(file_name, mode="w+",  encoding='utf-8')
        report_file.write(file)
        report_file.close()
    def get_datetime(self):
        now_time=time.strftime("%Y_%M_%d_%H:%M:%S",time.localtime())
        return now_time
if __name__ == '__main__':
    my=MyClass() 
    file=my.open_file()
    all_data=[["fs","as","s","s","f","f","wef","fs","fs","wr"],["fs","as","s","s","f","f","wef","fs","fs","wr"],["fs","as","s","s","f","f","wef","fs","fs","wr"],["fs","as","s","s","f","f","wef","fs","fs","wr"],["fs","as","s","s","f","f","wef","fs","fs","wr"],["fs","as","s","s","f","f","wef","fs","fs","wr"],["fs","as","s","s","f","f","wef","fs","fs","wr"],["fs","as","s","s","f","f","wef","fs","fs","wr"],["fs","as","s","s","f","f","wef","fs","fs","wr"],["fs","as","s","s","f","f","wef","fs","fs","wr"]]
    my.write_file(file, "2019_07_08_03:07:59", "1.3.0", "90", "7", "key error", "2019_07_08_03:07:59", all_data)
        

 生成报告

工程目录

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!