在上一篇文章中,我们讲了Airtest中测试报告生成的方法。细心的同学会发现,我们每次生成的报告名称都是一样的。
例如在AirtestIDE运行脚本之后,点击 查看报告 按钮,生成的报告默认都命名为 log.html :


每次重新运行脚本后,再点击 查看报告 ,都会把旧的Airtest报告内容覆盖掉。
但有些同学需要保存历史版本的Airtest报告,并不想每次都覆盖掉,这该如何实现呢?
为了解决上述问题,我们接下来介绍如何自定义Airtest报告的名称。
一、使用 simple_report 自定义报告名称
1)simple_report 函数
def simple_report(filepath, logpath=True, logfile=None, output=HTML_FILE):
path, name = script_dir_name(filepath)
if logpath is True:
logpath = os.path.join(path, getattr(ST, "LOG_DIR", DEFAULT_LOG_DIR))
rpt = LogToHtml(path, logpath, logfile=logfile or getattr(ST, "LOG_FILE", DEFAULT_LOG_FILE), script_name=name)
rpt.report(HTML_TPL, output_file=output)
simple_report 接口支持我们传入以下4个参数:
-
filepath:脚本文件的路径,用来确定脚本所在的目录 -
logpath:用于确定日志文件所在的路径,可选参数,默认值为True -
logfile:用于指定日志文件名,可选参数,默认值为None -
output:指定生成的html报告的输出位置,必须以.html结尾,可选参数,默认值是log.html
其中,output 参数就是我们可以用来自定义HTML报告名称的参数,我们可以用它来指定生成报告的完整路径(包含报告名称)
2)使用simple_report 函数生成报告
# -*- encoding=utf8 -*-
__author__ = "gmluo"
from airtest.core.api import *
from airtest.cli.parser import cli_setup
from airtest_selenium.proxy import WebChrome
from airtest.report.report import simple_report
if not cli_setup():
auto_setup(__file__, logdir=True, devices=["Windows:///", ])
driver = WebChrome(executable_path='D:/programs/chromedriver-win64/chromedriver.exe')
driver.implicitly_wait(20)
driver.maximize_window()
driver.get("https://www.baidu.com/")
driver.assert_template(Template(r"tpl1723541903681.png", record_pos=(0.06, -0.271), resolution=(2710, 2080)),
"Please fill in the test point.")
# 退出当前页面
driver.close()
# generate html report
report_file_path = './log/simple_report.html'
simple_report(__file__, logpath=True, output=report_file_path)


二、使用 LogToHtml 自定义报告名称
1)LogToHtml 函数
class LogToHtml(object):
"""Convert log to html display """
scale = 0.5
def __init__(self, script_root, log_root="", static_root="", export_dir=None, script_name="", logfile=None, lang="en", plugins=None):
self.log = []
self.devices = {}
self.script_root = script_root
self.script_name = script_name
if not self.script_name or os.path.isfile(self.script_root):
self.script_root, self.script_name = script_dir_name(self.script_root)
self.log_root = log_root or ST.LOG_DIR or os.path.join(".", DEFAULT_LOG_DIR)
self.static_root = static_root or STATIC_DIR
self.test_result = True
self.run_start = None
self.run_end = None
self.export_dir = export_dir
self.logfile = logfile or getattr(ST, "LOG_FILE", DEFAULT_LOG_FILE)
self.lang = lang
self.init_plugin_modules(plugins)
参数含义:
-
script_root:脚本所在文件夹。
-
log_root:log.txt 所在文件夹。
-
static_root:部署静态资源的服务器路径。
-
export_dir:导出报告文件夹。
-
script_name:脚本名称。
-
logfile:log.txt 的路径。
-
lang:报告的语言(中文:zh;英文:en)。
-
plugins:插件,使用了poco或者airtest-selenium会用到。
2)使用LogToHtml 函数生成报告
# -*- encoding=utf8 -*-
__author__ = "gmluo"
from airtest.core.api import *
from airtest.cli.parser import cli_setup
from airtest_selenium.proxy import WebChrome
from airtest.report.report import LogToHtml
if not cli_setup():
auto_setup(__file__, logdir=True, devices=["Windows:///", ])
driver = WebChrome(executable_path='D:/programs/chromedriver-win64/chromedriver.exe')
driver.implicitly_wait(20)
driver.maximize_window()
driver.get("https://www.baidu.com/")
driver.assert_template(Template(r"tpl1723543008762.png", record_pos=(0.324, -0.044), resolution=(2710, 2080)),
"Please fill in the test point.")
driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[4]/a").click()
sleep(1.0)
driver.assert_template(Template(r"tpl1723602062748.png", record_pos=(0.056, -0.176), resolution=(2710, 2080)),
"Please fill in the test point.")
# 退出当前页面
driver.close()
# generate html report
reporter = LogToHtml(script_root=__file__)
report_file_path = './log/LogToHtml.html'
reporter.report(output_file=report_file_path)


三、使用命令行 自定义报告名称
在命令行生成报告时,使用 outfile 参数可以来实现这个需求。
1)在cmd.exe中执行命令

2)生成报告时设置输出html报告名称
airtest report command_title.py --outfile log\command_title.html --lang zh


四、重复运行脚本时生成报告如何不覆盖历史报告
那了解了如何自定义报告名称之后,再来看 不覆盖历史报告 这个需求,就简单一些了。
我们可以自定义html报告的名称,为一种 不重复的命名规则 即可,比如每次都用当前时间来命名html报告;又或者更简单的是,直接在html的命名规则里添加递增的数字:
# -*- encoding=utf8 -*-
__author__ = "gmluo"
from airtest.core.api import *
from airtest.cli.parser import cli_setup
from airtest_selenium.proxy import WebChrome
from airtest.report.report import LogToHtml
if not cli_setup():
auto_setup(__file__, logdir=True, devices=["Windows:///", ])
driver = WebChrome(executable_path='D:/programs/chromedriver-win64/chromedriver.exe')
driver.implicitly_wait(20)
driver.maximize_window()
driver.get("https://www.baidu.com/")
driver.assert_template(Template(r"tpl1723543008762.png", record_pos=(0.324, -0.044), resolution=(2710, 2080)),
"Please fill in the test point.")
driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[4]/a").click()
sleep(1.0)
driver.assert_template(Template(r"tpl1723602062748.png", record_pos=(0.056, -0.176), resolution=(2710, 2080)),
"Please fill in the test point.")
# 退出当前页面
driver.close()
# generate html report
reporter = LogToHtml(script_root=__file__)
# report_file_path = './log/LogToHtml.html'
# 格式化当前时间
now_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
# 拼接当前时间作为后缀生成html报告
report_file_path = './log/LogToHtml' + now_time + '.html'
# 生成html报告
reporter.report(output_file=report_file_path)



总结
Airtest 提供的多种报告生成方式的同时,也支持测试报告生成时进行报告名称的自定义,可以根据实际需求进行选择。
最后: 下方这份完整的软件测试视频教程已经整理上传完成,需要的朋友们可以自行领取【保证100%免费】

软件测试面试文档
我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。




2263

被折叠的 条评论
为什么被折叠?



