研究了下好几个接口自动化报告的模板,自己改造了个带饼图的接口自动化模板,先贴报告吧~
在贴代码如下:
# *-*coding:utf-8*-*
"""
@version: Python3.4.4
@author: Hszhang
@time: 2017/6/14 16:34
- 使用bottle来动态生成html
- https://www.reddit.com/r/learnpython/comments/2sfeg0/using_template_engine_with_python_for_generating/
"""
from bottle import template
import webbrowser
import json
import sys
html_template = u"""
接口测试报告
测试基本信息
开始时间
{{start_time}}
结束时间
{{end_time}}
测试用时
{{used_time}}
总用例数
{{sum_all_cases}}
执行用例数
{{sum_executed_cases}}
跳过用例数
{{sum_untreaded_cases}}
"""
REPORT_TMPL_Lable = '''
'''
REPORT_TMPL_Title = '''
'''
REPORT_Title_Width = '''
'''
REPORT_Title_Lable = '''
{}
'''
REPORT_Table_Data = '''
{}
'''
REPORT_Table_Detail = '''
详细
'''
EXPORT_Detali_text = '''
'''
class HTML_REPORT:
def __init__(self, html_title, pie_theme, test_info, report_table_title, report_cases):
'''
@parameter html_title:HTML标题,字符串类型,如html_title = u'接口测试报告'
@parameter pie_theme:,饼图名称,字符串类型,pie_theme = u'接口数据统计'
@parameter test_info:,饼图数据统计,列表类型,长度 = 6,如test_info = ['2016-12-02 15:59:17', '2016-12-02 16:10:22', '11 分 4 秒', len(report_cases['all_cases']), len(report_cases['right_cases']) + len(report_cases['error_cases']), len(report_cases['untreaded_cases'])]
@parameter report_table_title:接口表格标题,列表类型,可以自定义,report_cases = ['项目名称', '用例编号', '用例名称', '接口地址', '接口方法', '测试结果', '详细信息']
@parameter report_cases:接口测试结果记录,字典格式且key不能变,report_cases = {'all_cases':[['WEB理财', '1', '登录', 'login', 'post', 'pass', '详细信息', ''],['WEB理财', '2', '产品列表', 'list', 'post', 'error', '详细信息', '']],
'right_cases':[['WEB理财', '1', '登录', 'login', 'post', 'pass', '详细信息', '']],
'error_cases':[['WEB理财', '2', '产品列表', 'list', 'post', 'error', '详细信息', '']],
'untreaded_cases':[['WEB理财', '3', '测试', 'login', 'post', '/', '详细信息', '']]
}
'''
if isinstance(html_title, str) and len(html_title) > 0:
self.html_title = html_title
else:
self.html_title = u'接口测试报告'
if isinstance(pie_theme, str) and len(pie_theme) > 0:
self.pie_theme = pie_theme
else:
self.theme = u'接口数据统计'
if isinstance(report_table_title, list) and len(report_table_title) > 0:
self.report_table_title = report_table_title
else:
print('nothing of report_table_title.')
sys.exit(0)
if isinstance(test_info, list) and 6 == len(test_info):
self.test_info = test_info
else:
print('nothing of test_info.')
sys.exit(0)
if isinstance(report_cases, dict) and len(report_cases) > 0:
self.report_cases = report_cases
self.pie_sum_number = {'right_sum': len(report_cases['right_cases']) ,'error_sum': len(report_cases['error_cases']), 'untreated_sum': len(report_cases['untreaded_cases'])}
else:
print('nothing of report_cases or length is not 6.')
sys.exit(0)
def packagedCases(self, export_lable_title, data_key, class_name, pannel_num):
cases_num = 1
cases_packaged = ''
cases_packaged = ''.join([cases_packaged,\
'
\n'.format(class_name),\
export_lable_title,
' \n'])
for data_case in self.report_cases[data_key]:
detail_id = ''.join([pannel_num, '-detail-', str(cases_num)])
hidden_id = ''.join([pannel_num, '-hidden-', str(cases_num)])
cases_num += 1
cases_packaged = ''.join([cases_packaged, '
\n'.format(class_name)])
for data in data_case[:-1]:
if '详细' == data:
cases_packaged = ''.join([cases_packaged, REPORT_Table_Detail.format(detail_id, hidden_id, 'success', detail_id)])
else:
cases_packaged = ''.join([cases_packaged, REPORT_Table_Data.format(data)])
cases_packaged = ''.join([cases_packaged, ' \n'])
cases_packaged = ''.join([cases_packaged, EXPORT_Detali_text.format(hidden_id, len(data_case) - 1, data_case[-1])])
return cases_packaged
def genHtmlReport(self, html_template):
export_lable_title = ''
for lable_tile in self.report_table_title:
export_lable_title = ''.join([export_lable_title, REPORT_Title_Lable.format(lable_tile)])
str_right_datas = ''
str_untreated_cases = ''
str_error_cases = ''
str_all_cases = ''
for data_key in self.report_cases.keys():
if 'right_cases' == data_key:
str_right_datas = self.packagedCases(export_lable_title, data_key, 'success', 'panel1')
elif 'untreaded_cases' == data_key:
str_untreated_cases = self.packagedCases(export_lable_title, data_key, 'untreaded', 'panel3')
elif 'error_cases' == data_key:
str_error_cases = self.packagedCases(export_lable_title, data_key, 'error', 'panel2')
else:
str_all_cases = self.packagedCases(export_lable_title, data_key, 'all', 'panel0')
'''
right_cases_num = 1
str_right_datas = ''.join([str_right_datas,\
'
\n',\
export_lable_title,
' \n'])
for data_case in self.report_cases[data_key]:
right_detail_id = ''.join(['panel1-detail-', str(right_cases_num)])
right_hidden_id = ''.join(['panel1-hidden-', str(right_cases_num)])
right_cases_num += 1
str_right_datas = ''.join([str_right_datas, '
\n'])
for data in data_case[:-1]:
if '详细' == data:
str_right_datas = ''.join([str_right_datas, REPORT_Table_Detail.format(right_detail_id, right_hidden_id, 'success', right_detail_id)])
else:
str_right_datas = ''.join([str_right_datas, REPORT_Table_Data.format(data)])
str_right_datas = ''.join([str_right_datas, ' \n'])
str_right_datas = ''.join([str_right_datas, EXPORT_Detali_text.format(right_hidden_id, len(data_case) - 1, data_case[-1])])
elif 'untreaded_cases' == data_key:
str_untreated_cases = ''.join([str_untreated_cases,\
'
\n',\
export_lable_title,
' \n'])
for data_case in self.report_cases[data_key][:-1]:
str_untreated_cases = ''.join([str_untreated_cases, '
\n'])
for data in data_case:
if '详细' in data:
str_untreated_cases = ''.join([str_untreated_cases,REPORT_Table_Detail.format(data)])
else:
str_untreated_cases = ''.join([str_untreated_cases, REPORT_Table_Data.format(data)])
str_untreated_cases = ''.join([str_untreated_cases, ' \n'])
elif 'error_cases' == data_key:
str_error_cases = ''.join([str_error_cases,\
export_lable_title,
'
\n',\
' \n'])
for data_case in self.report_cases[data_key][:-1]:
str_error_cases = ''.join([str_error_cases, '
\n'])
for data in data_case:
if '详细' in data:
str_error_cases = ''.join([str_error_cases,REPORT_Table_Detail.format(data)])
else:
str_error_cases = ''.join([str_error_cases, REPORT_Table_Data.format(data)])
str_error_cases = ''.join([str_error_cases, ' \n'])
elif 'all_cases' == data_key:
str_all_cases = ''.join([str_all_cases,\
'
\n',\
export_lable_title,
' \n'])
for data_case in self.report_cases[data_key][:-1]:
str_all_cases = ''.join([str_all_cases, '
\n'])
for data in data_case:
if '详细' == data:
str_all_cases = ''.join([str_all_cases, REPORT_Table_Detail.format(data)])
else:
str_all_cases = ''.join([str_all_cases, REPORT_Table_Data.format(data)])
str_all_cases = ''.join([str_all_cases, ' \n'])
'''
export_lable_datas = REPORT_TMPL_Lable.format(len(report_cases['all_cases']),len(report_cases['right_cases']),len(report_cases['error_cases']),len(report_cases['untreaded_cases']),\
str(str_all_cases[0:-1]),\
str(str_right_datas[0:-1]),\
str(str_error_cases[0:-1]),\
str(str_untreated_cases[0:-1]))
html_template = ''.join([html_template, export_lable_datas])
html = template(html_template, report_title = self.html_title, theme = self.pie_theme,\
start_time = self.test_info[0], end_time = self.test_info[1], used_time = self.test_info[2], sum_all_cases = self.test_info[3], sum_executed_cases = self.test_info[4], sum_untreaded_cases = self.test_info[5],\
right_sum = self.pie_sum_number['right_sum'], error_sum = self.pie_sum_number['error_sum'], untreated_sum = self.pie_sum_number['untreated_sum'])
return html
if __name__ == '__main__':
report_title = u'接口测试报告'
theme = u'接口数据统计'
report_table_title = ['项目名称', '用例编号', '用例名称', '接口地址', '接口方法', '测试结果', '详细信息']
case_detail = '''
sim_card_service.get_flow_by_serial Test failure!
Because of : Data check failure,AssertionError!
Data Should : sim_card_service.get_flow_by_serial
Data Actually : sim_card_service.get_flow_by_serial , 6.4413928985596
Response Json tree:
{
"msg": {
"message": "failed",
"code": -1,
"data": "该号码不存在"
},
"code": -1,
"trace": "sim_card_service.get_flow_by_serial , 6.4413928985596"
}
'''
report_cases = {'all_cases':[\
['理财', '1', '登录', 'lo144444444444444444444444444444444444444444444444444444444444444gin', 'post', 'Pass', '详细', case_detail],\
['WEB理财', '2', '产品列表', 'list', 'post', 'Fail', '详细', case_detail],\
['理财', '3', '测试', 'login', 'po6666666666666666666666666666666666666666666666666666st', '/', '详细', case_detail]\
],
'right_cases':[['理财', '1', '登录', 'login', 'po22222222222222222222222222222st', 'Pass', '详细', case_detail]],
'error_cases':[['理财', '2', '产品列表', 'list', 'pos5555555555555555555555t', 'Fail', '详细', case_detail]],
'untreaded_cases':[['WEB理财', '3', '测试', 'login', 'po6666666666666666666666666666666666666666666666666666st', '/', '详细', case_detail]]
}
test_info = ['2016-12-02 15:59:17', '2016-12-02 16:10:22', '11 分 4 秒', len(report_cases['all_cases']), len(report_cases['right_cases']) + len(report_cases['error_cases']), len(report_cases['untreaded_cases'])]
html_report_object = HTML_REPORT(report_title, theme, test_info, report_table_title, report_cases)
html = html_report_object.genHtmlReport(html_template)
with open("test_demo.html",'wb') as f:
f.write(html.encode('utf-8'))
#使用浏览器打开html
webbrowser.open("test_demo.html")