PyTestReport使用
PyTestReport详细介绍:
https://testerhome.com/opensource_projects/78
示例代码
#coding:utf-8
import os,unittest,time,HTMLTestRunner,smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from config import readConfig
from pytestreport import TestRunner
# import HTMLTestRunner_jpg # 当前脚本所在文件真实路径
cur_path = os.path.dirname(os.path.realpath(__file__))
print(cur_path)
# print(type(cur_path)) # 第一步:用discover方法加载所有的测试用例
def add_case(caseName="case",rule="test*.py"):
'''第一步:加载所有的测试用例'''
case_path = os.path.join(cur_path,caseName) #用例文件夹
# 如果不存在这个case文件夹,就自动创建一个
if not os.path.exists(case_path):os.mkdir(case_path)
print("\033[31mtest case path:\033[0m%s"%case_path) #定义discover方法的参数
discover = unittest.defaultTestLoader.discover(case_path,
pattern=rule,
top_level_dir=None) print(discover)
return discover # 第二步:生成HTML报告
def run_case(all_case,reportName="report"):
'''第二步:执行所有的用例,并把结果写入HTML测试报告'''
now = time.strftime("%Y_%m_%d_%H_%M_%S")
report_path = os.path.join(cur_path,reportName) # 测试报告文件夹 # 如果不存在这个report文件夹,就自动创建一个
if not os.path.exists(report_path):os.mkdir(report_path)
# report_abspath = os.path.join(report_path,now+"result.html") # 配合jenkins生成的测试报告名称,固定为result.html
report_abspath = os.path.join(report_path, "result.html") print("\033[31mreport path:\033[0m%s"%report_abspath) fp = open(report_abspath,"wb") # PyTestReport测试报告
with open(report_abspath,'wb') as fp:
runner = TestRunner(fp,
title='测试标题',
description='测试描述',
verbosity=2
)
# 调用add函数返回值
runner.run(all_case) # 第三步:获取最新的测试报告
def get_report_file(report_path):
'''第三步:获取最新的测试报告'''
lists = os.listdir(report_path)
lists.sort(key=lambda fn: os.path.getmtime(os.path.join(report_path,fn)))
print(u'最新测试生成的报告:'+lists[-1]) # 找到最新生成的测试报告文件
report_file = os.path.join(report_path,lists[-1])
return report_file # 第四步:发送测试报告到邮箱
def send_mail(sender,psw,receiver,smtpserver,report_file,port):
'''第四步:发送最新的测试报告内容'''
with open(report_file,"rb") as f:
mail_body = f.read() # 定义邮件内容
msg = MIMEMultipart()
body = MIMEText(mail_body,_subtype='html',_charset='utf-8')
msg['Subject'] = '自动化测试报告'
msg["from"] = sender
msg["to"] = receiver
msg.attach(body) # 添加附件
att = MIMEText(open(report_file,"rb").read(),"base64","utf-8")
att["Content-Type"] = "application/octet-stream"
att["Content-Disposition"] = 'attachment; filename="report.html"'
msg.attach(att) try:
smtp = smtplib.SMTP_SSL(smtpserver,port)
except:
smtp = smtplib.SMTP()
smtp.connect(smtpserver,port) # 用户名密码
smtp.login(sender,psw)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()
print('test report email has send out !') if __name__ == "__main__":
all_case = add_case() # 1加载用例
# 生成测试报告的路径
run_case(all_case) # 2执行用例
# 获取最新的测试报告文件
report_path = os.path.join(cur_path,"report") # 用例文件夹
report_file = get_report_file(report_path) # 3获取最新的测试报告 #邮箱配置
sender = readConfig.sender
psw = readConfig.psw
smtp_server = readConfig.smtp_server
port = readConfig.port
receiver = readConfig.receiver
# send_mail(sender,psw,receiver,smtp_server,report_file,port) # 4最后一步发送报告
生成的测试报告
PyTestReport使用的更多相关文章
- PyTestReport 自动化报告
安装 pip install PyTestReport pytest框架执行命令 pytest.main(["-s", "test_login.py", &qu ...
- 纯python自研接口自动化脚本更新版本,让小白也能实现0到1万+的接口自动化用例
查看完整文章点击原文链接:纯python自研接口自动化脚本更新版本,让小白也能实现0到1万+的接口自动化用例 你是否还在用postman\jmeter做接口自动化吗?用python的开源框架[unit ...
- pytest文档43-元数据使用(pytest-metadata)
前言 什么是元数据?元数据是关于数据的描述,存储着关于数据的信息,为人们更方便地检索信息提供了帮助. pytest 框架里面的元数据可以使用 pytest-metadata 插件实现.文档地址http ...
随机推荐
- IDEA+java通过SSH来进行分析日志,实现UI自动化动态验证码登录
在我写自动化脚本的时候是要真实发送验证码才能往下进行UI自动化 思路:验证码会显示在哪些地方,手机短信?数据库存储?日志? 完整代码如下: package guanyu.tools; import c ...
- Linux基础:Linux环境下安装JDK
title: Linux基础:JDK的及环境变量配置 author: Enjoyitlife.top date: 2019-10-09 20:50:36 tags: Linux JDK categor ...
- win10创建扩展分区
1.开始菜单中选择命令提示符,以管理员身份运行. 2.运行“diskpart”命令. 3.DISKPART>后面输入list disk命令,显示磁盘列表. 4.选择磁盘,select disk ...
- spring data solr 搜索关键字高亮显示
spring data solr 搜索关键字高亮显示 public Map<String, Object> highSearch(Map searchMap) { Map map = ne ...
- Java中的容器(集合)之ArrayList源码解析
1.ArrayList源码解析 源码解析: 如下源码来自JDK8(如需查看ArrayList扩容源码解析请跳转至<Java中的容器(集合)>第十条):. package java.util ...
- [BZOJ3203] [SDOI2013]保护出题人(二分+凸包)
[BZOJ3203] [SDOI2013]保护出题人(二分+凸包) 题面 题面较长,略 分析 对于第i关,我们算出能够打死前k个个僵尸的最小能力值,再取最大值就可以得到\(y_i\). 前j-1个僵尸 ...
- BZOJ 4919 (树上LIS+启发式合并)
题面 给定一棵n个节点的有根树,编号依次为1到n,其中1号点为根节点.每个点有一个权值v_i. 你需要将这棵树转化成一个大根堆.确切地说,你需要选择尽可能多的节点,满足大根堆的性质:对于任意两个点i, ...
- 洛谷P2460 [SDOI2007]科比的比赛(题解)(贪心+搜索)
科比的比赛(题解)(贪心+搜索) 标签:算法--贪心 阅读体验:https://zybuluo.com/Junlier/note/1301158 贪心+搜索 洛谷题目:P2460 [SDOI2007] ...
- 2019 Multi-University Training Contest 3 - 1006 - Fansblog - 打表 - 暴力
http://acm.hdu.edu.cn/showproblem.php?pid=6608 题意:给一个比较大的质数P(1e14以内),求比它小的最大的质数Q(貌似保证存在的样子,反正我没判不存在) ...
- Tomcat 保存镜像实战操作( 目录挂载方法 )
查看数据保存的位置 docker inspect --format='{{.Mounts}}' mxg_tomcat 宿主机数据保存在 /usr/local/project , 将此路径数据备份在 b ...