1#!/usr/bin/python 2# 3# Copyright 2018 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 itertools 18import unittest 19 20import net_test 21import util 22 23 24def InjectTests(): 25 ParmeterizationTest.InjectTests() 26 27 28# This test class ensures that the Parameterized Test generator in utils.py 29# works properly. It injects test methods into itself, and ensures that they 30# are generated as expected, and that the TestClosures being run are properly 31# defined, and running different parameterized tests each time. 32class ParmeterizationTest(net_test.NetworkTest): 33 tests_run_list = [] 34 35 @staticmethod 36 def NameGenerator(a, b, c): 37 return str(a) + "_" + str(b) + "_" + str(c) 38 39 @classmethod 40 def InjectTests(cls): 41 PARAMS_A = (1, 2) 42 PARAMS_B = (3, 4) 43 PARAMS_C = (5, 6) 44 45 param_list = itertools.product(PARAMS_A, PARAMS_B, PARAMS_C) 46 util.InjectParameterizedTest(cls, param_list, cls.NameGenerator) 47 48 def ParamTestDummyFunc(self, a, b, c): 49 self.tests_run_list.append( 50 "testDummyFunc_" + ParmeterizationTest.NameGenerator(a, b, c)) 51 52 def testParameterization(self): 53 expected = [ 54 "testDummyFunc_1_3_5", 55 "testDummyFunc_1_3_6", 56 "testDummyFunc_1_4_5", 57 "testDummyFunc_1_4_6", 58 "testDummyFunc_2_3_5", 59 "testDummyFunc_2_3_6", 60 "testDummyFunc_2_4_5", 61 "testDummyFunc_2_4_6", 62 ] 63 64 actual = [name for name in dir(self) if name.startswith("testDummyFunc")] 65 66 # Check that name and contents are equal 67 self.assertEqual(len(expected), len(actual)) 68 self.assertEqual(sorted(expected), sorted(actual)) 69 70 # Start a clean list, and run all the tests. 71 self.tests_run_list = list() 72 for test_name in expected: 73 test_method = getattr(self, test_name) 74 test_method() 75 76 # Make sure all tests have been run with the correct parameters 77 for test_name in expected: 78 self.assertTrue(test_name in self.tests_run_list) 79 80 81if __name__ == "__main__": 82 ParmeterizationTest.InjectTests() 83 unittest.main() 84