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 from ctypes.test.test_pickling import name 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 i in range(0,len(all_data)): if all_data[i]["name"] =="粉絲": content+="<tr height=40 bgcolor='red'>" content+="<td width='7%%' >%s</d>" %str(all_data[i]["name"]) content+="<td width='7%%'>%s</d>" %str(all_data[i]["age"]) content+="<td width='7%%'>%s</d>" %str(all_data[i]["sex"]) content+="<td width='7%%'>%s</d>" %str(all_data[i]["school"]) content+="<td width='7%%'>%s</d>" %str(all_data[i]["work"]) content+="<td width='7%%'>%s</d>" %str(all_data[i]["address"]) content+="<td width='7%%'>%s</d>" %str(all_data[i]["sex1"]) content+="<td width='7%%'>%s</d>" %str(all_data[i]["school2"]) content+="<td width='7%%'>%s</d>" %str(all_data[i]["work3"]) content+="<td width='7%%'>%s</d>" %str(all_data[i]["address4"]) content+="</tr>" else: content+="<tr height=40 bgcolor='bisque'>" content+="<td width='7%%' >%s</d>" %str(all_data[i]["name"]) content+="<td width='7%%'>%s</d>" %str(all_data[i]["age"]) content+="<td width='7%%'>%s</d>" %str(all_data[i]["sex"]) content+="<td width='7%%'>%s</d>" %str(all_data[i]["school"]) content+="<td width='7%%'>%s</d>" %str(all_data[i]["work"]) content+="<td width='7%%'>%s</d>" %str(all_data[i]["address"]) content+="<td width='7%%'>%s</d>" %str(all_data[i]["sex1"]) content+="<td width='7%%'>%s</d>" %str(all_data[i]["school2"]) content+="<td width='7%%'>%s</d>" %str(all_data[i]["work3"]) content+="<td width='7%%'>%s</d>" %str(all_data[i]["address4"]) content+="</tr>" # 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=[ {"name":"Tom","age":23,"sex":"男","school":"希望小學","work":"學生","address":"上海","sex1":"男","school2":"希望小學2","work3":"學生","address4":"上海"}, {"name":"數據","age":"13","sex":"男","school":"希望小學","work":"學生","address":"上海","sex1":"男","school2":"希望小3學","work3":"學生","address4":"上海"}, {"name":"粉絲","age":"13","sex":"男","school":"希望小學","work":"學生","address":"上海","sex1":"男","school2":"希望小學4","work3":"學生","address4":"上海"}, {"name":"阿道夫","age":"13","sex":"男","school":"希望小學","work":"學生","address":"上海","sex1":"男","school2":"希望小fs學","work3":"學生","address4":"上海"}, {"name":"二娃","age":"13","sex":"男","school":"希望小學","work":"學生","address":"上海","sex1":"男","school2":"希望小學fa","work3":"學生","address4":"上海"}, {"name":"我確認","age":"13","sex":"男","school":"希望小學","work":"學生","address":"上海","sex1":"男","school2":"希望小學","work3":"學生","address4":"上海"}, {"name":"特務","age":"13","sex":"男","school":"希望小學","work":"學生","address":"上海","sex1":"男","school2":"希望小學fa","work3":"學生","address4":"上海"}, {"name":"突然","age":"13","sex":"男","school":"希望小學","work":"學生","address":"上海","sex1":"男","school2":"希望小學ddd","work3":"學生","address4":"上海"}, {"name":"圈兒","age":"13","sex":"男","school":"希望小學","work":"學生","address":"上海","sex1":"男","school2":"希望小asss學","work3":"學生","address4":"上海"}, {"name":"讓他","age":"13","sex":"男","school":"希望小學","work":"學生","address":"上海","sex1":"男","school2":"希望小學fdsaa","work3":"學生","address4":"上海"}, ] 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)
生成報告
工程目錄