1d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville#!/usr/bin/env python 2d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# 3d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# Copyright 2009 Google Inc. All Rights Reserved. 4d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# 5d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# Redistribution and use in source and binary forms, with or without 6d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# modification, are permitted provided that the following conditions are 7d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# met: 8d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# 9d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# * Redistributions of source code must retain the above copyright 10d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# notice, this list of conditions and the following disclaimer. 11d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# * Redistributions in binary form must reproduce the above 12d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# copyright notice, this list of conditions and the following disclaimer 13d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# in the documentation and/or other materials provided with the 14d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# distribution. 15d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# * Neither the name of Google Inc. nor the names of its 16d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# contributors may be used to endorse or promote products derived from 17d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# this software without specific prior written permission. 18d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# 19d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 31d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville"""Verifies that test shuffling works.""" 32d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 33d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville__author__ = 'wan@google.com (Zhanyong Wan)' 34d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 35d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Savilleimport os 36d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Savilleimport gtest_test_utils 37d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 38d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# Command to run the gtest_shuffle_test_ program. 39d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink SavilleCOMMAND = gtest_test_utils.GetTestExecutablePath('gtest_shuffle_test_') 40d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 41d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville# The environment variables for test sharding. 42d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink SavilleTOTAL_SHARDS_ENV_VAR = 'GTEST_TOTAL_SHARDS' 43d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink SavilleSHARD_INDEX_ENV_VAR = 'GTEST_SHARD_INDEX' 44d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 45d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink SavilleTEST_FILTER = 'A*.A:A*.B:C*' 46d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 47d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink SavilleALL_TESTS = [] 48d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink SavilleACTIVE_TESTS = [] 49d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink SavilleFILTERED_TESTS = [] 50d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink SavilleSHARDED_TESTS = [] 51d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 52d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink SavilleSHUFFLED_ALL_TESTS = [] 53d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink SavilleSHUFFLED_ACTIVE_TESTS = [] 54d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink SavilleSHUFFLED_FILTERED_TESTS = [] 55d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink SavilleSHUFFLED_SHARDED_TESTS = [] 56d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 57d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 58d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Savilledef AlsoRunDisabledTestsFlag(): 59d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville return '--gtest_also_run_disabled_tests' 60d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 61d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 62d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Savilledef FilterFlag(test_filter): 63d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville return '--gtest_filter=%s' % (test_filter,) 64d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 65d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 66d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Savilledef RepeatFlag(n): 67d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville return '--gtest_repeat=%s' % (n,) 68d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 69d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 70d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Savilledef ShuffleFlag(): 71d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville return '--gtest_shuffle' 72d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 73d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 74d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Savilledef RandomSeedFlag(n): 75d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville return '--gtest_random_seed=%s' % (n,) 76d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 77d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 78d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Savilledef RunAndReturnOutput(extra_env, args): 79d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville """Runs the test program and returns its output.""" 80d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 81d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville try: 82d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville original_env = os.environ.copy() 83d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville os.environ.update(extra_env) 84d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville return gtest_test_utils.Subprocess([COMMAND] + args).output 85d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville finally: 86d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville for key in extra_env.iterkeys(): 87d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville if key in original_env: 88d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville os.environ[key] = original_env[key] 89d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville else: 90d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville del os.environ[key] 91d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 92d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 93d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Savilledef GetTestsForAllIterations(extra_env, args): 94d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville """Runs the test program and returns a list of test lists. 95d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 96d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville Args: 97d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville extra_env: a map from environment variables to their values 98d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville args: command line flags to pass to gtest_shuffle_test_ 99d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 100d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville Returns: 101d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville A list where the i-th element is the list of tests run in the i-th 102d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville test iteration. 103d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville """ 104d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 105d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville test_iterations = [] 106d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville for line in RunAndReturnOutput(extra_env, args).split('\n'): 107d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville if line.startswith('----'): 108d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville tests = [] 109d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville test_iterations.append(tests) 110d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville elif line.strip(): 111d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville tests.append(line.strip()) # 'TestCaseName.TestName' 112d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 113d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville return test_iterations 114d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 115d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 116d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Savilledef GetTestCases(tests): 117d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville """Returns a list of test cases in the given full test names. 118d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 119d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville Args: 120d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville tests: a list of full test names 121d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 122d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville Returns: 123d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville A list of test cases from 'tests', in their original order. 124d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville Consecutive duplicates are removed. 125d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville """ 126d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 127d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville test_cases = [] 128d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville for test in tests: 129d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville test_case = test.split('.')[0] 130d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville if not test_case in test_cases: 131d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville test_cases.append(test_case) 132d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 133d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville return test_cases 134d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 135d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 136d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Savilledef CalculateTestLists(): 137d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville """Calculates the list of tests run under different flags.""" 138d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 139d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville if not ALL_TESTS: 140d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville ALL_TESTS.extend( 141d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville GetTestsForAllIterations({}, [AlsoRunDisabledTestsFlag()])[0]) 142d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 143d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville if not ACTIVE_TESTS: 144d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville ACTIVE_TESTS.extend(GetTestsForAllIterations({}, [])[0]) 145d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 146d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville if not FILTERED_TESTS: 147d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville FILTERED_TESTS.extend( 148d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville GetTestsForAllIterations({}, [FilterFlag(TEST_FILTER)])[0]) 149d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 150d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville if not SHARDED_TESTS: 151d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville SHARDED_TESTS.extend( 152d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville GetTestsForAllIterations({TOTAL_SHARDS_ENV_VAR: '3', 153d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville SHARD_INDEX_ENV_VAR: '1'}, 154d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville [])[0]) 155d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 156d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville if not SHUFFLED_ALL_TESTS: 157d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville SHUFFLED_ALL_TESTS.extend(GetTestsForAllIterations( 158d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville {}, [AlsoRunDisabledTestsFlag(), ShuffleFlag(), RandomSeedFlag(1)])[0]) 159d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 160d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville if not SHUFFLED_ACTIVE_TESTS: 161d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville SHUFFLED_ACTIVE_TESTS.extend(GetTestsForAllIterations( 162d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville {}, [ShuffleFlag(), RandomSeedFlag(1)])[0]) 163d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 164d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville if not SHUFFLED_FILTERED_TESTS: 165d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville SHUFFLED_FILTERED_TESTS.extend(GetTestsForAllIterations( 166d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville {}, [ShuffleFlag(), RandomSeedFlag(1), FilterFlag(TEST_FILTER)])[0]) 167d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 168d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville if not SHUFFLED_SHARDED_TESTS: 169d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville SHUFFLED_SHARDED_TESTS.extend( 170d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville GetTestsForAllIterations({TOTAL_SHARDS_ENV_VAR: '3', 171d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville SHARD_INDEX_ENV_VAR: '1'}, 172d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville [ShuffleFlag(), RandomSeedFlag(1)])[0]) 173d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 174d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 175d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Savilleclass GTestShuffleUnitTest(gtest_test_utils.TestCase): 176d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville """Tests test shuffling.""" 177d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 178d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville def setUp(self): 179d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville CalculateTestLists() 180d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 181d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville def testShufflePreservesNumberOfTests(self): 182d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assertEqual(len(ALL_TESTS), len(SHUFFLED_ALL_TESTS)) 183d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assertEqual(len(ACTIVE_TESTS), len(SHUFFLED_ACTIVE_TESTS)) 184d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assertEqual(len(FILTERED_TESTS), len(SHUFFLED_FILTERED_TESTS)) 185d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assertEqual(len(SHARDED_TESTS), len(SHUFFLED_SHARDED_TESTS)) 186d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 187d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville def testShuffleChangesTestOrder(self): 188d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assert_(SHUFFLED_ALL_TESTS != ALL_TESTS, SHUFFLED_ALL_TESTS) 189d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assert_(SHUFFLED_ACTIVE_TESTS != ACTIVE_TESTS, SHUFFLED_ACTIVE_TESTS) 190d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assert_(SHUFFLED_FILTERED_TESTS != FILTERED_TESTS, 191d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville SHUFFLED_FILTERED_TESTS) 192d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assert_(SHUFFLED_SHARDED_TESTS != SHARDED_TESTS, 193d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville SHUFFLED_SHARDED_TESTS) 194d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 195d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville def testShuffleChangesTestCaseOrder(self): 196d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assert_(GetTestCases(SHUFFLED_ALL_TESTS) != GetTestCases(ALL_TESTS), 197d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville GetTestCases(SHUFFLED_ALL_TESTS)) 198d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assert_( 199d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville GetTestCases(SHUFFLED_ACTIVE_TESTS) != GetTestCases(ACTIVE_TESTS), 200d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville GetTestCases(SHUFFLED_ACTIVE_TESTS)) 201d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assert_( 202d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville GetTestCases(SHUFFLED_FILTERED_TESTS) != GetTestCases(FILTERED_TESTS), 203d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville GetTestCases(SHUFFLED_FILTERED_TESTS)) 204d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assert_( 205d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville GetTestCases(SHUFFLED_SHARDED_TESTS) != GetTestCases(SHARDED_TESTS), 206d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville GetTestCases(SHUFFLED_SHARDED_TESTS)) 207d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 208d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville def testShuffleDoesNotRepeatTest(self): 209d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville for test in SHUFFLED_ALL_TESTS: 210d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assertEqual(1, SHUFFLED_ALL_TESTS.count(test), 211d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville '%s appears more than once' % (test,)) 212d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville for test in SHUFFLED_ACTIVE_TESTS: 213d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assertEqual(1, SHUFFLED_ACTIVE_TESTS.count(test), 214d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville '%s appears more than once' % (test,)) 215d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville for test in SHUFFLED_FILTERED_TESTS: 216d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assertEqual(1, SHUFFLED_FILTERED_TESTS.count(test), 217d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville '%s appears more than once' % (test,)) 218d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville for test in SHUFFLED_SHARDED_TESTS: 219d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assertEqual(1, SHUFFLED_SHARDED_TESTS.count(test), 220d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville '%s appears more than once' % (test,)) 221d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 222d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville def testShuffleDoesNotCreateNewTest(self): 223d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville for test in SHUFFLED_ALL_TESTS: 224d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assert_(test in ALL_TESTS, '%s is an invalid test' % (test,)) 225d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville for test in SHUFFLED_ACTIVE_TESTS: 226d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assert_(test in ACTIVE_TESTS, '%s is an invalid test' % (test,)) 227d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville for test in SHUFFLED_FILTERED_TESTS: 228d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assert_(test in FILTERED_TESTS, '%s is an invalid test' % (test,)) 229d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville for test in SHUFFLED_SHARDED_TESTS: 230d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assert_(test in SHARDED_TESTS, '%s is an invalid test' % (test,)) 231d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 232d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville def testShuffleIncludesAllTests(self): 233d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville for test in ALL_TESTS: 234d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assert_(test in SHUFFLED_ALL_TESTS, '%s is missing' % (test,)) 235d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville for test in ACTIVE_TESTS: 236d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assert_(test in SHUFFLED_ACTIVE_TESTS, '%s is missing' % (test,)) 237d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville for test in FILTERED_TESTS: 238d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assert_(test in SHUFFLED_FILTERED_TESTS, '%s is missing' % (test,)) 239d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville for test in SHARDED_TESTS: 240d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assert_(test in SHUFFLED_SHARDED_TESTS, '%s is missing' % (test,)) 241d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 242d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville def testShuffleLeavesDeathTestsAtFront(self): 243d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville non_death_test_found = False 244d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville for test in SHUFFLED_ACTIVE_TESTS: 245d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville if 'DeathTest.' in test: 246d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assert_(not non_death_test_found, 247d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville '%s appears after a non-death test' % (test,)) 248d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville else: 249d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville non_death_test_found = True 250d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 251d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville def _VerifyTestCasesDoNotInterleave(self, tests): 252d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville test_cases = [] 253d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville for test in tests: 254d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville [test_case, _] = test.split('.') 255d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville if test_cases and test_cases[-1] != test_case: 256d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville test_cases.append(test_case) 257d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assertEqual(1, test_cases.count(test_case), 258d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 'Test case %s is not grouped together in %s' % 259d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville (test_case, tests)) 260d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 261d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville def testShuffleDoesNotInterleaveTestCases(self): 262d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self._VerifyTestCasesDoNotInterleave(SHUFFLED_ALL_TESTS) 263d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self._VerifyTestCasesDoNotInterleave(SHUFFLED_ACTIVE_TESTS) 264d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self._VerifyTestCasesDoNotInterleave(SHUFFLED_FILTERED_TESTS) 265d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self._VerifyTestCasesDoNotInterleave(SHUFFLED_SHARDED_TESTS) 266d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 267d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville def testShuffleRestoresOrderAfterEachIteration(self): 268d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville # Get the test lists in all 3 iterations, using random seed 1, 2, 269d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville # and 3 respectively. Google Test picks a different seed in each 270d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville # iteration, and this test depends on the current implementation 271d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville # picking successive numbers. This dependency is not ideal, but 272d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville # makes the test much easier to write. 273d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville [tests_in_iteration1, tests_in_iteration2, tests_in_iteration3] = ( 274d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville GetTestsForAllIterations( 275d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville {}, [ShuffleFlag(), RandomSeedFlag(1), RepeatFlag(3)])) 276d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 277d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville # Make sure running the tests with random seed 1 gets the same 278d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville # order as in iteration 1 above. 279d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville [tests_with_seed1] = GetTestsForAllIterations( 280d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville {}, [ShuffleFlag(), RandomSeedFlag(1)]) 281d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assertEqual(tests_in_iteration1, tests_with_seed1) 282d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 283d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville # Make sure running the tests with random seed 2 gets the same 284d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville # order as in iteration 2 above. Success means that Google Test 285d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville # correctly restores the test order before re-shuffling at the 286d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville # beginning of iteration 2. 287d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville [tests_with_seed2] = GetTestsForAllIterations( 288d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville {}, [ShuffleFlag(), RandomSeedFlag(2)]) 289d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assertEqual(tests_in_iteration2, tests_with_seed2) 290d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 291d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville # Make sure running the tests with random seed 3 gets the same 292d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville # order as in iteration 3 above. Success means that Google Test 293d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville # correctly restores the test order before re-shuffling at the 294d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville # beginning of iteration 3. 295d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville [tests_with_seed3] = GetTestsForAllIterations( 296d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville {}, [ShuffleFlag(), RandomSeedFlag(3)]) 297d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assertEqual(tests_in_iteration3, tests_with_seed3) 298d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 299d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville def testShuffleGeneratesNewOrderInEachIteration(self): 300d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville [tests_in_iteration1, tests_in_iteration2, tests_in_iteration3] = ( 301d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville GetTestsForAllIterations( 302d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville {}, [ShuffleFlag(), RandomSeedFlag(1), RepeatFlag(3)])) 303d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 304d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assert_(tests_in_iteration1 != tests_in_iteration2, 305d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville tests_in_iteration1) 306d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assert_(tests_in_iteration1 != tests_in_iteration3, 307d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville tests_in_iteration1) 308d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assert_(tests_in_iteration2 != tests_in_iteration3, 309d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville tests_in_iteration2) 310d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 311d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville def testShuffleShardedTestsPreservesPartition(self): 312d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville # If we run M tests on N shards, the same M tests should be run in 313d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville # total, regardless of the random seeds used by the shards. 314d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville [tests1] = GetTestsForAllIterations({TOTAL_SHARDS_ENV_VAR: '3', 315d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville SHARD_INDEX_ENV_VAR: '0'}, 316d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville [ShuffleFlag(), RandomSeedFlag(1)]) 317d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville [tests2] = GetTestsForAllIterations({TOTAL_SHARDS_ENV_VAR: '3', 318d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville SHARD_INDEX_ENV_VAR: '1'}, 319d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville [ShuffleFlag(), RandomSeedFlag(20)]) 320d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville [tests3] = GetTestsForAllIterations({TOTAL_SHARDS_ENV_VAR: '3', 321d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville SHARD_INDEX_ENV_VAR: '2'}, 322d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville [ShuffleFlag(), RandomSeedFlag(25)]) 323d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville sorted_sharded_tests = tests1 + tests2 + tests3 324d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville sorted_sharded_tests.sort() 325d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville sorted_active_tests = [] 326d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville sorted_active_tests.extend(ACTIVE_TESTS) 327d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville sorted_active_tests.sort() 328d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville self.assertEqual(sorted_active_tests, sorted_sharded_tests) 329d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 330d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Savilleif __name__ == '__main__': 331d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville gtest_test_utils.Main() 332