import unittest from unittest.mock import patch, call, ANY import kpi class InitKpiMetricsTestCase(unittest.TestCase): """ Class to test init_kpi_metrics method """ @patch("time.time") def test_init_kpi_metrics(self, mock_time): """ Test init_kpi_metrics method """ # Setup mock_time.return_value = 12345.67 # Run kpi.init_kpi_metrics() # Assert self.assertEqual(kpi.START, 12345.67) class GetFormatedTimeTestCase(unittest.TestCase): """ Class to test get_formated_time method """ def test_get_formated_time_hours(self): """ Test get_formated_time method with hours """ # Setup sec = 3665.67 # Run result = kpi.get_formated_time(sec) # Assert self.assertEqual(result, "1h 1m 5.67s") def test_get_formated_time_minutes(self): """ Test get_formated_time method with minutes """ # Setup sec = 65.67 # Run result = kpi.get_formated_time(sec) # Assert self.assertEqual(result, "1m 5.67s") def test_get_formated_time_seconds(self): """ Test get_formated_time method with seconds """ # Setup sec = 5.67 # Run result = kpi.get_formated_time(sec) # Assert self.assertEqual(result, "5.67s") class SetKpiMetricTestCase(unittest.TestCase): """ Class to test set_kpi_metric method """ def setUp(self): kpi.METRICS = {} kpi.STAGES = [] def test_set_kpi_metric(self): """ Test set_kpi_metric method """ # Setup metric = "some_metric" duration = 123.45 # Run kpi.set_kpi_metric(metric, duration) # Assert self.assertEqual(kpi.METRICS[metric], duration) self.assertIn(metric, kpi.STAGES) class PrintKpiTestCase(unittest.TestCase): """ Class to test print_kpi method """ @patch("kpi.LOG") @patch("kpi.get_formated_time", return_value="1m 23.45s") def test_print_kpi_metric(self, mock_get_formated_time, mock_log): """ Test print_kpi method with a metric """ # Setup kpi.STAGES = ["some_metric"] kpi.METRICS = {"some_metric": 123.45} metric = "some_metric" # Run kpi.print_kpi(metric) # Assert mock_get_formated_time.assert_called_once_with(123.45) mock_log.info.assert_called_once_with(" Time in stage '%s': %s ", metric, "1m 23.45s") @patch("kpi.LOG") @patch("kpi.get_formated_time", return_value="2m 46.90s") @patch("time.time") def test_print_kpi_total(self, mock_time, mock_get_formated_time, mock_log): """ Test print_kpi method with total """ # Setup kpi.START = 20 metric = "total" mock_time.return_value = 166.90 # Run kpi.print_kpi(metric) # Assert mock_get_formated_time.assert_called_once_with(146.9) mock_log.info.assert_called_once_with(" Total time: %s", "2m 46.90s") class GetKpiStrTestCase(unittest.TestCase): """ Class to test get_kpi_str method """ @patch("kpi.get_formated_time", return_value="1m 23.45s") def test_get_kpi_str_metric(self, mock_get_formated_time): """ Test get_kpi_str method with a metric """ # Setup kpi.STAGES = ["some_metric"] kpi.METRICS = {"some_metric": 123.45} metric = "some_metric" # Run result = kpi.get_kpi_str(metric) # Assert mock_get_formated_time.assert_called_once_with(123.45) self.assertEqual(result, " Time in stage 'some_metric': 1m 23.45s \n") @patch("kpi.get_formated_time", return_value="2m 46.90s") @patch("time.time") def test_get_kpi_str_total(self, mock_time, mock_get_formated_time): """ Test get_kpi_str method with total """ # Setup kpi.START = 20 metric = "total" mock_time.return_value = 166.90 # Run result = kpi.get_kpi_str(metric) # Assert mock_get_formated_time.assert_called_once_with(146.9) self.assertEqual(result, " Total time: 2m 46.90s\n") class GetKpiMetricsStrTestCase(unittest.TestCase): """ Class to test get_kpi_metrics_str method """ @patch("kpi.get_kpi_str") def test_get_kpi_metrics_str(self, mock_get_kpi_str): """ Test get_kpi_metrics_str method """ # Setup kpi.STAGES = ["metric1", "metric2"] kpi.METRICS = {"metric1": 123.45, "metric2": 166.9} kpi.START = 20 mock_get_kpi_str.side_effect = [" Time in stage 'metric1': 1m 23.45s \n", " Time in stage 'metric2': 2m 46.90s \n", " Total time: 4m 10.35s\n"] # Run result = kpi.get_kpi_metrics_str() # Assert expected_result = ("===================== Metrics ====================\n" " Time in stage 'metric1': 1m 23.45s \n" " Time in stage 'metric2': 2m 46.90s \n" " Total time: 4m 10.35s\n" "===============================================\n") self.assertEqual(result, expected_result) class PrintKpiMetricsTestCase(unittest.TestCase): """ Class to test print_kpi_metrics method """ @patch("serial.LOG.info") @patch("kpi.print_kpi") def test_print_kpi_metrics(self, mock_print_kpi, mock_log_info): """ Test print_kpi_metrics method """ # Setup kpi.STAGES = ["metric1", "metric2"] kpi.METRICS = {"metric1": 123.45, "metric2": 166.9} kpi.START = 20 # Run kpi.print_kpi_metrics() # Assert calls = [call("metric1"), call("metric2"), call('total')] mock_print_kpi.assert_has_calls(calls) mock_log_info.assert_any_call(ANY) if __name__ == '__main__': unittest.main()