1#!/usr/bin/env python3 2# 3# Copyright 2020 - 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 17import statistics 18 19from acts.base_test import BaseTestClass 20from acts.test_decorators import repeated_test 21from acts import signals 22 23 24def get_median_current(test_results, _): 25 """Returns the median current, or a failure if the test failed.""" 26 # If the last run was not a pass signal, the test exceeded 27 # acceptable_failures. 28 if not isinstance(test_results[-1], signals.TestPass): 29 return test_results[-1] 30 31 # Only look at results within the good range (i.e., passing results). 32 valid_results = filter(lambda result: isinstance(result, signals.TestPass), 33 test_results) 34 35 # Gather the current measurements and return the median. 36 median_current = statistics.median( 37 map(lambda result: result.extras['current'], valid_results)) 38 return signals.TestPass('Pass msg! Current: %s' % median_current, 39 extras={'current': median_current}) 40 41 42class RepeatedTest(BaseTestClass): 43 44 @repeated_test(num_passes=3, acceptable_failures=0) 45 def test_repeated_case(self, _): 46 self.log.info('This logic executes three times.') 47 48 @repeated_test(num_passes=3, acceptable_failures=2, 49 result_selector=get_median_current) 50 def test_repeated_case_pass(self, attempt_number): 51 """The end result of this test is a pass with current=3.5""" 52 returned_results = [ 53 signals.TestPass('0Pass msg!', extras={'current': 3.5}), 54 signals.TestFailure('Fail msg!', extras={'current': 100.0}), 55 signals.TestPass('1Pass msg!', extras={'current': 3.2}), 56 signals.TestPass('2Pass msg!', extras={'current': 3.6}) 57 ] 58 raise returned_results[attempt_number - 1] 59 60 @repeated_test(num_passes=3, acceptable_failures=2, 61 result_selector=get_median_current) 62 def test_repeated_case_with_failures(self, attempt_number): 63 """The end result of this test is the last failure to occur.""" 64 returned_results = [ 65 signals.TestPass('Pass msg!', extras={'current': 3.5}), 66 signals.TestFailure('Fail msg!', extras={'current': 100.0}), 67 signals.TestFailure('Fail msg!', extras={'current': 58.1}), 68 signals.TestFailure('Fail msg!', extras={'current': 74.2}), 69 ] 70 raise returned_results[(attempt_number - 1) % 4] 71