• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3
4#
5# Copyright (c) 2020-2022 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
19from xdevice import Plugin
20from xdevice import IListener
21from xdevice import LifeCycle
22from xdevice import platform_logger
23from xdevice import ListenerType
24from xdevice import TestDescription
25from xdevice import ResultCode
26
27__all__ = ["CollectingLiteGTestListener", "CollectingPassListener"]
28
29LOG = platform_logger("Listener")
30
31
32@Plugin(type=Plugin.LISTENER, id=ListenerType.collect_lite)
33class CollectingLiteGTestListener(IListener):
34    """
35    Listener test status information to the console
36    """
37
38    def __init__(self):
39        self.tests = []
40
41    def __started__(self, lifecycle, test_result):
42        if lifecycle == LifeCycle.TestCase:
43            if not test_result.test_class or not test_result.test_name:
44                return
45            test = TestDescription(test_result.test_class,
46                                   test_result.test_name)
47            if test not in self.tests:
48                self.tests.append(test)
49
50    def __ended__(self, lifecycle, test_result=None, **kwargs):
51        pass
52
53    def __skipped__(self, lifecycle, test_result):
54        pass
55
56    def __failed__(self, lifecycle, test_result):
57        if lifecycle == LifeCycle.TestCase:
58            if not test_result.test_class or not test_result.test_name:
59                return
60            test = TestDescription(test_result.test_class,
61                                   test_result.test_name)
62            if test not in self.tests:
63                self.tests.append(test)
64
65    def get_current_run_results(self):
66        return self.tests
67
68
69@Plugin(type=Plugin.LISTENER, id=ListenerType.collect_pass)
70class CollectingPassListener(IListener):
71    """
72    listener test status information to the console
73    """
74
75    def __init__(self):
76        self.tests = []
77
78    def __started__(self, lifecycle, test_result):
79        pass
80
81    def __ended__(self, lifecycle, test_result=None, **kwargs):
82        if lifecycle == LifeCycle.TestCase:
83            if not test_result.test_class or not test_result.test_name:
84                return
85            if test_result.code != ResultCode.PASSED.value:
86                return
87            test = TestDescription(test_result.test_class,
88                                   test_result.test_name)
89            if test not in self.tests:
90                self.tests.append(test)
91            else:
92                LOG.warning("Duplicate testcase: %s#%s" % (
93                    test_result.test_class, test_result.test_name))
94
95    def __skipped__(self, lifecycle, test_result):
96        pass
97
98    def __failed__(self, lifecycle, test_result):
99        pass
100
101    def get_current_run_results(self):
102        return self.tests