天天看點

python整合測試報告extendreport_python習題:封裝好的測試報告(report.py)接口測試報告

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()