airtest中log()和报告生成相关的接口,可以为报告的步骤添加用户自定义的信息
6.log记录和报告生成相关的接口
log()
接口方便插入用户自定义的一些log信息,将会被显示在Airtest报告中。在1.1.6版本的Airtest中,log接口支持传入4个参数:
args
,可以是字符串、非字符串或者traceback
对象;timestamp
,用于自定义当前log的时间戳;desc
,用于自定义log的标题;snapshot
,表示是否需要截取一张当前的屏幕图像并显示到报告中:
示例如下:
# 传入字符串
log("123",desc="这是标题01")
# 传入非字符串
data = {"test": 123, "time": 123456}
log(data,desc="这是标题02")
# 传入traceback
try:
1/0
except Exception as e:
log(e, desc="这是标题03")
注意,如果不传入 timestamp
的话,默认记录当前时间:
# 记录timestamp,并且对当前画面截图
log("123", timestamp=time.time(), desc="这是标题04", snapshot=True)
接口的API文档链接:https://airtest.readthedocs.io/zh_CN/latest/all_module/airtest.core.helper.html?highlight=log#airtest.core.helper.log
生成报告的简单接口,用户只需要在脚本中调用这个接口,然后传入几个必要的报告参数,就能够在脚本运行完毕之后,按照要求自动生成Airtest报告:
simple_report(filepath, logpath=True, logfile='log.txt', output='log.html')
其中可传入的4个参数分别表示:
filepath
,脚本文件的路径,可以直接传入变量__file__
logpath
,log内容所在路径,如为True
,则默认去当前脚本所在路径找log内容logfile
,log.txt的文件路径output
,报告的到处路径,必须以.html
结尾
示例如下:
from airtest.report.report import simple_report
auto_setup(__file__, logdir=True)
# 此处省略N条用例脚本
simple_report(__file__,logpath=True,logfile=r"D:\test\1234.air\log\log.txt",output=r"D:\test\1234.air\log\log1234.html")
另外需要注意的是,我们要在用例脚本之后调用这个生成报告的接口,如在脚本开头调用,则意味着还没有运行后面的用例步骤,就已经生成了一份报告,最终不论我们脚本运行情况如何,我们都只能拿到一份空的测试报告。
接口的API文档链接:https://airtest.readthedocs.io/zh_CN/latest/all_module/airtest.report.report.html#airtest.report.report.simple_report
报告的基类,实际上我们刚才介绍的 simple_report
也是用这个类实现的,只不过 simple_report
的参数更少,用法更加简单:
class LogToHtml(script_root, log_root='', static_root='', export_dir=None, script_name='', logfile='log.txt', lang='en', plugins=None)
可以看到, logtohtml
类可以传入的参数非常多,用法相对复杂一些:
script_root
,脚本路径log_root
,log文件的路径static_root
,部署静态资源的服务器路径export_dir
,导出报告的存放路径script_name
,脚本名称logfile
,log文件log.txt的路径lang
,报告的语言(中文:zh;英文:en)plugins
,插件,使用了poco或者airtest-selenium会用到
使用 logtohtml
生成测试报告时,我们一般先实例化一个 logtohtml
对象,然后用这个对象调用类方法 report()
生成报告,示例如下:
from airtest.report.report import LogToHtml
# 此处省略N条用例脚本
h1 = LogToHtml(script_root=r'D:\test\1234.air', log_root=r"D:\test\1234.air\log", export_dir=r"D:\test\1234.air" ,logfile=r'D:\test\1234.air\log\log.txt', lang='en', plugins=["poco.utils.airtest.report"])
h1.report()
LogToHtml
类的API文档链接:https://airtest.readthedocs.io/zh_CN/latest/all_module/airtest.report.report.html#airtest.report.report.LogToHtml
PS:如需查看命令行生成报告和使用脚本生成报告的详细教程,推荐阅读文章 Airtest报告“全攻略”,看完直接用脚本生成、导出报告,真香!
但一些经常导出报告的同学会发现,每次导出1份报告,就会带有1份相同的静态资源文件
static
,它包含了报告中的css和js等文件,这样的重复拷贝占用了很多的磁盘空间。并且我们知道,除非报告的样式做了某些更新,否则这些静态资源文件都是固定不变的。所以我们可不可以把这个静态资源文件统一放到1个地方,每一份HTML报告都去这个地方访问这些静态资源呢?这样即使静态文件有所改变,我们也只需要替换掉这个地方的静态资源文件即可。
答案当然是可以的,使用
--static_root
参数即可实现!我们可以将这些资源文件部署到静态资源文件服务器上,用例如https://host:port/static/css/
的路径来访问它。然后在生成报告时,将这个部署出来的服务器地址作为--static_root
的参数传过去,另外需要注意的是,传入--static_root
的参数需要以http
开头才能被识别
airtest report D:/test/report_test.air --log_root C:/Users/xiaoming/log --lang zh --static_root https://host:port --export E:/log_test/
--static_root 指定静态资源文件访问服务器地址
airtest report airCase1.air --log_root log --outfile log.html --lang zh --export exportDir --static_root http://127.0.0.1:8888/test
--static_root http://127.0.0.1:8888/test:设置静态资源文件访问服务器地址,设置后生成的报告log.html内容如下:
报告插件使用,例如接入poco报告命令:
airtest report airCase1.air --log_root log --outfile log.html --lang zh --export exportDir --plugins poco.utils.airtest.report
--plugins poco.utils.airtest.report:结合poco报告插件生成对应poco的报告,如果代码中有poco代码,报告中会显示
也可以接入selenium报告:
airtest report test.air --plugins airtest_selenium.report
【备注】:生成report之前必须先run,生成log.txt文件才可以出报告。
7.Airtest的一些全局设置
Airtest的设置模块详见文档:https://airtest.readthedocs.io/zh_CN/latest/all_module/airtest.core.settings.html?highlight=settings#airtest.core.settings.Settings
LOGFILE
用于自定义记录log内容的txt文档的名称;LOGDIR
用于自定义log内容的保存路径,示例如下:
from airtest.core.settings import Settings as ST
from airtest.core.helper import set_logdir
ST.LOG_FILE = "log123.txt"
set_logdir(r'D:\test\1234.air\logs')
auto_setup(__file__)
# 此处省略N条用例脚本
参考: