一份漂亮的使用Airtest批量执行案例脚本并聚合报告的方法

通过搜集网络上已有的解决方案以及自己研究airtest底层的代码,整理出适合大致的批量执行Airtest脚本的解决方法。

效果图如下:

代码目录结构:img ## 执行结果图:

img

点击案例名称调整至详细报告:

img

## 解决方案:

在Python3.6环境下新建myRunner.py文件:编写如下代码

 1 from airtest.cli.runner import AirtestCase, run_script
 2 from argparse import *
 3 import airtest.report.report as report
 4 import jinja2
 5 import shutil
 6 import os
 7 import io
 8 class CustomAirtestCase(AirtestCase): def setUp(self): print("custom setup") # add var/function/class/.. to globals # self.scope["hunter"] = "i am hunter" # self.scope["add"] = lambda x: x+1 # exec setup script # self.exec_other_script("setup.owl") super(CustomAirtestCase, self).setUp() def tearDown(self): print("custom tearDown") # exec teardown script # self.exec_other_script("teardown.owl") super(CustomAirtestCase, self).setUp() def run_air(self, root_dir='D:\tools\airtestCase\案例集', device=['android://127.0.0.1:5037/99.12.74.40:7281']):
 9 
10 # 聚合结果 results = [] # 获取所有用例集 root_log = root_dir + '\' + 'log' if os.path.isdir(root_log): shutil.rmtree(root_log) else: os.makedirs(root_log) print(str(root_log) + 'is created') for f in os.listdir(root_dir): if f.endswith(".air"): # f为.air案例名称:手机银行.air airName = f script = os.path.join(root_dir, f) # airName_path为.air的全路径:D:\tools\airtestCase\案例集\log\手机银行 print(script) # 日志存放路径和名称:D:\tools\airtestCase\案例集\log\手机银行1 log = os.path.join(root_dir, 'log' + '\' + airName.replace('.air', ''))
11 
12 print(log) if os.path.isdir(log): shutil.rmtree(log) else: os.makedirs(log) print(str(log) + 'is created') output_file = log + '\' + 'log.html' args = Namespace(device=device, log=log, recording=None, script=script) try: run_script(args, AirtestCase) except: pass finally: rpt = report.LogToHtml(script, log) rpt.report("log_template.html", output_file=output_file) result = {} result["name"] = airName.replace('.air', '') result["result"] = rpt.test_result results.append(result) # 生成聚合报告 env = jinja2.Environment( loader=jinja2.FileSystemLoader(root_dir), extensions=(), autoescape=True ) template = env.get_template("summary_template.html", root_dir html = template.render({"results": results}) output_file = os.path.join(root_dir, "summary.html") with io.open(output_file, 'w', encoding="utf-8") as f: f.write(html) print(output_file)
13 
14 if name == 'main': test = CustomAirtestCase() device = ['android://127.0.0.1:5037/99.12.74.40:7237'] test.run_air('D:\tools\airtestCase\案例集', device) 

myRunner.py文件导入了底层的report.py和runner.py文件,

可以直接调用底层的方法run_script执行案例,LogToHtml方法生成报告,这样的好处是不需要去修改和维护底层airtest库文件,(网上比较多的做法直接修改airtest底层库文件,这种方法就需要不断维护底层的库文件)

myRunner.py中最重要的方法run_air的思想是,

1.遍历根目录下的所有.air文件

2.调用run_script方法执行案例(IDE上的执行案例实际上就是运行该方法),

3.执行LogToHtml生成log.html报告,并将第二步执行的结果存放到result中

前三步完成后:将结果聚合起来:

报告模板文件summary_template.html:

 1 html <!DOCTYPE html> <html> <head>
 2 
 3 <title>测试结果汇总</title> <style> .fail { color: red; width: 7emem; text-align: center; } .success { color: green; width: 7emem; text-align: center; } .details-col-elapsed { width: 7em; text-align: center; }
 4 
 5 .details-col-msg { width: 7em; text-align: center; } </style> </head> <body> <div>
 6 
 7  8 Test Statistics
 9  {% for r in results %}{% endfor %}
10 案例名称    执行结果
11 {{r.name}}    {{"成功" if r.result else "失败"}}
12 
13

 

 

posted @ 2020-08-19 22:31  茶茶爱喝奶茶ya  阅读(394)  评论(0编辑  收藏  举报