1# 2# Copyright (C) 2018 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 17import logging 18 19from vts.runners.host import base_test 20from vts.runners.host import records 21from vts.runners.host import test_runner 22 23 24class ParamTestClass(base_test.BaseTestClass): 25 """Base class to run a parameterized test. 26 27 A parameterized test is a test with a set of parameters and the test will be 28 run against each parameter. This allows to test logic with different 29 parameters without without writing multiple copies of the same test. 30 31 An example use case of parameterized test is service name aware HAL testing 32 which we expect to run the same test logic against all service instances 33 through their corresponding service names. e.g to test graphics.composer HAL 34 against two different instance: default and vr. 35 36 Attributes: 37 params: list, a list of parameters for test run. 38 cur_param: the parameter used for the current run. 39 """ 40 41 def __init__(self, configs): 42 super(ParamTestClass, self).__init__(configs) 43 self.initParams() 44 45 def initParams(self): 46 """Initialize test parameters. Expected to be overridden by a subclass.""" 47 self._params = [] 48 49 def getParamTag(self, param): 50 """Get the test tag used to attach with test name from the parameter. 51 52 expected to be overridden by a subclass. 53 54 Args: 55 param: the current test parameter. 56 """ 57 return str(param) 58 59 @property 60 def params(self): 61 """Get params""" 62 return self._params 63 64 @params.setter 65 def params(self, params): 66 """Set params""" 67 self._params = params 68 69 @property 70 def cur_param(self): 71 """Get cur_param""" 72 return self._cur_param 73 74 @cur_param.setter 75 def cur_param(self, cur_param): 76 """Set cur_param""" 77 self._cur_param = cur_param 78 79 def run(self, test_names=None): 80 """Run a parameterized test. 81 82 For each parameter initialized for the test, runs test cases within 83 this test class against that parameter. 84 85 Args: 86 test_names: A list of string that are test case names requested in 87 cmd line. 88 89 Returns: 90 The test results object of this class. 91 """ 92 logging.info("==========> %s <==========", self.test_module_name) 93 original_tests = self.getTests(test_names) 94 # Run the set of original tests against each parameter. 95 for param in self.params: 96 self.cur_param = param 97 tests = [(str(test_name + self.getParamTag(param)), test_func) 98 for (test_name, test_func) in original_tests] 99 if not self.run_as_vts_self_test: 100 self.results.requested = [ 101 records.TestResultRecord(test_name, self.test_module_name) 102 for test_name, _ in tests 103 ] 104 self.runTestsWithRetry(tests) 105 return self.results 106