-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathreport.py
141 lines (113 loc) · 4.53 KB
/
report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# -*- coding: utf-8 -*-
import os
import io
import types
import shutil
import json
import jinja2
from airtest.utils.compat import decode_path
import airtest.report.report as R
HTML_FILE = "log.html"
HTML_TPL = "log_template.html"
STATIC_DIR = os.path.dirname(R.__file__)
def get_parger(ap):
ap.add_argument("script", help="script filepath")
ap.add_argument("--outfile", help="output html filepath, default to be log.html")
ap.add_argument("--static_root", help="static files root dir")
ap.add_argument("--log_root", help="log & screen data root dir, logfile should be log_root/log.txt")
ap.add_argument("--record", help="custom screen record file path", nargs="+")
ap.add_argument("--export", help="export a portable report dir containing all resources")
ap.add_argument("--lang", help="report language", default="en")
ap.add_argument("--plugins", help="load reporter plugins", nargs="+")
return ap
def get_script_info(script_path):
script_name = os.path.basename(script_path)
result_json = {"name": script_name, "author": None, "title": script_name, "desc": None}
return json.dumps(result_json)
def _make_export_dir(self):
dirpath = self.script_root
logpath = self.script_root
# copy static files
for subdir in ["css", "fonts", "image", "js"]:
dist = os.path.join(dirpath, "static", subdir)
shutil.rmtree(dist, ignore_errors=True)
self.copy_tree(os.path.join(STATIC_DIR, subdir), dist)
return dirpath, logpath
def report(self, template_name, output_file=None, record_list=None):
"""替换LogToHtml中的report方法"""
self._load()
steps = self._analyse()
# 修改info获取方式
info = json.loads(get_script_info(self.script_root))
if self.export_dir:
self.script_root, self.log_root = self._make_export_dir()
output_file = os.path.join(self.script_root, HTML_FILE)
self.static_root = "static/"
if not record_list:
record_list = [f for f in os.listdir(self.log_root) if f.endswith(".mp4")]
records = [os.path.join(self.log_root, f) for f in record_list]
if not self.static_root.endswith(os.path.sep):
self.static_root = self.static_root.replace("\\", "/")
self.static_root += "/"
data = {}
data['steps'] = steps
data['name'] = os.path.basename(self.script_root)
data['scale'] = self.scale
data['test_result'] = self.test_result
data['run_end'] = self.run_end
data['run_start'] = self.run_start
data['static_root'] = self.static_root
data['lang'] = self.lang
data['records'] = records
data['info'] = info
return self._render(template_name, output_file, **data)
def get_result(self):
return self.test_result
def main(args):
# script filepath
path = decode_path(args.script)
record_list = args.record or []
log_root = decode_path(args.log_root) or path
static_root = args.static_root or STATIC_DIR
static_root = decode_path(static_root)
export = decode_path(args.export) if args.export else None
lang = args.lang if args.lang in ['zh', 'en'] else 'zh'
plugins = args.plugins
# gen html report
rpt = R.LogToHtml(path, log_root, static_root, export_dir=export, lang=lang, plugins=plugins)
# override methods
rpt._make_export_dir = types.MethodType(_make_export_dir, rpt)
rpt.report = types.MethodType(report, rpt)
rpt.get_result = types.MethodType(get_result, rpt)
rpt.report(HTML_TPL, output_file=args.outfile, record_list=record_list)
return rpt.get_result()
if __name__ == "__main__":
import argparse
ap = argparse.ArgumentParser()
args = get_parger(ap).parse_args()
basedir = os.path.dirname(os.path.realpath(__file__))
logdir = os.path.realpath(args.script)
# 聚合结果
results = []
# 遍历所有日志
for subdir in os.listdir(logdir):
if os.path.isfile(os.path.join(logdir, subdir)):
continue
args.script = os.path.join(logdir, subdir)
args.outfile = os.path.join(args.script, HTML_FILE)
result = {}
result["name"] = subdir
result["result"] = main(args)
results.append(result)
# 生成聚合报告
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(basedir),
extensions=(),
autoescape=True
)
template = env.get_template("summary_template.html")
html = template.render({"results": results})
output_file = os.path.join(logdir, "summary.html")
with io.open(output_file, 'w', encoding="utf-8") as f:
f.write(html)
print(output_file)