1#!/usr/bin/env python3 2# coding=utf-8 3 4# 5# Copyright (c) 2020-2023 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18from _core.report.reporter_helper import ReportConstant 19from _core.report.reporter_helper import DataHelper 20from _core.context.center import Context 21 22 23class RepeatHelper: 24 def __init__(self, report_path): 25 self.data_helper = DataHelper() 26 self.report_path = report_path 27 28 def __generate_repeat_xml__(self, summary_data_path): 29 if Context.get_scheduler() and\ 30 Context.get_scheduler().get_repeat_index() <= 1: 31 return 32 root_tree = self.data_helper.parse_data_report(summary_data_path) 33 modules = dict() 34 name_set = set() 35 for suite in root_tree: 36 module_name = suite.attrib.get(ReportConstant.module_name, "") 37 if not module_name: 38 continue 39 name_set.add(module_name) 40 name_in_suite = suite.attrib.get(ReportConstant.name, "") 41 uuid = "{}#{}".format(module_name, name_in_suite) 42 total = int(suite.attrib.get(ReportConstant.tests, 0)) 43 if total == 0: 44 continue 45 if uuid not in modules.keys(): 46 modules[uuid] = suite 47 continue 48 self._update_suite(modules, suite, uuid) 49 50 root_tree = self._update_root_tree(modules, root_tree) 51 root_tree.attrib.update({ReportConstant.modules: str(len(name_set))}) 52 root_tree.attrib.update({ReportConstant.run_modules: str(len(name_set))}) 53 file_name = r"{}\repeated.xml".format(self.report_path) 54 self.data_helper.generate_report(root_tree, file_name) 55 56 @classmethod 57 def _update_root_tree(cls, modules, root_tree): 58 for item in root_tree.findall(ReportConstant.test_suite): 59 root_tree.remove(item) 60 need_update_attributes = \ 61 [ReportConstant.tests, ReportConstant.ignored, 62 ReportConstant.failures, ReportConstant.disabled, 63 ReportConstant.errors] 64 root_tree.attrib.update({ReportConstant.tests: "0"}) 65 root_tree.attrib.update({ReportConstant.unavailable: "0"}) 66 for _, test_suite in modules.items(): 67 for update_attribute in need_update_attributes: 68 value = int(test_suite.attrib.get(update_attribute, 0)) 69 value = int(root_tree.attrib.get(update_attribute, 0)) + value 70 root_tree.attrib.update({update_attribute: str(value)}) 71 root_tree.append(test_suite) 72 return root_tree 73 74 def _update_suite(self, modules, suite, uuid): 75 76 for testcase in suite: 77 name = testcase.attrib.get(ReportConstant.name, "") 78 class_name = testcase.attrib.get(ReportConstant.class_name, "") 79 pattern = ".//{}[@{}='{}'][@{}='{}']".format( 80 ReportConstant.test_case, ReportConstant.class_name, class_name, 81 ReportConstant.name, name) 82 matched_case = modules[uuid].find(pattern) 83 if matched_case is None: 84 modules[uuid].append(testcase) 85 tests = int( 86 modules[uuid].attrib.get(ReportConstant.tests, 0)) + 1 87 modules[uuid].attrib[ReportConstant.tests] = str(tests) 88 status = self._need_update_status(testcase) 89 if status: 90 value = int(modules[uuid].attrib.get( 91 ReportConstant.status, 0)) + 1 92 modules[uuid].attrib[status] = str(value) 93 continue 94 if testcase.attrib.get(ReportConstant.result, 95 ReportConstant.false) == ReportConstant.true: 96 modules[uuid].remove(matched_case) 97 modules[uuid].append(testcase) 98 status = self._need_update_status(testcase) 99 if status: 100 value = int(modules[uuid].attrib.get( 101 ReportConstant.status, 0)) - 1 102 modules[uuid].attrib[status] = str(value) 103 104 @classmethod 105 def _need_update_status(cls, testcase): 106 status = testcase.attrib.get(ReportConstant.status, "") 107 result = testcase.attrib.get(ReportConstant.result, "") 108 if result == ReportConstant.true: 109 return "" 110 if status == ReportConstant.run: 111 return ReportConstant.failures 112 elif status == ReportConstant.skip: 113 return ReportConstant.ignored 114 else: 115 return ReportConstant.disabled 116