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