airtest 批量执行脚本,测试报告内容始终为空,不知道是哪里出问题了,无法解决。

生成聚合报告

点击链接跳转到 airtest report

配置文件

import os
deviceType = "APP"                                                              #设备类别:app、win和web
devices = ['android://127.0.0.1:5037/780a7588']                                 #设备信息,只有当deviceType为APP是有效
root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))         #工程根目录
air_path = os.path.join(root_path, 'air')                                       #脚本目录
log_path = os.path.join(root_path, 'log')                                       #日志目录
template_path = os.path.join(root_path, 'template')                             #测试报告模板目录
report_path = os.path.join(root_path, 'report')                                 #测试报告路径
data_path = os.path.join(root_path, 'data')                                     #测试数据目录
template_name ="summary_template.html"                                          #测试报告模板名称
clear_report = False                                                            #是否清空旧测试报告


主入口信息

from airtest.cli.runner import AirtestCase,run_script
import airtest.report.report as report
from conf.settings import *
from argparse import *
import shutil,os,io,jinja2,datetime
from lib.common import RetryFunc
from lib import video
from airtest.core.helper import device_platform
from airtest.core.api import auto_setup, log, connect_device


class Air_Case_Handler(AirtestCase):
    def __init__(self,dev_id):
        super(Air_Case_Handler, self).__init__()
        if deviceType.upper() == "WEB":
            pass
        else:
            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 = []
        root_log = log_path
        if os.path.isdir(root_log):
            shutil.rmtree(root_log)
        else:
            os.makedirs(root_log)


        for file in os.listdir(air_path):
            if file.endswith(".air"):
                airName = file
                airDirName = file.replace(".air","")
                script = os.path.join(air_dir,file)

                air_log = os.path.join(root_path,"log\\" + airDirName)

                if os.path.isdir(air_log):
                    shutil.rmtree(air_log)
                else:
                    os.makedirs(air_log)

                html = os.path.join(air_log,"log.html")
                if deviceType.upper() == "WEB":
                    args = Namespace(log=air_log, recording=None, script=script,
                                     language="zh")
                elif deviceType.upper() == "APP":
                    args = Namespace(device=device, log=air_log, recording=airDirName+".mp4", script=script, compress=10, language="zh")
                else:
                    args = Namespace(device=device, log=air_log, recording=None, script=script,
                                     language="zh")
                try:
                    run_script(args,AirtestCase)
                except AssertionError as e:
                    print(e)
                finally:
                    rpt = report.LogToHtml(script, air_log)
                    rpt.report("log_template.html", output_file=html)
                    result = {}
                    result["name"] = airName.replace('.air', '')
                    result["result"] = rpt.test_result
                    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
        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})
        output_file = os.path.join(root_path,"report" ,report_name)
        with io.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)


↙↙↙阅读原文可查看相关链接,并与作者交流