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