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""" 16 17import json 18 19 20class TestSignalError(Exception): 21 """Raised when an error occurs inside a test signal.""" 22 23 24class TestSignal(Exception): 25 """Base class for all test result control signals. This is used to signal 26 the result of a test. 27 28 Attributes: 29 details: A string that describes the reason for raising this signal. 30 extras: A json-serializable data type to convey extra information about 31 a test result. 32 """ 33 34 def __init__(self, details, extras=None): 35 super().__init__(details) 36 self.details = details 37 try: 38 json.dumps(extras) 39 self.extras = extras 40 except TypeError: 41 raise TestSignalError('Extras must be json serializable. %s ' 42 'is not.' % extras) 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 68 69class TestAbortClass(TestAbortSignal): 70 """Raised when all subsequent tests within the same test class should 71 be aborted. 72 """ 73 74 75class TestAbortAll(TestAbortSignal): 76 """Raised when all subsequent tests should be aborted.""" 77 78 79class ControllerError(Exception): 80 """Raised when an error occurred in controller classes.""" 81