python自动化测试报告(excel篇)

转:https://www.jianshu.com/p/6c5d328bf390

# -*- coding: utf-8 -*-
import xlsxwriter

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))
workbook = xlsxwriter.Workbook(‘report.xlsx‘)
worksheet = workbook.add_worksheet("测试总况")
worksheet2 = workbook.add_worksheet("测试详情")

def init(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_row(1, 30)
    worksheet.set_row(2, 30)
    worksheet.set_row(3, 30)
    worksheet.set_row(4, 30)
    worksheet.set_row(5, 30)

    # worksheet.set_row(0, 200)

    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")
    # Create a new Chart object.

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

    _write_center(worksheet, "B3", ‘项目名称‘, workbook)
    _write_center(worksheet, "B4", ‘接口版本‘, workbook)
    _write_center(worksheet, "B5", ‘脚本语言‘, workbook)
    _write_center(worksheet, "B6", ‘测试网络‘, workbook)

    data = {"test_name": "智商", "test_version": "v2.0.8", "test_pl": "android", "test_net": "wifi"}
    _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)

    _write_center(worksheet, "D3", "接口总数", workbook)
    _write_center(worksheet, "D4", "通过总数", workbook)
    _write_center(worksheet, "D5", "失败总数", workbook)
    _write_center(worksheet, "D6", "测试日期", workbook)

    data1 = {"test_sum": 100, "test_success": 80, "test_failed": 20, "test_date": "2018-10-10 12:10"}
    _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‘, ‘60‘, get_format_center(workbook))

    pie(workbook, worksheet)

 # 生成饼形图
def pie(workbook, worksheet):
    chart1 = workbook.add_chart({‘type‘: ‘pie‘})
    chart1.add_series({
    ‘name‘:       ‘接口测试统计‘,
    ‘categories‘:‘=测试总况!$D$4:$D$5‘,
   ‘values‘:    ‘=测试总况!$E$4:$E$5‘,
    })
    chart1.set_title({‘name‘: ‘接口测试统计‘})
    chart1.set_style(10)
    worksheet.insert_chart(‘A9‘, chart1, {‘x_offset‘: 25, ‘y_offset‘: 10})

def test_detail(worksheet):

    # 设置列行的宽高
    worksheet.set_column("A:A", 30)
    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)

    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)
    worksheet.set_row(6, 30)
    worksheet.set_row(7, 30)

    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": [{"t_id": "1001", "t_name": "登陆", "t_method": "post", "t_url": "http://XXX?login", "t_param": "{user_name:test,pwd:111111}",
                      "t_hope": "{code:1,msg:登陆成功}", "t_actual": "{code:0,msg:密码错误}", "t_result": "失败"}, {"t_id": "1002", "t_name": "商品列表", "t_method": "get", "t_url": "http://XXX?getFoodList", "t_param": "{}",
                      "t_hope": "{code:1,msg:成功,info:[{name:123,detal:dfadfa,img:product/1.png},{name:456,detal:dfadfa,img:product/1.png}]}", "t_actual": "{code:1,msg:成功,info:[{name:123,detal:dfadfa,img:product/1.png},{name:456,detal:dfadfa,img:product/1.png}]}", "t_result": "成功"}],
            "test_sum": 100,"test_success": 20, "test_failed": 80}
    temp = 4
    for item in data["info"]:
        _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

init(worksheet)
test_detail(worksheet2)

workbook.close()

  

原文地址:https://www.cnblogs.com/zipon/p/9059195.html

时间: 2024-10-12 16:40:51

python自动化测试报告(excel篇)的相关文章

Python自动化整理Excel 表格

相信有不少朋友日常工作会用到 Excel 处理各式表格文件,更有甚者可能要花大把时间来做繁琐耗时的表格整理工作.最近有朋友问可否编程来减轻表格整理工作量,今儿我们就通过实例来实现 Python 对表格的自动化整理. 首先我们有这么一份数据表 source.csv: 我们要做的是从上表中提取数据,来生成一份符合以下要求的表格: 按照以下分组名单 group.xls 来整理数据表中的数据: 最终要展现的数据项: 其中"K数据/60"为数据表中的"数据K"/60后保留的2

Python自动化 【第一篇】:Python简介和入门

Python简介: 一.什么是python Python是一门动态解释性的强类型定义语言. pythonde 特点:“优雅”.“明确”.“简单”. 二.Python由来 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承. 三.目前Python主要应用领域 云计算: 云计算最火的语言, 典型应用OpenStack WEB开发: 众多优秀的WEB框架,众多大

【python自动化第十篇:】

复习: 线程与进程的区别: 进程:资源的集合 线程:最小调度单位 进程至少包含一个线程 线程之间的内存是共享的,两个线程操作同一个数据就会修改整个结果(需要mutex加锁来保持数据的一致性),递归锁,join(等待) 信号量:也相当于是lock 守护线程:服务于非守护线程: quene:程序的解耦:提高效率:也是有序的容器:队列只有一份数据,取完了就没有了 先进先出(FIFO) 后进先出(LIFO) 生产者消费者模型:也就是为了实现解耦 event:事件---红绿灯实验 i/o不占用cpu,计算

Python自动化 【第二篇】:Python基础-列表、元组、字典

本节内容 模块初识 .pyc简介 数据类型初识 数据运算 列表.元组操作 字符串操作 字典操作 集合操作 字符编码与转码 一.模块初识 Python的强大之处在于他有非常丰富和强大的标准库和第三方库,几乎你想实现的任何功能都有相应的Python库支持,让我们先来象征性的学2个简单的. sys 1 #!/usr/bin/env python 2 3 # -*- coding: utf-8 -*- 4 5 6 7 import sys 8 9 10 11 print(sys.argv) 12 13

python自动化之excel

import openpyxl wb=openpyxl.load_workbook(r'C:\Users\Administrator\Desktop\sl.xlsx') type(wb) wb.get_sheet_names() sheet=wb.get_sheet_by_name('o') type(sheet) sheet.title anotherSheet=wb.get_active_sheet()  #######得到活动页sheet anotherShee #############

【python自动化第六篇:面向对象】

知识点概览: 面向对象的介绍 面向对象的特性(class,object)实例变量,类变量 面型对象编程的介绍 私有方法,私有属性 一.面向对象介绍 编程范式:面向对象,面向过程,函数式编程 面向过程:通过一组指令告诉机器去一步一做什么:一个过程包含一组要被进行计算的步骤,从头到尾的执行顺序:程序的修改要注意各个部分的依赖关系,否则会影响其他过程的运行,发生一连串的影响,维护困难. 面向对象:OOP编程是利用类和对象来创建各种模型实现对真实世界的描述:使用面向对象编程的原因是一方面它会使得程序的维

python自动化

[python自动化第一篇:python介绍与入门] [python自动化第二篇:python入门] [python自动化第三篇:python入门进阶] 原文地址:https://www.cnblogs.com/hcxy2007107708/p/10066532.html

python自动化办公?学这些就够用了

知乎上有人提问:用python进行办公自动化都需要学习什么知识呢? 这可能是很多非IT职场人士面临的困惑,想把python用到工作中,却不知如何下手? python在自动化办公领域越来越受欢迎,批量处理简直是加班族的福音. 自动化办公无非是excel.ppt.word.邮件.文件处理.数据分析处理.爬虫这些,这次我就来理一理python自动化办公的那些知识点. python基础 excel自动化 ppt自动化 word自动化 邮件处理 文件批量处理 数据处理与分析 自动化爬虫 下面一一详解. p

Selenium2+python自动化54-unittest生成测试报告(HTMLTestRunner)

前言 批量执行完用例后,生成的测试报告是文本形式的,不够直观,为了更好的展示测试报告,最好是生成HTML格式的. unittest里面是不能生成html格式报告的,需要导入一个第三方的模块:HTMLTestRunner 一.导入HTMLTestRunner 1.这个模块下载不能通过pip安装了,只能下载后手动导入,下载地址:http://tungwaiyip.info/software/HTMLTestRunner.html 2.Download下HTMLTestRunner.py文件就是我们需