接口自动化测试(5)-生成excel测试报告

前面几篇都是个铺垫,直到这篇我还是感觉jmeter好用,包括第三篇准备的数据,可以用在jmeter参数化里面。这篇稍微能让人眼前一亮,自动出一个excel测试报告,比jmeter自己再去看测试结果稍微好点。代码我理解的也不是很透彻,包括课程的讲师感觉水平也一般,但是至少老师给指条路,还是得心怀感恩的。代码还是有点bug,但是我不会改,文章结尾再说一下问题。

先看一下生成的excel测试报告:

接口自动化测试(5)-生成excel测试报告_第1张图片

接口自动化测试(5)-生成excel测试报告_第2张图片

excel两个工作标签,具体内容都是代码控制的,包括测试总结那里“写啥都行,也可以不要这格”,原本放的是课程的机构名称,被我临时改成这个了,当然代码没实现自动换行,我特意把报告这里手动改成了自动换行。下面放代码,很长,需要耐心看。

import xlsxwriter
import TestRequest
from testvote import *
from testdata.getpath import GetTestReport
#调用第四篇写的
tianqi()
# 调用测试结果 
TestReport = TestRequest.hlist  
hpassnum = 0  # 定义一个变量,用来计算测试通过的用例数量
#
def get_format(wd, option={}):
    return wd.add_format(option)
# 设置居中
def get_format_center(wd, num=1):
    return wd.add_format({'align': 'center', 'valign': 'vcenter', 'border': num})
#设置边框
def set_border_(wd, num=1):
    return wd.add_format({}).set_border(num)
# 写数据
def _write_center(worksheet, cl, data, wd):
    return worksheet.write(cl, data, get_format_center(wd))
#
timenow=time.strftime("%Y/%m/%d %H:%M",time.localtime(time.time()))
ReportPath=GetTestReport()
workbook=xlsxwriter.Workbook(ReportPath)    #生成的报告路径
worksheet=workbook.add_worksheet("测试总结")#第一个工作标签名
worksheet2=workbook.add_worksheet("用例详情")#第二个工作标签名
#第一个标签页内容
def init(worksheet):
    # 设置列行的宽高
    worksheet.set_column("A:A", 15)#A列的宽
    worksheet.set_column("B:B", 20)#B列的宽
    worksheet.set_column("C:C", 20)
    worksheet.set_column("D:D", 20)
    worksheet.set_column("E:E", 20)
    worksheet.set_column("F:F", 20)
    worksheet.set_row(1, 30)#第一行的高
    worksheet.set_row(2, 30)
    worksheet.set_row(3, 30)
    worksheet.set_row(4, 30)
    worksheet.set_row(5, 30)
    #下面几行有点像方法,把单元格设置字体、居中、背景颜色、字体颜色,别处调用单元格样式就可以
    define_format_H1 = get_format(workbook, {'bold': True, 'font_size': 18})
    define_format_H2 = get_format(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("blue")
    define_format_H2.set_color("#ffffff")
    # 合并单元格
    worksheet.merge_range('A1:F1', '接口自动化测试报告', define_format_H1)
    worksheet.merge_range('A2:F2', '测试概括', define_format_H2)
    worksheet.merge_range('A3:A6', '写啥都行,也可以不要这格', get_format_center(workbook))
    # B列单元格
    _write_center(worksheet, "B3", '项目名称', workbook)
    _write_center(worksheet, "B4", '接口版本', workbook)
    _write_center(worksheet, "B5", '脚本语言', workbook)
    _write_center(worksheet, "B6", '测试地址', workbook)
#测试接口内容,C列单元格
    data = {"test_name": "京东万象接口", "test_version": "v1.0.0",
            "test_pl": "Python3", "test_net": testurl}
    _write_center(worksheet, "C3", data['test_name'], workbook)
    _write_center(worksheet, "C4", data['test_version'], workbook)
    _write_center(worksheet, "C5", data['test_pl'], workbook)
    _write_center(worksheet, "C6", data['test_net'], workbook)
#D列单元格
    _write_center(worksheet, "D3", "测试用例总数", workbook)
    _write_center(worksheet, "D4", "测试用例通过数", workbook)
    _write_center(worksheet, "D5", "测试用例失败数", workbook)
    _write_center(worksheet, "D6", "测试日期", workbook)
#E列单元格
    data1 = {"test_sum": len(TestReport),#总用例数上面调用测试结果那里获取
             "test_success": hpassnum,#成功用例数在下面test_detail中有计数
             "test_failed": len(TestReport) - hpassnum,
             "test_date": timenow}#时间
    _write_center(worksheet, "E3", data1['test_sum'], workbook)
    _write_center(worksheet, "E4", data1['test_success'], workbook)
    _write_center(worksheet, "E5", data1['test_failed'], workbook)
    _write_center(worksheet, "E6", data1['test_date'], workbook)
    _write_center(worksheet, "F3", "测试用例通过率", workbook)
#计算测试用例通过率,‘%’是字符,前面数字也要转化为字符型才能合并输出
    worksheet.merge_range('F4:F6', str(
        (round(hpassnum / len(TestReport), 4)) * 100) + '%', get_format_center(workbook))
#调用生成饼形图方法
    pie(workbook, worksheet)
#第二个标签页内容
#设置用例详情的格式
def test_detail(worksheet):
    # 设置列宽高
    worksheet.set_column("A:A", 15)
    worksheet.set_column("B:B", 20)
    worksheet.set_column("C:C", 20)
    worksheet.set_column("D:D", 20)
    worksheet.set_column("E:E", 20)
    worksheet.set_column("F:F", 20)
    worksheet.set_column("G:G", 20)
    worksheet.set_column("H:H", 20)
    # 设置行的宽高,根据用例总数+表头的两行,设置这么多行就够了
    for hrow in range(len(TestReport) + 2):
        worksheet.set_row(hrow, 20)
#第一行测试详情单元格设置
    worksheet.merge_range('A1:H1', '测试详情', get_format(workbook, {'bold': True,
                                                                 'font_size': 18,
                                                                 'align': 'center',
                                                                 'valign': 'vcenter',
                                                                 'bg_color': 'blue',
                                                                 'font_color': '#ffffff'}))
#表头设置
    _write_center(worksheet, "A2", '用例ID', workbook)
    _write_center(worksheet, "B2", '接口名称', workbook)
    _write_center(worksheet, "C2", '接口协议', workbook)
    _write_center(worksheet, "D2", 'URL', workbook)
    _write_center(worksheet, "E2", '参数', workbook)
    _write_center(worksheet, "F2", '预期值', workbook)
    _write_center(worksheet, "G2", '实际值', workbook)
    _write_center(worksheet, "H2", '测试结果', workbook)
# 获取测试结果被添加到测试报告里
    data = {"info": TestReport} 
#测试报告是从最后一行往上写的,从测试结果中取对应值,写进单元格。测试结果样式在TestRequest.py里定的
    temp = len(TestReport) + 2
    global hpassnum
    for item in data["info"]:
        if item["t_result"] == "通过":
            hpassnum += 1
        else:
            pass
        _write_center(worksheet, "A" + str(temp), item["t_id"], workbook)
        _write_center(worksheet, "B" + str(temp), item["t_name"], workbook)
        _write_center(worksheet, "C" + str(temp), item["t_method"], workbook)
        _write_center(worksheet, "D" + str(temp), item["t_url"], workbook)
        _write_center(worksheet, "E" + str(temp), item["t_param"], workbook)
        _write_center(worksheet, "F" + str(temp), item["t_hope"], workbook)
        _write_center(worksheet, "G" + str(temp), item["t_actual"], workbook)
        _write_center(worksheet, "H" + str(temp), item["t_result"], workbook)
        temp = temp - 1
# 生成饼形图,可以用代码指定excel能生成的所有图表
def pie(workbook, worksheet):
    chart1 = workbook.add_chart({'type': 'pie'})
    chart1.add_series({
        'name':       '接口测试统计',
#“测试总结”一定要与标签名一致,否则不能生成饼形图
        'categories': '=测试总结!$D$4:$D$5',
        'values':    '=测试总结!$E$4:$E$5',
#指定饼形图的两种颜色,两种方法,分别是16进制标识的绿色还有直接英文写的红色
        'points': [
        {'fill': {'color': '#00CD00'}},
        {'fill': {'color': 'red'}},
    ],
    })
    chart1.set_title({'name': '接口测试统计'})
    chart1.set_style(10)
    worksheet.insert_chart('A9', chart1, {'x_offset': 25, 'y_offset': 10})
test_detail(worksheet2)
init(worksheet)
workbook.close()

开始调用了前面写的多个模块,还有写excel的包,报告要哪些字段,都可以自己定义,一些讲解都在代码的注释里,如果我没写,可能就是我也不太理解。理论上这篇代码和前面几篇的代码,直接粘下来就可以直接用,当然命名需要和我写的命名一致,如果有报错,只能各位自己处理一下吧。

最后说一下这代码可能会出现的问题:正常情况下执行这段代码,会调用前面写的多个模块,出个测试报告。如果出现服务器宕机的情况,就不会出现测试报告了。因为宕机抛出的异常不是字典格式,代码完全提不出来东西,所以没有数据可往excel里写。如果有极端情况,把两个服务器的接口想一次执行出来,结果其中一个服务器宕机了,那么只看报告情况,可能会显示用例全部通过,实际是只有一个服务器接口的那部分用例通过。

你可能感兴趣的:(requests)