代码拉取完成,页面将自动刷新
from typing import List, Dict
from junitparser import TestCase, JUnitXml
from collections import defaultdict
import os
from dataclasses import dataclass
class Test:
def __init__(self, runtag, classname, cases: List[TestCase]):
'''
runtag: tag for the test run
classname: name for the test class, e.g. test_sin
'''
self.runtag = runtag
self.classname = classname
self.pass_ = 0
self.fail = 0
self.skip = 0
self.case_num = len(cases)
self._stats(cases)
self.cases = {case.name:case for case in cases}
def _stats(self, cases: List[TestCase]):
for case in cases:
self.pass_ += 1 if case.is_passed else 0
self.skip += 1 if case.is_skipped else 0
self.fail = self.case_num - self.pass_ - self.skip
def summary(self):
return (self.pass_, self.fail, self.skip)
@dataclass
class TestRun:
tag: str
test_suite: Dict[str, Test] # classname, Test
@classmethod
def fromjunitxml(cls, path):
tag = os.path.splitext(os.path.basename(path))[0]
run = JUnitXml.fromstring(cls.preprocess(path))
name_cases = defaultdict(list)
for test_suite_ in run:
for case in test_suite_:
name_cases[case.classname].append(case)
tests = {classname: Test(tag, classname, cases)
for classname, cases
in name_cases.items()}
return cls(tag, tests)
@classmethod
def preprocess(cls, path):
'''
In some cases LFs are not converted to ' ' in xml. Do it manually.
'''
with open(path, 'r', encoding='utf-8') as xml:
s = xml.read()
start = 0
while start != -1 and s.find('<failure', start) != -1:
end = s.find('>', start)
before = s[:start]
after = s[end:]
replaced = s[start:end].replace('\n', ' ')
s = before + replaced + after
start = s.find('<failure', end)
return s.encode('utf-8')
class TestComparison():
def __init__(self, tests:List[Test]):
self.tests = tests
self.name_cases_compare = dict()
self._compare(tests)
def _compare(self, tests:List[Test]):
name_cases = defaultdict(list)
for test in tests: # cluster case by name
for name, case in test.cases.items():
name_cases[name].append(case)
for name, cases in name_cases.items():
self.name_cases_compare[name] = self._compare_cases(cases)
def _compare_cases(self, cases:List[TestCase]):
return cases
def get_case_compare(self, name):
return self.name_cases_compare[name]
def get_case_compares(self):
return self.name_cases_compare
class Comparison:
def __init__(self, runs: List[TestRun]):
self.test_compares: Dict[str, TestComparison] = dict()
self._compare(runs)
def _compare(self, runs: List[TestRun]):
name_tests = defaultdict(list)
for run in runs:
for classname, test in run.test_suite.items():
name_tests[classname].append(test)
self._compare_tests(name_tests)
def _compare_tests(self, name_tests: Dict[str, List[Test]]):
for classname, test in name_tests.items():
self.test_compares[classname] = TestComparison(test)
def get_test_compare(self, classname):
return self.test_compares[classname]
def get_test_compares(self):
return self.test_compares
if __name__ == "__main__":
run = TestRun.fromjunitxml("junitxml2")
import pdb
pdb.set_trace()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。