1 Star 2 Fork 2

ZHAIM/Airtest-Runner-single

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
runCase.py 5.15 KB
一键复制 编辑 原始数据 按行查看 历史
ZHAIM 提交于 2022-02-10 22:14 . V1.2
#!/usr/python3
import logging
import shutil,jinja2,datetime
from argparse import *
from conf.settings import *
from airtest.report.report import LogToHtml
from airtest.core.api import connect_device
from airtest.cli.runner import AirtestCase,run_script
# 只在终端输出error级别的及以上的log
logger = logging.getLogger()
logger.setLevel(logging.ERROR)
class Air_Case_Handler(AirtestCase):
def __init__(self,dev_id):
super(Air_Case_Handler, self).__init__()
if deviceType.upper() == "WEB":
pass
else:
# 如果deviceType非“Web”,说明需要连接手机,则调用airtest的connect_device方法
self.dev = connect_device(dev_id)
def setUp(self):
super(Air_Case_Handler, self).setUp()
def tearDown(self):
super(Air_Case_Handler,self).tearDown()
def run_air(self,air_dir,device):
start_time = datetime.datetime.now()
start_time_fmt = start_time.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
results = [] # {name:项目名,result:运行结果True/False}
root_log = log_path
# 如果没有log和report目录则创建,有则删除后再创建
if os.path.exists(root_log):
if cover_report:
shutil.rmtree(root_log)
os.makedirs(root_log)
else:
os.makedirs(root_log)
if os.path.exists(report_path):
if cover_report:
shutil.rmtree(report_path)
os.makedirs(report_path)
else:
os.makedirs(report_path)
for file in os.listdir(air_path):
# 找到air目录下所有后缀是.air的脚本
if file.endswith(".air"):
airName = file
# 把文件路径的后缀删掉,就变成了文件路径了。后面用来做脚本同名的log日志文件夹
airDirName = file.replace(".air","") #/Users/zhaohaiming/Desktop/TestDev/Airtest_Runner/air/air_script
script = os.path.join(air_dir,file) #/Users/zhaohaiming/Desktop/TestDev/Airtest_Runner/air/air_script.air
# log的路径
air_log = os.path.join(root_path,"log/" + airDirName + str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))#/Users/zhaohaiming/Desktop/TestDev/Airtest_Runner/log/飞凡APP登录2022-02-05 21:51:10.002758
# 创建log的目录
os.makedirs(air_log)
html = os.path.join(air_log,"log.html") #/Users/zhaohaiming/Desktop/TestDev/Airtest_Runner/log/air_script/log.html
if deviceType.upper() == "WEB":
# 直接向argparse的namespace添加命令行参数
args = Namespace(device=None,log = air_log, recording=None, script=script,compress=compress,no_image=no_image,language="zh")
elif deviceType.upper() == "APP":
args = Namespace(device=device, log = air_log, recording=None, script=script,language="zh",compress=compress,no_image=no_image)
else:
args = Namespace(device=device, log=air_log, recording=None, script=script,
language="zh")
try:
run_script(args)
except AssertionError as e:
pass
finally:
if deviceType.upper() == "WEB":
rpt = LogToHtml(script, air_log, plugins=["airtest_selenium.report"])
else:
rpt = LogToHtml(script, air_log)
rpt.report(output_file=html)
result = {}
result["name"] = airName.replace('.air', '')
result["result"] = rpt.test_result
result["log_air"] = air_log
results.append(result)
end_time = datetime.datetime.now()
end_time_fmt = end_time.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
duration = (end_time - start_time).seconds
# 创建一个文件系统加载器对象
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(template_path),
extensions=(),
autoescape=True
)
# 获取一个模板文件
template = env.get_template(template_name, template_path)
project_name = root_path.split("/")[-1]
success = 0
fail = 0
# 根据results的值,统计success和fail的总数
for res in results:
if res['result']:
success += 1
else:
fail += 1
report_name = "report_"+end_time.strftime("%Y%m%d%H%M%S")+".html"
# 渲染
html = template.render({"results": results,"device":device,"stime":start_time_fmt,'etime':end_time_fmt,'duration':duration,"project":project_name,"success":success,"fail":fail})
# 把渲染后的html写到指定的报告中
output_file = os.path.join(root_path,"report" ,report_name)
with open(output_file, 'w', encoding="utf-8") as f:
f.write(html)
if __name__ == "__main__":
for device in devices:
test = Air_Case_Handler(device)
test.run_air(air_path,device)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/zhaim/airtest-runner-single.git
git@gitee.com:zhaim/airtest-runner-single.git
zhaim
airtest-runner-single
Airtest-Runner-single
master

搜索帮助