#!/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. Spring系列之Mybatis动态代理实现全过程?回答正确率不到1%

    面试中,可能会问到Spring怎么绑定Mapper接口和SQL语句的.一般的答案是Spring会为Mapper生成一个代理类,调用的时候实际调用的是代理类的实现.但是如果被追问代理类实现的细节,很多同 ...

  2. JS020. Array map()函数查到需要的元素时跳出遍历循环,不再执行到数组边界

    Array.prototype.map() map( )  方法创建一个 新数组 *,其结果是该数组中的每个元素是调用一次提供的 函数后的返回值 *.[ MDN / RUNOOB ] * map 添加 ...

  3. 真香!原来 CLI 开发可以这么简单

    CLI(命令行工具,Command Line Interface)大家都非常熟悉了,比如 create-react-app 等.我们今天介绍一个 CLI 工具的开发框架,可以帮助我们快速构建 CLI ...

  4. 【Azure API 管理】APIM 配置Validate-JWT策略,验证RS256非对称(公钥/私钥)加密的Token

    问题描述 在APIM中配置对传入的Token进行预验证,确保传入后端被保护的API的Authorization信息正确有效,可以使用validate-jwt策略.validate-jwt 策略强制要求 ...

  5. PHP的可变变量与可变函数

    什么叫可变.在程序世界中,可变的当然是变量.常量在定义之后都是不可变的,在程序执行过程中,这个常量都是不能修改的.但是变量却不同,它们可以修改.那么可变变量和可变函数又是什么意思呢?很明显,就是用另一 ...

  6. php curl下载文件由于空格导致下载文件失败

    <?php //$result=httpcopy('http://www.phpernote.com/image/logo.gif'); echo '<pre>';print_r($ ...

  7. dede5.7 标题长度限制修改

    我们经常碰到dede标题长度不够用的问题20个字的标题有时候是真的有点短了网上也有些修改长度问题的帖子,但我发现都不完整所以写下来供大家参考下.免得浪费时间 第一步: 修改下面4处文件: dede目录 ...

  8. Dapr实战(二) 服务调用

    服务调用是什么 在分布式应用程序中的服务之间进行调用会涉及到许多挑战. 例如: 维护其他服务的地址. 如何安全地调用服务. 在发生短暂的 暂时性错误 时如何处理重试. 分布式应用程序调用链路追踪. 服 ...

  9. 再谈OAuth授权

    工作场景流程 大家都知道OAuth是用于第三方授权的,当你用其他的APP想访问微信账号的昵称.粉丝.联系人等信息,这里就需要微信进行授权,此时在APP的网页端是可以发现有微信登录的,点开会出现弹框,在 ...

  10. web、app、小程序测试异同点

    http://www.spasvo.com/Company/news_show.asp?id=702 https://blog.csdn.net/weixin_43489515/article/det ...