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()
python自动化测试报告(excel篇)的更多相关文章
- 【python自动化第十一篇】
[python自动化第十一篇:] 课程简介 gevent协程 select/poll/epoll/异步IO/事件驱动 RabbitMQ队列 上节课回顾 进程: 进程的诞生时为了处理多任务,资源的隔离, ...
- python自动化运维篇
1-1 Python运维-课程简介及基础 1-2 Python运维-自动化运维脚本编写 2-1 Python自动化运维-Ansible教程-Ansible介绍 2-2 Python自动化运维-Ansi ...
- Python“文件操作”Excel篇(上)
大家好,我们今天来一起探索一下用Python怎么操作Excel文件.与word文件的操作库python-docx类似,Python也有专门的库为Excel文件的操作提供支持,这些库包括xlrd.xlw ...
- Python 自动化测试报告HTMLTestRunner
HTMLTestRunner 是Python的一个测试报告 参考网址: http://tungwaiyip.info/software/HTMLTestRunner.html https://pypi ...
- Python自动化 【第九篇】:Python基础-线程、进程及python GIL全局解释器锁
本节内容: 进程与线程区别 线程 a) 语法 b) join c) 线程锁之Lock\Rlock\信号量 d) 将线程变为守护进程 e) Event事件 f) queue队列 g) 生 ...
- Python自动化 【第二篇】:Python基础-列表、元组、字典
本节内容 模块初识 .pyc简介 数据类型初识 数据运算 列表.元组操作 字符串操作 字典操作 集合操作 字符编码与转码 一.模块初识 Python的强大之处在于他有非常丰富和强大的标准库和第三方库, ...
- Python自动化 【第一篇】:Python简介和入门
Python简介: 一.什么是python Python是一门动态解释性的强类型定义语言. pythonde 特点:“优雅”.“明确”.“简单”. 二.Python由来 python的创始人为吉多·范 ...
- 【python自动化第十篇:】
复习: 线程与进程的区别: 进程:资源的集合 线程:最小调度单位 进程至少包含一个线程 线程之间的内存是共享的,两个线程操作同一个数据就会修改整个结果(需要mutex加锁来保持数据的一致性),递归锁, ...
- 【python自动化第八篇:网络编程】
一.拾遗 动态导入模块 目的是为了在导入模块的过程中将模块以字符的格式导入. #!/usr/bin/env python # -*- coding:utf-8 -*- #Author:wanghui ...
随机推荐
- Acronis.Disk.Director磁盘分区管理
Acronis.Disk.Director分为for 专业版和服务器版的,我在生产环境中调整Windows2003跳板机使用的是Acronis.Disk.Director Server 10.0.20 ...
- VC下的C语言程序随机数的产生
本文章适用于VC编译器,VC编译器里有个rand()函数,我们用它来实现取随机数. #include <stdio.h> #include<stdlib.h> //随机数的头文 ...
- Codevs1080 线段树练习
题目描述 Description 一行N个方格,开始每个格子里都有一个整数.现在动态地提出一些问题和修改:提问的形式是求某一个特定的子区间[a,b]中所有元素的和:修改的规则是指定某一个格子x,加上或 ...
- 数据结构实用C语言基础
大纲: 主要介绍了C语言中的指针,内存分配,两种传参方式,typedef的简单用法 关于C语言中的指针: 指针变量也称为指针(Pointer) 例如:int* p; 则p为一个指向int类型的指针. ...
- Qt的由来和发展
一.Qt的由来 Haavard Nord 和Eirik Chambe-Eng于1991年开始开发"Qt",1994年3月4日创立公司,早名为Quasar Technologies, ...
- python学习之判断和循环的使用
作为一个小白运维,工作中常常发现很多东西还是自动化的好一点,所以就想到的用python来编写脚本.当然,我肯定是不会的啦,哈哈哈~~~~所以啦,身为一个懒癌晚期的上班族不得不在闲余时间来好好学学pyt ...
- Goroutine 中执行匿名函数 坑
//相对应for 循环 goroutine跑到慢 所以这里很大概率只会打印最后一条数据 func goRun() { values := []int{1, 2, 3} for _, v := rang ...
- 20181206(re,正则表达式,哈希)
1.re&正则表达式 2.hashlib 一:re模块&正则表达式 正则:正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描 ...
- Python语言程序设计之一--for循环中累加变量是否要清零
最近学到了Pyhton中循环这一章.之前也断断续续学过,但都只是到了函数这一章就停下来了,写过的代码虽然保存了下来,但是当时的思路和总结都没有记录下来,很可惜.这次我开通了博客,就是要把这些珍贵的学习 ...
- 笔记-python-centos环境下安装配置
笔记-python-centos环境下安装配置 1. 准备工作 环境准备 centos6.5 mini,已有python 2.6.6 下载源码包 Python官网下载Gzipped sour ...