1# Copyright 2022 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 15import abc 16 17class BaseSuite(abc.ABC): 18 """Class used to define a Mobly suite. 19 20 To create a suite, inherit from this class and implement setup_suite. 21 22 Use `BaseSuite.add_test_class` to specify which classes to run with which 23 configs and test selectors. 24 25 After defining the sub class, the suite can be executed using 26 suite_runner.run_suite_class. 27 28 Users can use this class if they need to define their own setup and teardown 29 steps on the suite level. Otherwise, just use suite_runner.run_suite on the 30 list of test classes. 31 """ 32 33 def __init__(self, runner, config): 34 self._runner = runner 35 self._config = config.copy() 36 37 @property 38 def user_params(self): 39 return self._config.user_params 40 41 def add_test_class(self, clazz, config=None, tests=None, name_suffix=None): 42 """Adds a test class to the suite. 43 44 Args: 45 clazz: class, a Mobly test class. 46 config: config_parser.TestRunConfig, the config to run the class with. If 47 not specified, the default config passed from google3 infra is used. 48 tests: list of strings, names of the tests to run in this test class, in 49 the execution order. If not specified, all tests in the class are 50 executed. 51 name_suffix: string, suffix to append to the class name for reporting. 52 This is used for differentiating the same class executed with different 53 parameters in a suite. 54 """ 55 if not config: 56 config = self._config 57 self._runner.add_test_class(config, clazz, tests, name_suffix) 58 59 @abc.abstractmethod 60 def setup_suite(self, config): 61 """Function used to add test classes, has to be implemented by child class. 62 63 Args: 64 config: config_parser.TestRunConfig, the config provided by google3 infra. 65 66 Raises: 67 Error: when setup_suite is not implemented by child class. 68 """ 69 pass 70 71 def teardown_suite(self): 72 """Function used to add post tests cleanup tasks (optional).""" 73 pass 74