测试报告效果:
1.类似htmltesrunner 的html文件
# -*- coding:utf-8 -*-
from xml.sax import saxutils
import sys
import datetime
import xlrd
import os
reload(sys)
sys.setdefaultencoding('utf8')
d = datetime.datetime.now()
HTML_TMPL = r"""
%(title)s
%(stylesheet)s
%(heading)s
%(report)s
%(ending)s
"""
# variables: (title, generator, stylesheet, heading, report, ending)
# ------------------------------------------------------------------------
# Stylesheet
#
# alternatively use a for external style sheet, e.g.
#
STYLESHEET_TMPL = """
"""
# ------------------------------------------------------------------------
# Heading
#
HEADING_TMPL = """
%(title)s
%(parameters)s
%(description)s
""" # variables: (title, parameters, description)
HEADING_ATTRIBUTE_TMPL = """%(name)s: %(value)s
""" # variables: (name, value)
# ------------------------------------------------------------------------
# Report
#
# Summary
# Failed
REPORT_TMPL = """
| %(Project_Name)s | 总数 | 通过 | 失败 | 查看 |
| 合计 | %(count)s | %(Pass)s | %(fail)s |
%(desc)s
%(script)s
%(desc)s
2.执行文件:
# -*- coding:utf-8 -*-
from result.reporthtml import *
from xml.sax import saxutils
import time
import os
# os.path("D:/daima/ORSD/result/RE.py" )
# path = os.path.dirname(os.path.realpath(__file__))
# print path
path_res = "D:/daima/ORSD/result"
path = "D:/daima/ORSD/dataconfig"
# path = os.path.dirname(os.path.realpath(__file__))
# print path
# print (os.path.isdir(path))
# if True == os.path.isdir(path):
# path_res = path
# else:
# path_res = os.path.dirname(path)
def getReportAttributes(startTime,duration,success_count,failure_count):
"""
Return report attributes as a list of (name, value).
Override this to add custom attributes.
"""
status = []
status.append('通过数 %s' % success_count)
status.append('失败数 %s' % failure_count)
if status:
status = ' '.join(status)
else:
status = 'none'
print status
startTime = time.strftime("%Y_%M_%d_%H:%M:%S", time.localtime())
return [
('开始时间', startTime),
('持续时间', duration),
('运行状态', status),
]
def generateReport(excel,sheet_name,project_Name,title,duration):
report, pass_count, fail_count = generate_report(excel, sheet_name,project_Name)
report_attrs = getReportAttributes(str(),duration,pass_count,fail_count)
generator = 'Ports_1.0'
stylesheet = generate_stylesheet()
heading = generate_heading(title,report_attrs)
ending = generate_ending()
output = HTML_TMPL % dict(
title=saxutils.escape(title),
generator=generator,
stylesheet=stylesheet,
heading=heading,
report=report,
ending=ending,
)
# self.stream.write(output.encode('utf8'))
return output
def generate_stylesheet():
return STYLESHEET_TMPL
def generate_heading(title,report_attrs):
a_lines = []
for name, value in report_attrs:
line = HEADING_ATTRIBUTE_TMPL % dict(
name=saxutils.escape(name),
value=saxutils.escape(value),
)
a_lines.append(line)
heading = HEADING_TMPL % dict(
title=saxutils.escape(title),
parameters=''.join(a_lines),
description=saxutils.escape(u"执行情况"),
)
return heading
def exceldata(excel_path, sheet_name):
wb = xlrd.open_workbook(path + '/'+ excel_path)
sheet = wb.sheet_by_name(sheet_name)
return sheet
def Duplicate_removal(info_list):
if len(info_list) != 0:
Info = []
[Info.append(i) for i in info_list if not i in Info]
else:
Info = []
return Info
def getexceldata(excel,sheet_name):
sheet = exceldata(excel, sheet_name)
a = [];
b = [];
c = []
for x in range(sheet.nrows - 1):
cells = sheet.row_values(x + 1)
a.append(cells[0])
if cells[11] == 'pass':#统计成功
b.append("pass")
elif cells[11] != 'pass':#统计失败
c.append("FAIL")
#Info = Duplicate_removal(a)
return b,c,sheet
def generate_ending():
return ENDING_TMPL
def generate_report(excel,sheet_name,project_Name):
b, c,sheet=getexceldata(excel,sheet_name)
if "FAIL" in c:
style="failClass"
else:style="passClass"
rows = REPORT_CLASS_TMPL % dict(
style=style,
desc=u"用例名称",
count=len(b) + len(c),
Pass=len(b),
fail=len(c),
cid='c1',
)
# print "cccccc=========="
# print rows
rows_list = []
print rows_list
for x in range(0,sheet.nrows-1):
cells = sheet.row_values(x+1)
tmp1=REPORT_TEST_WITH_OUTPUT_TMPL
if cells[11] !='pass':
class_name=''
class_style='failCase'
status_res='fail'
f_t="ft1."
else:
class_name='hiddenRow'
class_style='passCase'
status_res = 'pass'
f_t = "pt1."
# x+1表示序号,script运行过程及结果
xx_res=f_t+str(x+1)+u":ncase_id:"+str(cells[0])+
u"n用例名称:"+str(cells[1])+
u"n请求URL: "+str(cells[2])+
u"n请求方式:"+str(cells[4])+
u"n请求参数:"+str(cells[9])+
u"n返回报文:" + str(cells[11])
row = tmp1 % dict(tid=f_t+str(x+1),Class=class_name,style=class_style,desc=cells[1],script=saxutils.escape(xx_res),status=status_res)
# print row
rows_list.append(row)
# print rows_list
#excel.split(".xls")[0]
report = REPORT_TMPL % dict(
Project_Name=project_Name,
test_list=rows+''.join(rows_list),
count=str(len(b) + len(c)),
Pass=str(len(b)),
fail=str(len(c)),
)
# print report
pass_count = len(b)
fail_count = len(c)
return report,pass_count,fail_count
def c_htmlfile(result_name):
import io
f = io.open(path_res + '\' + 'html.html', "a",encoding='utf-8')
f.close()
import time
time_report = time.strftime('%Y%m%d%H%M%S', time.localtime())
os.rename(path_res + '\' + 'html.html',
path_res + '\' + result_name + '_' + time_report + '.html')
html_result = result_name + '_' + time_report + '.html'
return html_result
def write_html(excel,sheet_name,project_Name,title,duration):#excel名,表名,测试项目名,标题,持续时间
if True== os.path.exists(path_res + '\.html'):
os.remove(path_res + '\.html')
output = generateReport(excel,sheet_name,project_Name,title,duration)
html_path=c_htmlfile("report")
import io
f = io.open(path_res + '\'+html_path, "a",encoding='utf-8')
f.write(output)
f.close()
return html_path
write_html('interface.xls', u"测试详情","测试标题","接口自动化",'0.002')
file_all = os.listdir("D:/daima/ORSD/result")
path_res = "D:/daima/ORSD/result"
for a in file_all:
portion = os.path.splitext(a)
if portion[1] == ".html":
newname = portion[0] + ".html"
if newname != None:
print newname
os.remove(path_res + '/' + newname)
break
3.这里只保留最新生成的测试报告
# -*- coding:utf-8 -*-
import os
file_all = os.listdir("D:/daima/ORSD/result")
path_res = "D:/daima/ORSD/result"
days = set(fname[0:26] for fname in file_all)
# print days
for a in file_all:
portion = os.path.splitext(a)
if portion[1] == ".html":
newname = portion[0] + ".html"
if newname != None:
print newname
os.remove(path_res + '/' + newname)
break



