def __init__(self, test_configs, run_list): """Constructor for TestRunner. During construction, the input config_parser.TestRunConfig object is processed and populated with information specific to a test run. The config object is later passed to each test class for execution. Args: test_configs: A config_parser.TestRunConfig object. run_list: A list of tuples specifying what tests to run. """ self.test_run_info = None self.test_configs = test_configs test_bed_name = self.test_configs.test_bed_name start_time = logger.get_log_file_timestamp() self.id = '%s@%s' % (test_bed_name, start_time) # log_path should be set before parsing configs. l_path = os.path.join(self.test_configs.log_path, test_bed_name, start_time) self.log_path = os.path.abspath(l_path) logger.setup_test_logger(self.log_path, test_bed_name) self.controller_registry = {} self.controller_destructors = {} self.run_list = run_list self.results = records.TestResult() self.running = False self.test_classes = {}
def test_setup_test_logger_creates_log_alias_with_custom_value( self, mock_create_latest_log_alias, mock__setup_test_logger): mock_alias = mock.MagicMock() logger.setup_test_logger(self.log_dir, alias=mock_alias) mock_create_latest_log_alias.assert_called_once_with(self.log_dir, alias=mock_alias)
def test_setup_test_logger_creates_log_alias(self, mock_create_latest_log_alias, mock__setup_test_logger): logger.setup_test_logger(self.log_dir) mock__setup_test_logger.assert_called_once_with(self.log_dir, None) mock_create_latest_log_alias.assert_called_once_with(self.log_dir, alias='latest')
def run(self): """Executes tests. This will instantiate controller and test classes, execute tests, and print a summary. Raises: Error: if no tests have previously been added to this runner using add_test_class(...). """ if not self._test_run_infos: raise Error('No tests to execute.') start_time = logger.get_log_file_timestamp() log_path = os.path.join(self._log_dir, self._test_bed_name, start_time) summary_writer = records.TestSummaryWriter( os.path.join(log_path, records.OUTPUT_FILE_SUMMARY)) logger.setup_test_logger(log_path, self._test_bed_name) try: for test_run_info in self._test_run_infos: # Set up the test-specific config test_config = test_run_info.config.copy() test_config.log_path = log_path test_config.register_controller = functools.partial( self._register_controller, test_config) test_config.summary_writer = summary_writer try: self._run_test_class(test_config, test_run_info.test_class, test_run_info.tests) except signals.TestAbortAll as e: logging.warning( 'Abort all subsequent test classes. Reason: %s', e) raise finally: self._unregister_controllers() finally: # Write controller info and summary to summary file. summary_writer.dump(self.results.controller_info, records.TestSummaryEntryType.CONTROLLER_INFO) summary_writer.dump(self.results.summary_dict(), records.TestSummaryEntryType.SUMMARY) # Stop and show summary. msg = '\nSummary for test run %s@%s: %s\n' % ( self._test_bed_name, start_time, self.results.summary_str()) self._write_results_json_str(log_path) logging.info(msg.strip()) logger.kill_test_logger(logging.getLogger())
def mobly_logger(self, alias='latest'): """Starts and stops a logging context for a Mobly test run. Args: alias: optional string, the name of the latest log alias directory to create. If a falsy value is specified, then the directory will not be created. Yields: The host file path where the logs for the test run are stored. """ self._update_log_path() logger.setup_test_logger(self._log_path, self._test_bed_name, alias=alias) try: yield self._log_path finally: logger.kill_test_logger(logging.getLogger())
def __init__(self, test_configs, run_list): self.test_run_info = {} self.test_configs = test_configs self.testbed_configs = self.test_configs[keys.Config.key_testbed.value] self.testbed_name = self.testbed_configs[ keys.Config.key_testbed_name.value] start_time = logger.get_log_file_timestamp() self.id = "{}@{}".format(self.testbed_name, start_time) # log_path should be set before parsing configs. l_path = os.path.join( self.test_configs[keys.Config.key_log_path.value], self.testbed_name, start_time) self.log_path = os.path.abspath(l_path) logger.setup_test_logger(self.log_path, self.testbed_name) self.log = logging.getLogger() self.controller_registry = {} self.controller_destructors = {} self.run_list = run_list self.results = records.TestResult() self.running = False
def mobly_logger(self, alias='latest'): """Starts and stops a logging context for a Mobly test run. Args: alias: optional string, the name of the latest log alias directory to create. If a falsy value is specified, then the directory will not be created. Yields: The host file path where the logs for the test run are stored. """ # Refresh the log path at the beginning of the logger context. root_output_path = self._test_run_metadata.generate_test_run_log_path() logger.setup_test_logger(root_output_path, self._testbed_name, alias=alias) try: yield self._test_run_metadata.root_output_path finally: logger.kill_test_logger(logging.getLogger())
def setup_logger(self): """Sets up logging for the next test run. This is called automatically in 'run', so normally, this method doesn't need to be called. Only use this method if you want to use Mobly's logger before the test run starts. .. code-block:: python tr = TestRunner(...) tr.setup_logger() logging.info(...) tr.run() """ if self._log_path is not None: return self._start_time = logger.get_log_file_timestamp() self._log_path = os.path.join(self._log_dir, self._test_bed_name, self._start_time) logger.setup_test_logger(self._log_path, self._test_bed_name)