1# Copyright 2016 Google Inc. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""This module is where all the test signal classes and related utilities live.""" 15 16import json 17 18 19class TestSignalError(Exception): 20 """Raised when an error occurs inside a test signal.""" 21 22 23class TestSignal(Exception): 24 """Base class for all test result control signals. This is used to signal 25 the result of a test. 26 27 Attributes: 28 details: A string that describes the reason for raising this signal. 29 extras: A json-serializable data type to convey extra information about 30 a test result. 31 """ 32 33 def __init__(self, details, extras=None): 34 super().__init__(details) 35 self.details = details 36 try: 37 json.dumps(extras) 38 self.extras = extras 39 except TypeError: 40 raise TestSignalError( 41 'Extras must be json serializable. %s is not.' % extras 42 ) 43 44 def __str__(self): 45 return 'Details=%s, Extras=%s' % (self.details, self.extras) 46 47 48class TestError(TestSignal): 49 """Raised when a test has an unexpected error.""" 50 51 52class TestFailure(TestSignal): 53 """Raised when a test has failed.""" 54 55 56class TestPass(TestSignal): 57 """Raised when a test has passed.""" 58 59 60class TestSkip(TestSignal): 61 """Raised when a test has been skipped.""" 62 63 64class TestAbortSignal(TestSignal): 65 """Base class for abort signals.""" 66 67 68class TestAbortClass(TestAbortSignal): 69 """Raised when all subsequent tests within the same test class should 70 be aborted. 71 """ 72 73 74class TestAbortAll(TestAbortSignal): 75 """Raised when all subsequent tests should be aborted.""" 76 77 78class ControllerError(Exception): 79 """Raised when an error occurred in controller classes.""" 80