1 Star 2 Fork 1

panzhihui/autotest

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
report.py 3.55 KB
一键复制 编辑 原始数据 按行查看 历史
panzhihui 提交于 2023-07-27 14:34 . Add doc
from collections import defaultdict
from typing import List
from dataclasses import dataclass
import os
import argparse
from jinja2 import Environment
from jinja2 import FileSystemLoader
from jinja2 import select_autoescape
from junitparser import TestCase
from benchmark import TestRun, Comparison
def get_template(load_path, template_name):
env = Environment(
loader=FileSystemLoader(load_path),
autoescape=select_autoescape(),
trim_blocks=True,
lstrip_blocks=True
)
return env.get_template(template_name)
def get_runs(run_fnames) -> List[TestRun]:
runs = [TestRun.fromjunitxml(fname) for fname in run_fnames]
return runs
def get_comparison(runs) -> Comparison:
return Comparison(runs)
@dataclass
class TemplateCase:
'''Data structure for 'case' in the template.'''
name: str
classname: str
results: List[str]
match: bool
@classmethod
def from_junixml_testcases(cls, cases: List[TestCase]):
if not cases:
raise ValueError("Empty cases.")
def get_result(case: TestCase):
if case.is_passed:
return 'pass'
elif case.is_skipped:
return 'skip'
else:
return case.result[0].message
is_match = all(case.is_passed for case in cases) or\
all(case.is_skipped for case in cases) or\
all(not case.is_passed and not case.is_skipped for case in cases)
return cls(cases[0].name,
cases[0].classname,
[get_result(case) for case in cases],
is_match)
class Report:
'''Collect JUnitXML test results and generate html report.'''
def __init__(self, run_paths, template_path, template_name):
self.runs = get_runs(run_paths)
self.comparison = get_comparison(self.runs)
self.template = get_template(template_path, template_name)
self.name_tests_diff_run = None
def _get_runs(self):
return self.runs
def _get_tests(self):
self.name_tests_diff_run = defaultdict(list)
for run in self.runs:
for classname, test in run.test_suite.items():
self.name_tests_diff_run[classname].append(test)
return self.name_tests_diff_run
def _get_cases(self):
cases = dict()
for classname, test_compare in self.comparison.get_test_compares().items():
cases[classname] = [TemplateCase.from_junixml_testcases(cases) for cases in test_compare.get_case_compares().values()]
return cases
def render_report(self):
'''Render template and generate the html report.'''
params = {
"runs": self._get_runs(),
"tests": self._get_tests(),
"name_cases": self._get_cases()
}
rendered = self.template.render(**params)
return rendered
def make_parser():
parser = argparse.ArgumentParser()
parser.add_argument('run_paths',
nargs='+',
metavar='path',
help='Paths of JunitXML test result.')
parser.add_argument('report_path')
return parser
if __name__ == "__main__":
parser = make_parser()
args = parser.parse_args()
run_paths = [os.path.abspath(path) for path in args.run_paths]
report = Report(run_paths, os.path.dirname(__file__), "template.html")
html = report.render_report()
with open(os.path.abspath(args.report_path), 'w', encoding='utf-8') as f:
f.write(html)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/panzhihui1/autotest.git
[email protected]:panzhihui1/autotest.git
panzhihui1
autotest
autotest
master

搜索帮助