#!/usr/bin/env python
# -*- coding=utf-8 -*- __CreateAt__ = '2020/4/19-17:34' import shutil
from airtest.cli.runner import AirtestCase, run_script
from argparse import *
from air_case.report import report
import jinja2
import io
from util.send_email import SendEmail
from util.compress_file import copy_and_zip from util.android_util import attached_devices
from util.common import * PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(__file__), p)
) def run_case(data):
devices = attached_devices()
if not devices:
print("无可用设备")
return
test = CustomAirtestCase(data["root_path"])
device = ["Android://127.0.0.1:5037/%s?touch_method=ADBTOUCH" % data["dev"]]
# "android:///%s" % data["dev"]
test.run_air(device, data) def run(root_path, test_case, device, log_date):
script = os.path.join(root_path, test_case["module"], test_case["case"])
log = os.path.join(root_path, 'log', test_case["module"], log_date, test_case["case"].replace('.air', ''))
if os.path.isdir(log):
shutil.rmtree(log)
else:
os.makedirs(log)
print(str(log) + 'is created')
output_file = os.path.join(log, 'log.html')
args = Namespace(device=device, log=log, compress=None, recording=None, script=script, no_image=None)
try:
run_script(args, AirtestCase)
is_success = True
except:
is_success = False
return {"is_success": is_success, "output_file": output_file, "script": script, "log": log} class CustomAirtestCase(AirtestCase):
@classmethod
def setUpClass(cls):
super(CustomAirtestCase, cls).setUpClass() def __init__(self, root_dir):
self.fail_data = []
self.results = {"dev": "", "modules": [], "total_time": "", "data": []}
self.log_list = []
super().__init__() def setUp(self):
super(CustomAirtestCase, self).setUp() def tearDown(self):
print("custom tearDown")
super(CustomAirtestCase, self).setUp() def run_air(self, device, data):
root_log = os.path.join(data["root_path"], "log")
# remove_log表示如果传值就会删除整个log文件夹,删除后无法查看历史报告
if os.path.isdir(root_log):
if data.get("remove_log"):
shutil.rmtree(root_log, ignore_errors=True)
print("删除log文件夹")
else:
os.makedirs(root_log)
get_data_list = get_test_case(data)
if not get_data_list:
print("无可用用例")
return
# 整个用例开始执行时间
start_time = datetime.now().strftime("%H:%M:%S")
modules = []
# 日志按照日期格式生成
log_date = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
for j in get_data_list:
# 日志按照日期格式生成
# log_date = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
# 用例开始执行日期
st_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# 用例开始执行时间
s_time = datetime.now().strftime("%H:%M:%S")
# 循环运行用例
get_run = run(data["root_path"], j, device, log_date)
# 用例结束执行日期
end_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# 用例结束执行时间
e_time = datetime.now().strftime("%H:%M:%S")
# 用例耗时时间
sum_time = get_case_total_time(s_time, e_time)
# 生成测试用例的详情报告
rpt = report.LogToHtml(get_run["script"], get_run["log"], "../../../../report")
# rpt = report.LogToHtml(get_run["script"], get_run["log"])
rpt.report("log_template.html", output_file=get_run["output_file"])
# 记录测试结果
result = {"name": j["case"].replace(".air", ""), "result": get_run["is_success"], "start_date": st_date,
"end_date": end_date, "sum_time": sum_time, "module": j["module"], "log_date": log_date}
modules.append(j["module"])
self.results["data"].append(result)
self.log_list.append(get_run["log"])
# 记录失败用例
if not get_run["is_success"]:
self.fail_data.append({"module": j["module"], "case": j["case"]})
# 整个用例结束执行时间
end_time = datetime.now().strftime("%H:%M:%S")
# 以小时,分钟,秒钟的方式记录所有用例耗时时间
total_time = get_case_total_time(start_time, end_time)
self.results["total_time"] = total_time
# 记录测试模块
self.results["modules"] = get_test_modules(modules)
# 记录设备名字
self.results["phone"] = data["phone"]
# 打印失败用例
if self.fail_data:
print("存在失败用例")
print(self.fail_data) # 生成测试报告
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(data["root_path"]),
extensions=(),
autoescape=True
)
print("用例结果为:%s" % self.results)
t = os.path.join(data["root_path"], "summary_template.html")
if not os.path.exists(t):
shutil.copy(PATH("util/summary_template.html"), data["root_path"])
template = env.get_template("summary_template.html", data["root_path"])
html = template.render({"results": self.results})
output_file = os.path.join(data["root_path"], "summary_%s.html" % datetime.now().strftime("%Y-%m-%d-%H-%M-%S"))
with io.open(output_file, 'w', encoding="utf-8") as f:
f.write(html)
# 按日期生成测试报告,方便对比历史报告,但是程序入口字段需要设置为"remove_log": False
print("测试报告为:%s" % output_file) # 固定输出给CI
output_file = os.path.join(data["root_path"], "summary.html")
with io.open(output_file, 'w', encoding="utf-8") as f:
f.write(html)
# 当发送邮件参数为真,就对文件进行压缩并发送测试报告到指定邮箱
if data.get("send_email"):
# 压缩测试报告
report_file = os.path.join(data["root_path"], "report")
case_log = os.path.join(data["root_path"], "log")
case_html = output_file
zip_list = [report_file, case_log, case_html]
zip_path = copy_and_zip(zip_list, "report")
# 发送测试报告邮件
to_addr = ["284772894@qq.com"]
SendEmail.send(zip_path, to_addr) if __name__ == '__main__':
"""
python E:/project/trade-auto/runner1.py
"""
root_path = PATH("air_case")
# test_plan=1 表示调试用例需要配合test_module使用,0表示全部用例,比如:"test_plan": 0, "test_module": ["我的"]
# remove_log为真 表示是否运行用例之前,删除log文件夹,删除后会影响查看历史报告
# send_email为真,表示发送邮件,否则就不发送
# dev参数使用adb devices 获取到设备udid,填进去,若想多台并行,这里的dev必须填正确,不能是同一台手机
# data = {"root_path": root_path, "test_plan": 0, "test_module": [], "dev": "TPG5T18130013404", "phone": "Nova2s"}
data = {"root_path": root_path, "remove_log": True, "send_email": True,
"test_plan": 0, "test_module": [], "dev": "TPG5T18130013404", "phone": "Nova2s"}
run_case(data)

aritest发送测试报告到邮件的更多相关文章

  1. Java+Selenium3框架设计篇5-如何实现邮件发送测试报告

    https://blog.csdn.net/u011541946/article/details/77278837 本篇继续回答网友的问题,这个主题是如何通过邮件发送测试报告.通过邮件发送测试报告,这 ...

  2. python3 邮件方式发送测试报告

    以邮件方式发送测试报告 import smtplib from email.mime.text import MIMEText class SendEmail: """邮 ...

  3. Python+unittest发送测试报告

    案例:将E:\Python_script\unittest\Test_Baidu生成的最新测试报告发送到指定邮箱. 我们将之前的unittest的报告生成和Python自动发送邮件结合在一起,就可以完 ...

  4. python接口自动化(三十)--html测试报告通过邮件发出去——中(详解)

    简介 上一篇,我们虽然已经将生成的最新的测试报告发出去了,但是MIMEText 只能发送正文,无法带附件,因此我还需要继续改造我们的代码,实现可以发送带有附件的邮件.发送带附件的需要导入另外一个模块 ...

  5. python接口自动化(二十九)--html测试报告通过邮件发出去——上(详解)

    简介 前边几篇,已经教小伙伴们掌握了如何生成HTML的测试报告,那么生成测试报告,我们也不能放在那里不管了,这样即使你报告在漂亮,领导也看不到.因此如果想向领导汇报工作,不仅需要提供更直观的测试报告. ...

  6. Jenkins发送测试报告

    邮件全局配置 邮件插件:Email Extension Plugin 功能:发送邮件 邮件全局配置:jenkins--系统管理--系统配置:截图: 配置说明: 系统管理员邮件地址:必须配置,配置后邮件 ...

  7. ORACLE发送带附件邮件的二三事之一

    在oracle使用过程中,我们可以通过pl/sql生成数据文件,也可以通过spool on spool off生成,但某些环境下,我们需要通过存储过程处理数据,数据处理完,需要自动生成数据文件,手工导 ...

  8. linux 安装wordpress 无故往外发送大量垃圾邮件

    linux 安装wordpress 无故往外发送大量垃圾邮件 始末 表现出来的现象就是, 网站运行没多久,mysql服务就挂了,重启也无法启动起来,提示 No such file or dicrion ...

  9. EBS中使用JAVA方式发送HTML格式邮件

    转自huan.gu专栏:http://blog.csdn.net/gh320/article/details/17174769 EBS中使用JAVA方式发送HTML格式邮件 一.开发工具:JDevel ...

随机推荐

  1. noip模拟42

    A. 卷 发现乘积足以爆 \(long\) \(long\),但是数据随机,可以略忽略精度问题 一个快速降低数的级别的方法是取对数,由于有性质 \(log(x * y)=logx+logy\),合并时 ...

  2. vscode安装go插件失败

    解决办法:使用golang代理,在环境变量中添加两个新变量: 详情参考vscode中为golang开发环境配置代理goproxy 之后便有一部分可以安装成功

  3. SQL-INSERT触发器练习

    &练习一 有这样的一个基础表A,字段包括:id.type.value.create_time,主要是记录某个类型的状态变化时间和值.在插入类型(type)为'runtime' 的数据时,根据前 ...

  4. Git:git commit时退出报错解决(Error45、Error325)

    Git 报错 在输入git commit编辑注释日志时强制退出git程式,文件会变成只读文件,于是出现下述报错: 解决方法(ERROR45) 我们提交代码的正常操作流程一般是: 输入git commi ...

  5. angularjs实现购物清单

    HTML: 1:要定义ng-app,在html上定义ng-app="App"; 2:在body上定义ng-controller="ToDoCtrl" 3: &l ...

  6. iNeuOS工业互联平台,PLC监测与控制应用过程案例。新闻:.NET 6 RC1 正式发布

    目       录 1.      概述... 1 2.      平台演示... 2 3.      应用过程... 2 1.   概述 iNeuOS工业互联网操作系统主要使用.netcore 3. ...

  7. 离散化模板题 II ——重复元素离散化后的数字不相同

    离散化模板题 II --重复元素离散化后的数字不相同 题目描述 现有数列A1, A2, ⋯, An,数列中可能有重复元素. 现在要求输出该数列的离散化数列,重复元素离散化后的数字不相同. 输入 第一行 ...

  8. CodeForce-813B The Golden Age(数学+枚举)

    The Golden Age CodeForces - 813B 题目大意:如果一个数t=x^a+y^b(a,b都是大于等于0的整数)那就是一个unlucky数字.给你x,y,l,r(2 ≤ x, y ...

  9. 在树莓派用C#+Winform实现传感器监测

    最近学校里发了个任务,说要做一个科技节小发明,然后我就掏出我的树莓派准备大干一场. 调料 Raspberry Pi 3B+ 树莓派GPIO扩展板 3.5寸电容触摸屏(GPIO接口) 土壤湿度传感器(G ...

  10. JDBC-1(概述&建立)

    基于宋红康老师所讲JDBC所作笔记 1.JDBC概述 1.1 数据持久化 持久化:将数据保持到可掉电式存储设备中以供之后使用. 数据持久化意味着将内存中的数据保存到硬盘上加以固化,实现过程大多通过各种 ...