1#!/usr/bin/env python 2# 3# Copyright 2006, Google Inc. 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions are 8# met: 9# 10# * Redistributions of source code must retain the above copyright 11# notice, this list of conditions and the following disclaimer. 12# * Redistributions in binary form must reproduce the above 13# copyright notice, this list of conditions and the following disclaimer 14# in the documentation and/or other materials provided with the 15# distribution. 16# * Neither the name of Google Inc. nor the names of its 17# contributors may be used to endorse or promote products derived from 18# this software without specific prior written permission. 19# 20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 32"""Unit test for Google Test's --gtest_list_tests flag. 33 34A user can ask Google Test to list all tests by specifying the 35--gtest_list_tests flag. This script tests such functionality 36by invoking googletest-list-tests-unittest_ (a program written with 37Google Test) the command line flags. 38""" 39 40import re 41from googletest.test import gtest_test_utils 42 43# Constants. 44 45# The command line flag for enabling/disabling listing all tests. 46LIST_TESTS_FLAG = 'gtest_list_tests' 47 48# Path to the googletest-list-tests-unittest_ program. 49EXE_PATH = gtest_test_utils.GetTestExecutablePath( 50 'googletest-list-tests-unittest_' 51) 52 53# The expected output when running googletest-list-tests-unittest_ with 54# --gtest_list_tests 55EXPECTED_OUTPUT_NO_FILTER_RE = re.compile( 56 r"""FooDeathTest\. 57 Test1 58Foo\. 59 Bar1 60 Bar2 61 DISABLED_Bar3 62Abc\. 63 Xyz 64 Def 65FooBar\. 66 Baz 67FooTest\. 68 Test1 69 DISABLED_Test2 70 Test3 71TypedTest/0\. # TypeParam = (VeryLo{245}|class VeryLo{239})\.\.\. 72 TestA 73 TestB 74TypedTest/1\. # TypeParam = int\s*\*( __ptr64)? 75 TestA 76 TestB 77TypedTest/2\. # TypeParam = .*MyArray<bool,\s*42> 78 TestA 79 TestB 80My/TypeParamTest/0\. # TypeParam = (VeryLo{245}|class VeryLo{239})\.\.\. 81 TestA 82 TestB 83My/TypeParamTest/1\. # TypeParam = int\s*\*( __ptr64)? 84 TestA 85 TestB 86My/TypeParamTest/2\. # TypeParam = .*MyArray<bool,\s*42> 87 TestA 88 TestB 89MyInstantiation/ValueParamTest\. 90 TestA/0 # GetParam\(\) = one line 91 TestA/1 # GetParam\(\) = two\\nlines 92 TestA/2 # GetParam\(\) = a very\\nlo{241}\.\.\. 93 TestB/0 # GetParam\(\) = one line 94 TestB/1 # GetParam\(\) = two\\nlines 95 TestB/2 # GetParam\(\) = a very\\nlo{241}\.\.\. 96""" 97) 98 99# The expected output when running googletest-list-tests-unittest_ with 100# --gtest_list_tests and --gtest_filter=Foo*. 101EXPECTED_OUTPUT_FILTER_FOO_RE = re.compile( 102 r"""FooDeathTest\. 103 Test1 104Foo\. 105 Bar1 106 Bar2 107 DISABLED_Bar3 108FooBar\. 109 Baz 110FooTest\. 111 Test1 112 DISABLED_Test2 113 Test3 114""" 115) 116 117# Utilities. 118 119 120def Run(args): 121 """Runs googletest-list-tests-unittest_ and returns the list of tests printed.""" 122 123 return gtest_test_utils.Subprocess( 124 [EXE_PATH] + args, capture_stderr=False 125 ).output 126 127 128# The unit test. 129 130 131class GTestListTestsUnitTest(gtest_test_utils.TestCase): 132 """Tests using the --gtest_list_tests flag to list all tests.""" 133 134 def RunAndVerify(self, flag_value, expected_output_re, other_flag): 135 """Run googletest-list-tests-unittest_ and verify the output. 136 137 Runs googletest-list-tests-unittest_ and verifies that it prints 138 the correct tests. 139 140 Args: 141 flag_value: value of the --gtest_list_tests flag; None if the flag 142 should not be present. 143 expected_output_re: regular expression that matches the expected output 144 after running command; 145 other_flag: a different flag to be passed to command along with 146 gtest_list_tests; None if the flag should not be present. 147 """ 148 149 if flag_value is None: 150 flag = '' 151 flag_expression = 'not set' 152 elif flag_value == '0': 153 flag = '--%s=0' % LIST_TESTS_FLAG 154 flag_expression = '0' 155 else: 156 flag = '--%s' % LIST_TESTS_FLAG 157 flag_expression = '1' 158 159 args = [flag] 160 161 if other_flag is not None: 162 args += [other_flag] 163 164 output = Run(args) 165 166 if expected_output_re: 167 self.assertTrue( 168 expected_output_re.match(output), 169 'when %s is %s, the output of "%s" is "%s",\n' 170 'which does not match regex "%s"' 171 % ( 172 LIST_TESTS_FLAG, 173 flag_expression, 174 ' '.join(args), 175 output, 176 expected_output_re.pattern, 177 ), 178 ) 179 else: 180 self.assertTrue( 181 not EXPECTED_OUTPUT_NO_FILTER_RE.match(output), 182 'when %s is %s, the output of "%s" is "%s"' 183 % (LIST_TESTS_FLAG, flag_expression, ' '.join(args), output), 184 ) 185 186 def testDefaultBehavior(self): 187 """Tests the behavior of the default mode.""" 188 189 self.RunAndVerify(flag_value=None, expected_output_re=None, other_flag=None) 190 191 def testFlag(self): 192 """Tests using the --gtest_list_tests flag.""" 193 194 self.RunAndVerify(flag_value='0', expected_output_re=None, other_flag=None) 195 self.RunAndVerify( 196 flag_value='1', 197 expected_output_re=EXPECTED_OUTPUT_NO_FILTER_RE, 198 other_flag=None, 199 ) 200 201 def testOverrideNonFilterFlags(self): 202 """Tests that --gtest_list_tests overrides the non-filter flags.""" 203 204 self.RunAndVerify( 205 flag_value='1', 206 expected_output_re=EXPECTED_OUTPUT_NO_FILTER_RE, 207 other_flag='--gtest_break_on_failure', 208 ) 209 210 def testWithFilterFlags(self): 211 """Tests that --gtest_list_tests takes into account the filter flags. 212 213 Tests that --gtest_list_tests takes into account the 214 --gtest_filter flag. 215 """ 216 217 self.RunAndVerify( 218 flag_value='1', 219 expected_output_re=EXPECTED_OUTPUT_FILTER_FOO_RE, 220 other_flag='--gtest_filter=Foo*', 221 ) 222 223 224if __name__ == '__main__': 225 gtest_test_utils.Main() 226