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# 18 19import uuid 20from dataclasses import dataclass 21from _core.report.suite_reporter import ResultCode 22 23__all__ = ["CaseResult", "SuiteResult", "SuitesResult", "StateRecorder"] 24 25 26@dataclass 27class CaseResult: 28 index = "" 29 code = ResultCode.FAILED.value 30 test_name = None 31 test_class = None 32 stacktrace = "" 33 run_time = 0 34 is_completed = False 35 num_tests = 0 36 current = 0 37 report = "" 38 result_content = "" 39 40 # hts参数 41 normal_screen_urls = "" 42 failure_screen_urls = "" 43 44 def is_running(self): 45 return self.test_name is not None and not self.is_completed 46 47 48@dataclass 49class SuiteResult: 50 index = "" 51 code = ResultCode.UNKNOWN.value 52 suite_name = None 53 test_num = 0 54 stacktrace = "" 55 run_time = 0 56 is_completed = False 57 is_started = False 58 suite_num = 0 59 report = "" 60 61 62@dataclass 63class SuitesResult: 64 index = "" 65 code = ResultCode.UNKNOWN.value 66 suites_name = None 67 test_num = 0 68 stacktrace = "" 69 run_time = 0 70 is_completed = False 71 product_info = {} 72 73 74@dataclass 75class StateRecorder: 76 current_suite = None 77 current_suites = None 78 current_test = None 79 trace_logs = [] 80 running_test_index = 0 81 82 def is_started(self): 83 return self.current_suite is not None 84 85 def suites_is_started(self): 86 return self.current_suites is not None 87 88 def suite_is_running(self): 89 suite = self.current_suite 90 return suite is not None and suite.suite_name is not None and \ 91 not suite.is_completed 92 93 def suites_is_running(self): 94 suites = self.current_suites 95 return suites is not None and suites.suites_name is not None and \ 96 not suites.is_completed 97 98 def test_is_running(self): 99 test = self.current_test 100 return test is not None and test.is_running() 101 102 def suite(self, reset=False): 103 if reset or not self.current_suite: 104 self.current_suite = SuiteResult() 105 self.current_suite.index = uuid.uuid4().hex 106 return self.current_suite 107 108 def get_suites(self, reset=False): 109 if reset or not self.current_suites: 110 self.current_suites = SuitesResult() 111 self.current_suites.index = uuid.uuid4().hex 112 return self.current_suites 113 114 def test(self, reset=False, test_index=None): 115 if reset or not self.current_test: 116 self.current_test = CaseResult() 117 if test_index: 118 self.current_test.index = test_index 119 else: 120 self.current_test.index = uuid.uuid4().hex 121 return self.current_test 122