接口测试HTML报告模板(第一版 纯字符串拼接)

本报告模板示例,是用Python的requests做的接口测试,返回的是json格式的response。


效果截图:(看清晰图,右键新窗口打开图片)


# encoding: utf-8
"""
@author: lileilei
@site: 
@software: PyCharm
@file: py_Html.py
@time: 2017/6/5 17:04
"""
import  os
titles='接口测试'
def title(titles):
	title='''<!DOCTYPE html>
	<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>%s</title>
		<style type="text/css">
			td{ width:40px; height:50px;}
		</style>
	</head>
	<body>
	'''%(titles)
	return title
connent='''
<div style='width: 1170px;margin-left: 20%'>
<h1>接口测试的结果</h1>'''
def time(starttime,endtime,passge,fail):
	beijing='''
		<p><strong>开始时间:</strong> %s</p>
		<p><strong>结束时间:</strong> %s</p>
		<p><strong>耗时:</strong> %s</p>
		<p><strong>结果:</strong>
			<span >Pass: <strong >%s</strong>
			Fail: <strong >%s</strong>
			        </span></p>                  
			    <p ><strong>测试详情如下</strong></p>  </div> '''%(starttime,endtime,(endtime-starttime),passge,fail)
	return beijing
shanghai='''


        <p> </p>
        <table border='2'cellspacing='1' cellpadding='1' width='1100'align="center" >
		<tr >
            <td ><strong>用例ID </strong></td>
            <td><strong>用例名字</strong></td>
            <td><strong>key</strong></td>
            <td><strong>请求内容</strong></td>
            <td><strong>url</strong></td>
            <td><strong>请求方式</strong></td>
            <td><strong>预期</strong></td>
            <td><strong>实际返回</strong></td>  
            <td><strong>结果</strong></td>
        </tr>
    '''
def passfail(tend):
    if tend =='pass':
        htl=' <td bgcolor="green">pass</td>'
    elif tend =='fail':
        htl=' <td bgcolor="fail">fail</td>'
    else:
        htl='<td bgcolor="#8b0000">error</td>'
    return htl
def ceshixiangqing(id,name,key,coneent,url,meth,yuqi,json,relust):
    xiangqing='''
        <tr>
            <td>%s</td>
            <td>%s</td>
       
            <td>%s</td>
            <td>%s
           </td>
            <td>%s</td>
            <td>%s</td>
            <td>%s</td>
            <td>%s</td>
            %s
        </tr>
        
    '''%(id,name,key,coneent,url,meth,yuqi,json,passfail(relust))
    return xiangqing
weibu='''
	</table>
	
    </body>
    </html>'''

def relust(titles,starttime,endtime,passge,fail,id,name,key,coneent,url,meth,yuqi,json,relust):
    if type(name) ==list:
        relus=' '
        for i in range(len(name)):
            relus+=(ceshixiangqing(id[i],name[i],key[i],coneent[i],url[i],meth[i],int(yuqi[i]),json[i],relust[i]))
        text=title(titles)+connent+time(starttime,endtime,passge,fail)+shanghai+relus+weibu
    else:
        text=title(titles)+connent+time(starttime,endtime,passge,fail)+shanghai+ceshixiangqing(id,name,key,coneent,url,meth,int(yuqi),json,relust)+weibu
    return text
def createHtml(filepath,titles,starttime,endtime,passge,fail,id,name,key,coneent,url,meth,yuqi,json,relusts):
	texts=relust(titles,starttime,endtime,passge,fail,id,name,key,coneent,url,meth,yuqi,json,relusts)
	with open(filepath,'wb') as f:
		f.write(texts.encode())




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值