1#!/usr/bin/env python 2# 3# Copyright 2019, The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17"""Unittest for atest_execution_info.""" 18 19import time 20import unittest 21 22from test_runners import test_runner_base 23import atest_execution_info as aei 24import result_reporter 25 26RESULT_TEST_TEMPLATE = test_runner_base.TestResult( 27 runner_name='someRunner', 28 group_name='someModule', 29 test_name='someClassName#sostName', 30 status=test_runner_base.PASSED_STATUS, 31 details=None, 32 test_count=1, 33 test_time='(10ms)', 34 runner_total=None, 35 group_total=2, 36 additional_info={}, 37 test_run_name='com.android.UnitTests' 38) 39 40# pylint: disable=protected-access 41class AtestRunInfoUnittests(unittest.TestCase): 42 """Unit tests for atest_execution_info.py""" 43 44 def test_arrange_test_result_one_module(self): 45 """Test _arrange_test_result method with only one module.""" 46 pass_1 = self._create_test_result(status=test_runner_base.PASSED_STATUS) 47 pass_2 = self._create_test_result(status=test_runner_base.PASSED_STATUS) 48 pass_3 = self._create_test_result(status=test_runner_base.PASSED_STATUS) 49 fail_1 = self._create_test_result(status=test_runner_base.FAILED_STATUS) 50 fail_2 = self._create_test_result(status=test_runner_base.FAILED_STATUS) 51 ignore_1 = self._create_test_result(status=test_runner_base.IGNORED_STATUS) 52 reporter_1 = result_reporter.ResultReporter() 53 reporter_1.all_test_results.extend([pass_1, pass_2, pass_3]) 54 reporter_2 = result_reporter.ResultReporter() 55 reporter_2.all_test_results.extend([fail_1, fail_2, ignore_1]) 56 info_dict = {} 57 aei.AtestExecutionInfo._arrange_test_result(info_dict, [reporter_1, reporter_2]) 58 expect_summary = {aei._STATUS_IGNORED_KEY : 1, 59 aei._STATUS_FAILED_KEY : 2, 60 aei._STATUS_PASSED_KEY : 3} 61 self.assertEqual(expect_summary, info_dict[aei._TOTAL_SUMMARY_KEY]) 62 63 def test_arrange_test_result_multi_module(self): 64 """Test _arrange_test_result method with multi module.""" 65 group_a_pass_1 = self._create_test_result(group_name='grpup_a', 66 status=test_runner_base.PASSED_STATUS) 67 group_b_pass_1 = self._create_test_result(group_name='grpup_b', 68 status=test_runner_base.PASSED_STATUS) 69 group_c_pass_1 = self._create_test_result(group_name='grpup_c', 70 status=test_runner_base.PASSED_STATUS) 71 group_b_fail_1 = self._create_test_result(group_name='grpup_b', 72 status=test_runner_base.FAILED_STATUS) 73 group_c_fail_1 = self._create_test_result(group_name='grpup_c', 74 status=test_runner_base.FAILED_STATUS) 75 group_c_ignore_1 = self._create_test_result(group_name='grpup_c', 76 status=test_runner_base.IGNORED_STATUS) 77 reporter_1 = result_reporter.ResultReporter() 78 reporter_1.all_test_results.extend([group_a_pass_1, group_b_pass_1, group_c_pass_1]) 79 reporter_2 = result_reporter.ResultReporter() 80 reporter_2.all_test_results.extend([group_b_fail_1, group_c_fail_1, group_c_ignore_1]) 81 82 info_dict = {} 83 aei.AtestExecutionInfo._arrange_test_result(info_dict, [reporter_1, reporter_2]) 84 expect_group_a_summary = {aei._STATUS_IGNORED_KEY : 0, 85 aei._STATUS_FAILED_KEY : 0, 86 aei._STATUS_PASSED_KEY : 1} 87 self.assertEqual( 88 expect_group_a_summary, 89 info_dict[aei._TEST_RUNNER_KEY]['someRunner']['grpup_a'][aei._SUMMARY_KEY]) 90 91 expect_group_b_summary = {aei._STATUS_IGNORED_KEY : 0, 92 aei._STATUS_FAILED_KEY : 1, 93 aei._STATUS_PASSED_KEY : 1} 94 self.assertEqual( 95 expect_group_b_summary, 96 info_dict[aei._TEST_RUNNER_KEY]['someRunner']['grpup_b'][aei._SUMMARY_KEY]) 97 98 expect_group_c_summary = {aei._STATUS_IGNORED_KEY : 1, 99 aei._STATUS_FAILED_KEY : 1, 100 aei._STATUS_PASSED_KEY : 1} 101 self.assertEqual( 102 expect_group_c_summary, 103 info_dict[aei._TEST_RUNNER_KEY]['someRunner']['grpup_c'][aei._SUMMARY_KEY]) 104 105 expect_total_summary = {aei._STATUS_IGNORED_KEY : 1, 106 aei._STATUS_FAILED_KEY : 2, 107 aei._STATUS_PASSED_KEY : 3} 108 self.assertEqual(expect_total_summary, info_dict[aei._TOTAL_SUMMARY_KEY]) 109 110 def test_preparation_time(self): 111 """Test preparation_time method.""" 112 start_time = time.time() 113 aei.PREPARE_END_TIME = None 114 self.assertTrue(aei.preparation_time(start_time) is None) 115 aei.PREPARE_END_TIME = time.time() 116 self.assertFalse(aei.preparation_time(start_time) is None) 117 118 def test_arrange_test_result_multi_runner(self): 119 """Test _arrange_test_result method with multi runner.""" 120 runner_a_pass_1 = self._create_test_result(runner_name='runner_a', 121 status=test_runner_base.PASSED_STATUS) 122 runner_a_pass_2 = self._create_test_result(runner_name='runner_a', 123 status=test_runner_base.PASSED_STATUS) 124 runner_a_pass_3 = self._create_test_result(runner_name='runner_a', 125 status=test_runner_base.PASSED_STATUS) 126 runner_b_fail_1 = self._create_test_result(runner_name='runner_b', 127 status=test_runner_base.FAILED_STATUS) 128 runner_b_fail_2 = self._create_test_result(runner_name='runner_b', 129 status=test_runner_base.FAILED_STATUS) 130 runner_b_ignore_1 = self._create_test_result(runner_name='runner_b', 131 status=test_runner_base.IGNORED_STATUS) 132 133 reporter_1 = result_reporter.ResultReporter() 134 reporter_1.all_test_results.extend([runner_a_pass_1, runner_a_pass_2, runner_a_pass_3]) 135 reporter_2 = result_reporter.ResultReporter() 136 reporter_2.all_test_results.extend([runner_b_fail_1, runner_b_fail_2, runner_b_ignore_1]) 137 info_dict = {} 138 aei.AtestExecutionInfo._arrange_test_result(info_dict, [reporter_1, reporter_2]) 139 expect_group_a_summary = {aei._STATUS_IGNORED_KEY : 0, 140 aei._STATUS_FAILED_KEY : 0, 141 aei._STATUS_PASSED_KEY : 3} 142 self.assertEqual( 143 expect_group_a_summary, 144 info_dict[aei._TEST_RUNNER_KEY]['runner_a']['someModule'][aei._SUMMARY_KEY]) 145 146 expect_group_b_summary = {aei._STATUS_IGNORED_KEY : 1, 147 aei._STATUS_FAILED_KEY : 2, 148 aei._STATUS_PASSED_KEY : 0} 149 self.assertEqual( 150 expect_group_b_summary, 151 info_dict[aei._TEST_RUNNER_KEY]['runner_b']['someModule'][aei._SUMMARY_KEY]) 152 153 expect_total_summary = {aei._STATUS_IGNORED_KEY : 1, 154 aei._STATUS_FAILED_KEY : 2, 155 aei._STATUS_PASSED_KEY : 3} 156 self.assertEqual(expect_total_summary, info_dict[aei._TOTAL_SUMMARY_KEY]) 157 158 def _create_test_result(self, **kwargs): 159 """A Helper to create TestResult""" 160 test_info = test_runner_base.TestResult(**RESULT_TEST_TEMPLATE._asdict()) 161 return test_info._replace(**kwargs) 162 163if __name__ == "__main__": 164 unittest.main() 165