• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# Copyright (C) 2016 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16"""This module is where all the test signal classes and related utilities live.
17"""
18
19import functools
20import json
21
22
23def GeneratedTest(func):
24    """A decorator used to suppress result reporting for the test case that
25    kicks off a group of generated test cases.
26
27    Returns:
28        What the decorated function returns.
29    """
30
31    @functools.wraps(func)
32    def wrapper(*args, **kwargs):
33        func(*args, **kwargs)
34        raise TestSilent("Result reporting for %s is suppressed" %
35                         func.__name__)
36
37    return wrapper
38
39
40class TestSignalError(Exception):
41    """Raised when an error occurs inside a test signal."""
42
43
44class TestSignal(Exception):
45    """Base class for all test result control signals."""
46
47    def __init__(self, details, extras=None):
48        if not isinstance(details, str):
49            raise TestSignalError("Message has to be a string.")
50        super(TestSignal, self).__init__(details)
51        self.details = details
52        try:
53            json.dumps(extras)
54            self.extras = extras
55        except TypeError:
56            raise TestSignalError(("Extras must be json serializable. %s "
57                                   "is not.") % extras)
58
59    def __str__(self):
60        return "Details=%s, Extras=%s" % (self.details, self.extras)
61
62
63class TestFailure(TestSignal):
64    """Raised when a test has failed."""
65
66
67class TestPass(TestSignal):
68    """Raised when a test has passed."""
69
70
71class TestSkip(TestSignal):
72    """Raised when a test has been skipped."""
73
74
75class TestSilent(TestSignal):
76    """Raised when a test should not be reported. This should only be used for
77    generated test cases.
78    """
79
80
81class TestAbortClass(TestSignal):
82    """Raised when all subsequent test cases within the same test class should
83    be aborted.
84    """
85
86
87class TestAbortAll(TestSignal):
88    """Raised when all subsequent test cases should be aborted."""
89
90
91class ControllerError(Exception):
92    """Raised when an error occured in controller classes."""
93