#导包
import xlsxwriter
from datetime import *
#创建报告类
class report:
workbook = xlsxwriter.Workbook('guaishounan_report.xlsx') #测试报告文件名称
worksheet = workbook.add_worksheet("测试总况") #表格名称
def get_format(self, wd, option):
return wd.add_format(option)
# 设置居中
def get_format_center(self, wb, num=1):
return wb.add_format({'align': 'center', 'valign': 'vcenter', 'border': num})
def set_border_(self, wb, num=1):
return wb.add_format({}).set_border(num)
# 写数据
def write_center(self, worksheet, cl, data, wb):
return worksheet.write(cl, data, self.get_format_center(wb))
def init(self, data, data1, score):
# 设置列行的宽高
self.worksheet.set_column("A:A", 15)
self.worksheet.set_column("B:B", 20)
self.worksheet.set_column("C:C", 20)
self.worksheet.set_column("D:D", 20)
self.worksheet.set_column("E:E", 20)
self.worksheet.set_column("F:F", 20)
self.worksheet.set_row(1, 30)
self.worksheet.set_row(2, 30)
self.worksheet.set_row(3, 30)
self.worksheet.set_row(4, 30)
self.worksheet.set_row(5, 30)
define_format_H1 = self.get_format(self.workbook, {'bold': True, 'font_size': 18})
define_format_H2 = self.get_format(self.workbook, {'bold': True, 'font_size': 14})
define_format_H1.set_border(1)
define_format_H2.set_border(1)
define_format_H1.set_align("center")
define_format_H2.set_align("center")
define_format_H2.set_bg_color("#70DB93")
define_format_H2.set_color("#ffffff")
self.worksheet.merge_range('A1:F1', 'guaishounan系统测试报告总概况', define_format_H1) #表格标题名称
self.worksheet.merge_range('A2:F2', '测试概括', define_format_H2)
self.worksheet.merge_range('A3:A6', '项目图片', self.get_format_center(self.workbook))
self.write_center(self.worksheet, "B3", '项目名称', self.workbook)
self.write_center(self.worksheet, "B4", '系统版本', self.workbook)
self.write_center(self.worksheet, "B5", '运行环境', self.workbook)
self.write_center(self.worksheet, "B6", '测试网络', self.workbook)
self.write_center(self.worksheet, "C3", data['test_name'], self.workbook)
self.write_center(self.worksheet, "C4", data['test_version'], self.workbook)
self.write_center(self.worksheet, "C5", data['test_pl'], self.workbook)
self.write_center(self.worksheet, "C6", data['test_net'], self.workbook)
self.write_center(self.worksheet, "D3", "用例总数", self.workbook)
self.write_center(self.worksheet, "D4", "通过总数", self.workbook)
self.write_center(self.worksheet, "D5", "失败总数", self.workbook)
self.write_center(self.worksheet, "D6", "测试日期", self.workbook)
self.write_center(self.worksheet, "E3", data1['test_sum'], self.workbook)
self.write_center(self.worksheet, "E4", data1['test_success'], self.workbook)
self.write_center(self.worksheet, "E5", data1['test_failed'], self.workbook)
self.write_center(self.worksheet, "E6", data1['test_date'], self.workbook)
self.write_center(self.worksheet, "F3", "分数", self.workbook)
self.worksheet.merge_range('F4:F6', '%s' % score, self.get_format_center(self.workbook))
self.pie(self.workbook, self.worksheet)
# 生成饼形图
def pie(self, wob, wos):
chart1 = wob.add_chart({'type': 'pie'})
chart1.add_series({
'categories': '=测试总况!$D$4:$D$5',
'values': '=测试总况!$E$4:$E$5',
})
chart1.set_title({'name': 'guaishounan系统测试统计'}) #饼状图标题
chart1.set_style(10)
wos.insert_chart('A9', chart1, {'x_offset': 25, 'y_offset': 10})
if __name__ == '__main__':
data_title = {"test_name": "guaishounan系统", "test_version": "v1.0", "test_pl": "win10", "test_net": "局域网"}
test_sum = 168 # 填写测试用例总数,除数不能为0
test_success = 88 #填写测试通过总数
test_failed = test_sum - test_success #测试失败总数
data_re = {"test_sum": test_sum, "test_success": test_success, "test_failed": test_failed,
"test_date": str(date.today())}
r = report()
r.init(data_title, data_re, int((test_success / test_sum) * 100))
r.workbook.close()