airtest测试用例报告聚合方便查看每个用例以及跳转至具体用例

使用airtest的人都知道,测试用例全部运行结束之后,airtest没有将用例进行聚合,查看单个用例结果非常不方便,需要将报告聚合起来查看,我的本地环境文件夹目录如下:

在当前文件夹下新建summary_template.html文件(生成html聚合报告的模板),与自己跑的myrunner.py文件,

myrunner文件代码如下:

from airtest.cli.runner import AirtestCase, run_script
from argparse import *
import airtest.report.report as report
import jinja2
import shutil
import os
import io


class CustomAirtestCase(AirtestCase):
    def setUp(self):
        print("custom setup")
        print('从这里开始')
        super(CustomAirtestCase, self).setUp()

    def tearDown(self):
        print("custom tearDown")
        super(CustomAirtestCase, self).setUp()

    def run_air(self, root_dir='C:\\Users\\Desktop\\LynkcoApp\\TestCase',
                device=['android://127.0.0.1:5037/7da89ed2']):
        # 聚合结果
        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"):
                airName = f
                script = os.path.join(root_dir, f)
                print('script', script)
                log = os.path.join(root_dir, 'log' + '\\' + airName.replace('.air', ''))
                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, compress=1,no_image=False)
                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)


if __name__ == '__main__':
    test = CustomAirtestCase()
    device = ['android://127.0.0.1:5037/7da89ed2']
    test.run_air('C:\\Users\\Desktop\\LynkcoApp\\TestCase', device)

 

模板html文件如下:

<!DOCTYPE html>
<html>
<head>
    <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;
      }
      .details-col-msg {
         width: 7em;
         text-align: center;
         background-color:#ccc;
      }
 
    </style>
</head>
<body>
<div>
<div><h2>Test Statistics</h2></div>
 
    <table width="800" border="thin" cellspacing="0" cellpadding="0"> 
        <tr  width="600">
            <th width="300" class='details-col-msg'>案例名称</th>
            <th class='details-col-msg'>执行结果</th>
        </tr>
        {% for r in results %}
        <tr width="600">
            <td class='details-col-elapsed'><a href="log/{{r.name}}/log.html" target="view_window">{{r.name}}</a></td>
            <td class="{{'success' if r.result else 'fail'}}">{{"成功" if r.result else "失败"}}</td>
        </tr>
        {% endfor %}
    </table>
</div>
</body>
</html>

运营结果:

点击蓝色部分就可以跳转到某个具体用例

大家可以看到,聚合报告的生产使用了jinja2模板语言,这个语言类似python 的django模板语言。py文件中需要传入case路径和设备名称等。

问题一:运行

大家在运行的时候,使用python myrunner.py 运行,不要直接在pycharm下运行unittest,不然会报错

问题二:

args = Namespace(device=device, log=log, recording=None, script=script, compress=1,no_image=False) 本地airtest版本是1.1.8版本,老的版本传参不一致,这里需要修改

问题三:

生成的html语言无法跳转,这里要修改路径,

<td class='details-col-elapsed'><a href="log/{{r.name}}/log.html" target="view_window">{{r.name}}</a></td>,将href连接地址修改为相对路径地址,r 是一个对象

问题四:

对于有些手机,团队中使用的小米手机,版本比较高,会报相关错误信息minCap run timeout,需要增加参数

device = ['android://127.0.0.1:5037/881fe831?cap_method=JAVACAP&ori_method=ADBOR'] 在传入的device信息中增加命令操作

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值