unittest + airtest + HTMLTestRunner 执行脚本并生成测试报告

使用场景:功能回归测试、list清单检查或者单元测试场景

airtest脚本

1、poco控件
2、图片识别

区别在于:

  • 用poco控件编写的脚本容错率较低,兼容性更强,更适合跑兼容性测试,但需要项目接入poco-sdk
  • 用图片识别编写的脚本容错率较高,兼容性较弱,没有代码权限无法尝试接入poco-sdk时可以选择此方式

结合unittest单元测试框架和HTMLTestRunner

测试报告模板使用HTMLTestRunner

difference
另外在此模板的基础上,添加了截图显示,在报告中点击缩略图会跳转显示原图

脚本内需要导入(使用poco需要另外导入相应模块):

# airtest模块
from airtest.core.api import *
# unittest是python的单元测试框架
import unittest
# HTMLTestRunnerCN是专门为unitest搞的html测试报告
import HTMLTestRunnerCN

setUp()方法用于测试用例执行前的初始化工作。
如测试用例中需要访问数据库,可以在setUp中建立数据库连接并进行初始化。如测试用例需要登录web,可以先实例化浏览器
在这里我主要是用于移动端测试,所以在setUp中建立手机连接

def setUp(self):
    auto_setup(__file__, logdir=True, devices=[
        f"android://127.0.0.1:5037/{devices}?cap_method=javacap&touch_method=adb",
    ])

生成测试报告

# 获取当前时间,这样便于下面的使用。
now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time())) + '.html'
# filePath:测试报告保存的路径
filePath = os.path.join(os.getcwd(), 'report', now)
fp = open(filePath, 'wb')
# 测试报告的title、描述
runner = HTMLTestRunnerCN.HTMLTestReportCN(stream=fp,
                                           title='自动化回归测试报告',
                                           description='',
                                           tester='屈闯'
                                           )
runner.run(suite)
# 最后记着关闭文件
fp.close()

报告展示

report_1

report_2

其他问题

使用图片识别写脚本,可以正常运行,但是报告中会显示报错

error: (-213:The function/feature is not implemented) 
This algorithm is patented and is excluded in this configuration; 
Set OPENCV_ENABLE_NONFREE CMake option and rebuild the library in function 'cv::xfeatures2d::SURF::create'

bug_1
谷歌一下,发现是sift算法已经申请专利,开源OpenCV没有版权,新的OpenCV去掉了这个算法

解决方法:
1.卸载已安装的高版本
pip uninstall opencv-python
pip uninstall opencv-contrib-python
2.安装低版本
pip install --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python==3.4.2.16
pip install --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-contrib-python==3.4.2.16
完美解决

posted @ 2021-02-25 16:44  者諹  阅读(566)  评论(0编辑  收藏  举报