发布于2019-08-06 11:18 阅读(3085) 评论(0) 点赞(0) 收藏(2)
1.项目结构如图:
2.执行结果图:
3.点击跳转相信报告
4.summary_template.heml 源代码
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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>
<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>
4.启动器源代码
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")
# add var/function/class/.. to globals
# self.scope["hunter"] = "i am hunter"
# self.scope["add"] = lambda x: x+1
# exec setup script
# self.exec_other_script("setup.owl")
super(CustomAirtestCase, self).setUp()
def tearDown(self):
print("custom tearDown")
# exec teardown script
# self.exec_other_script("teardown.owl")
super(CustomAirtestCase, self).setUp()
def run_air(self, root_dir='F:\\airtest-runner\\suite', device=['Windows:///526006']):
# 聚合结果
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"):
# f为.air案例名称:手机银行.airK
airName = f
script = os.path.join(root_dir, f)
# airName_path为.air的全路径:D:\tools\airtestCase\案例集\log\手机银行
print(script)
# 日志存放路径和名称:D:\tools\airtestCase\案例集\log\手机银行1
log = os.path.join(root_dir, 'log' + '\\' + airName.replace('.air', ''))
log_1 = script + '\\' + 'log'
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_1, recording=None, script=script)
try:
run_script(args, AirtestCase)
except:
pass
finally:
# rpt = report.LogToHtml(script, log)
rpt = report.LogToHtml(script, log_1, script_name=f.replace(".air", ".py"))
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 = ['Windows:///526006']
test.run_air('F:\\airtest-runner\\suite', device)
作者:可以给我吃一口吗
链接:https://www.pythonheidong.com/blog/article/8276/30860ad350e61abd1ad0/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!