Repository navigation
Expand file tree
/
Copy pathprocessjunit.py
More file actions
61 lines (52 loc) · 2.64 KB
/
Copy pathprocessjunit.py
File metadata and controls
61 lines (52 loc) · 2.64 KB
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
import logging
from copy import deepcopy
from ast import literal_eval
from functools import cached_property, lru_cache
from pathlib import Path
from xml.etree import ElementTree
import shutil
class ProcessJUnit:
def __init__(self, new_report_xml_path: Path, tests_result_path: Path, tag: str, driver_type: str):
self.report_path = new_report_xml_path
self.tests_result_path = tests_result_path
self._summary = {"time": 0.0, "tests": 0, "errors": 0, "skipped": 0, "failures": 0}
self.tag = tag
self.driver_type = driver_type
@lru_cache(maxsize=None)
def _create_report(self):
if not self.tests_result_path.is_dir():
raise NotADirectoryError(f"The {self.tests_result_path} directory not exits")
new_tree = ElementTree.Element("testsuite")
is_first_run = True
for file_path in self.tests_result_path.glob("TEST-*.xml"):
tree = ElementTree.parse(file_path)
testsuite_element = next(tree.iter("testsuite"))
for key in self._summary:
self._summary[key] += literal_eval(testsuite_element.attrib[key].replace(',', ''))
if is_first_run:
is_first_run = False
properties_element = tree.find("properties")
if properties_element is not None:
new_tree.append(deepcopy(properties_element))
for testcase_parent_element in tree.iterfind("testcase"):
new_testcase_parent_element = deepcopy(testcase_parent_element)
attrib = new_testcase_parent_element.attrib
attrib['classname'] = f"{self.driver_type}.{self.tag}.{attrib['classname']}"
new_tree.append(new_testcase_parent_element)
new_tree.attrib["name"] = self.report_path.stem
new_tree.attrib.update({key: str(value) for key, value in self._summary.items()})
new_tree.attrib["time"] = f"{self._summary['time']:.3f}"
logging.info("Creating a new report file in '%s' path", self.report_path)
self.report_path.parent.mkdir(exist_ok=True)
with self.report_path.open(mode="w", encoding="utf-8") as file:
file.write(ElementTree.tostring(element=new_tree, encoding="utf-8").decode())
@cached_property
def summary(self):
self._create_report()
return self._summary
@cached_property
def is_failed(self) -> bool:
return not (self.summary["tests"] and self.summary["errors"] == self.summary["failures"] == 0)
def clear_original_reports(self):
logging.info("Removing all run's xml files of '%s' version", self.tag)
shutil.rmtree(self.tests_result_path)