Skip to content

Commit

Permalink
support customized suite name
Browse files Browse the repository at this point in the history
  • Loading branch information
mhaoli committed Dec 20, 2024
1 parent ce91462 commit 929cfe8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
12 changes: 12 additions & 0 deletions mobly/base_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ def set_test_selector(self, test_selector):
"""
self._test_selector = test_selector

def get_suite_name(self):
"""The name of this suite.
By default, use the name of the test class. User can overwrite to return
a customized suite name. User can include test runtime info as this will be
collected after all test classes are executed.
Returns:
A string of suite name.
"""
return self.__class__.__name__

def get_suite_info(self):
"""User defined extra suite information to be recorded in test summary.
Expand Down
19 changes: 15 additions & 4 deletions mobly/suite_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,26 @@ class TestSummaryEntryType(enum.Enum):
class SuiteInfoRecord:
"""A record representing the test suite info in test summary."""

KEY_SUITE_NAME = 'Suite Name'
KEY_TEST_SUITE_CLASS = 'Test Suite Class'
KEY_EXTRAS = 'Extras'
KEY_BEGIN_TIME = 'Suite Begin Time'
KEY_END_TIME = 'Suite End Time'

# The name of the test suite.
_suite_name: str
# The class name of the test suite class.
_test_suite_class: str
# Epoch timestamp of when the suite started.
_begin_time: int
# Epoch timestamp of when the suite ended.
_end_time: int
# User defined extra information of the test result. Must be serializable.
_extras: dict
# Epoch timestamp of when the suite started.
_begin_time: int | None
# Epoch timestamp of when the suite ended.
_end_time: int | None

def __init__(self, test_suite_class):
self._test_suite_class = test_suite_class
self._suite_name = ''
self._extras = dict()
self._begin_time = None
self._end_time = None
Expand All @@ -122,12 +126,18 @@ def suite_end(self):
"""Call this when the suite ends execution."""
self._end_time = utils.get_current_epoch_time()

def set_suite_name(self, suite_name):
"""Sets the name of the test suite."""
self._suite_name = suite_name

def set_extras(self, extras):
"""Sets extra information. Must be serializable."""
self._extras = extras

def to_dict(self):
result = {}
result[self.KEY_TEST_SUITE_CLASS] = self._test_suite_class
result[self.KEY_SUITE_NAME] = self._suite_name
result[self.KEY_EXTRAS] = self._extras
result[self.KEY_BEGIN_TIME] = self._begin_time
result[self.KEY_END_TIME] = self._end_time
Expand Down Expand Up @@ -334,6 +344,7 @@ def run_suite_class(argv=None):
finally:
suite.teardown_suite()
suite_record.suite_end()
suite_record.set_suite_name(suite.get_suite_name())
suite_record.set_extras(suite.get_suite_info())
_dump_suite_info(suite_record, log_path)
if not ok:
Expand Down
18 changes: 17 additions & 1 deletion tests/mobly/suite_runner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ def test_run_suite_class_finds_suite_class_when_not_in_main_module(
):
mock_test_runner = mock_test_runner_class.return_value
mock_test_runner.results.is_all_pass = True
mock_test_runner
tmp_file_path = self._gen_tmp_config_file()
mock_cli_args = ['test_binary', f'--config={tmp_file_path}']

Expand All @@ -275,19 +274,36 @@ def test_run_suite_class_finds_suite_class_when_not_in_main_module(
)
def test_run_suite_class_records_suite_info(self, mock_time, _):
tmp_file_path = self._gen_tmp_config_file()
customized_suite_name = 'Customized Suite Name'
mock_cli_args = ['test_binary', f'--config={tmp_file_path}']
expected_record = suite_runner.SuiteInfoRecord(
test_suite_class='FakeTestSuite'
)
expected_record.suite_begin()
expected_record.suite_end()
expected_record.set_suite_name(customized_suite_name)
expected_record.set_extras(
{
'extra-key-0': 'extra-value-0',
'extra-key-1': 'extra-value-1',
}
)
expected_summary_entry = expected_record.to_dict()
expected_summary_entry['Type'] = (
suite_runner.TestSummaryEntryType.SUITE_INFO.value
)

class FakeTestSuite(base_suite.BaseSuite):

def get_suite_name(self):
return customized_suite_name

def get_suite_info(self):
return {
'extra-key-0': 'extra-value-0',
'extra-key-1': 'extra-value-1',
}

def setup_suite(self, config):
super().setup_suite(config)
self.add_test_class(FakeTest1)
Expand Down

0 comments on commit 929cfe8

Please sign in to comment.