Skip to content

Commit

Permalink
Set logging.log_path for each test class run.
Browse files Browse the repository at this point in the history
  • Loading branch information
xpconanfan committed Oct 23, 2019
1 parent 1bf70b6 commit 38be752
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions mobly/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,7 @@ def run(self, test_names=None):
Returns:
The test results object of this class.
"""
logging.log_path = self.log_path
# Executes pre-setup procedures, like generating test methods.
if not self._setup_generated_tests():
return self.results
Expand Down
9 changes: 9 additions & 0 deletions mobly/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def _setup_test_logger(log_path, prefix=None):
log.addHandler(fh_debug)
log.log_path = log_path
logging.log_path = log_path
logging.root_output_path = log_path


def kill_test_logger(logger):
Expand Down Expand Up @@ -241,6 +242,14 @@ def create_latest_log_alias(actual_path, alias):
def setup_test_logger(log_path, prefix=None, alias='latest'):
"""Customizes the root logger for a test run.
In addition to configuring the Mobly logging handlers, this also sets two
attributes on the `logging` module for the output directories:
root_output_path: path to the directory for the entire test run.
log_path: same as `root_output_path` outside of a test class run. In the
context of a test class run, this is the output directory for files
specific to a test class.
Args:
log_path: string, the location of the report file.
prefix: optional string, a prefix for each log line in terminal.
Expand Down
1 change: 1 addition & 0 deletions tests/mobly/base_test_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def test_func(self):
bt_cls = MockBaseTest(self.mock_test_cls_configs)
bt_cls.run(test_names=["test_func"])
self.assertEqual(path_checker.root_output_path, self.tmp_dir)
self.assertTrue(os.path.exists(path_checker.root_output_path))
expected_log_path = os.path.join(self.tmp_dir, 'MockBaseTest')
self.assertEqual(path_checker.log_path, expected_log_path)
self.assertTrue(os.path.exists(path_checker.log_path))
Expand Down
15 changes: 10 additions & 5 deletions tests/mobly/output_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class OutputTest(unittest.TestCase):
"""This test class has unit tests for the implementation of Mobly's output
files.
"""

def setUp(self):
self.tmp_dir = tempfile.mkdtemp()
self.base_mock_test_config = config_parser.TestRunConfig()
Expand Down Expand Up @@ -195,7 +196,7 @@ def test_logging_before_run(self):
tr.add_test_class(mock_test_config,
integration_test.IntegrationTest)
tr.run()
output_dir = logging.log_path
output_dir = logging.root_output_path
(summary_file_path, debug_log_path,
info_log_path) = self.assert_output_logs_exist(output_dir)
self.assert_log_contents(debug_log_path,
Expand All @@ -218,10 +219,10 @@ def test_run_twice_for_two_sets_of_logs(self, mock_timestamp):
tr.add_test_class(mock_test_config, integration_test.IntegrationTest)
with tr.mobly_logger():
tr.run()
output_dir1 = logging.log_path
output_dir1 = logging.root_output_path
with tr.mobly_logger():
tr.run()
output_dir2 = logging.log_path
output_dir2 = logging.root_output_path
self.assertNotEqual(output_dir1, output_dir2)
self.assert_output_logs_exist(output_dir1)
self.assert_output_logs_exist(output_dir2)
Expand Down Expand Up @@ -279,7 +280,11 @@ def test_basic_output(self):
tr.add_test_class(mock_test_config,
integration_test.IntegrationTest)
tr.run()
output_dir = logging.log_path
expected_class_path = os.path.join(logging.root_output_path,
'IntegrationTest')
self.assertEqual(expected_class_path, logging.log_path)
os.path.exists(logging.log_path)
output_dir = logging.root_output_path
(summary_file_path, debug_log_path,
info_log_path) = self.assert_output_logs_exist(output_dir)
summary_entries = []
Expand All @@ -303,7 +308,7 @@ def test_teardown_class_output(self):
mock_test_config,
teardown_class_failure_test.TearDownClassFailureTest)
tr.run()
output_dir = logging.log_path
output_dir = logging.root_output_path
summary_file_path = os.path.join(output_dir,
records.OUTPUT_FILE_SUMMARY)
found = False
Expand Down
3 changes: 2 additions & 1 deletion tests/mobly/test_runner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class TestRunnerTest(unittest.TestCase):
"""This test class has unit tests for the implementation of everything
under mobly.test_runner.
"""

def setUp(self):
self.tmp_dir = tempfile.mkdtemp()
self.base_mock_test_config = config_parser.TestRunConfig()
Expand Down Expand Up @@ -134,7 +135,7 @@ def test_summary_file_entries(self):
tr.add_test_class(mock_test_config,
integration_test.IntegrationTest)
tr.run()
summary_path = os.path.join(logging.log_path,
summary_path = os.path.join(logging.root_output_path,
records.OUTPUT_FILE_SUMMARY)
with io.open(summary_path, 'r', encoding='utf-8') as f:
summary_entries = list(yaml.safe_load_all(f))
Expand Down

0 comments on commit 38be752

Please sign in to comment.