Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 """Unit test for Google Test's --gtest_list_tests flag.
00033
00034 A user can ask Google Test to list all tests by specifying the
00035 --gtest_list_tests flag. This script tests such functionality
00036 by invoking gtest_list_tests_unittest_ (a program written with
00037 Google Test) the command line flags.
00038 """
00039
00040 __author__ = 'phanna@google.com (Patrick Hanna)'
00041
00042 import gtest_test_utils
00043 import re
00044
00045
00046
00047
00048
00049 LIST_TESTS_FLAG = 'gtest_list_tests'
00050
00051
00052 EXE_PATH = gtest_test_utils.GetTestExecutablePath('gtest_list_tests_unittest_')
00053
00054
00055
00056 EXPECTED_OUTPUT_NO_FILTER_RE = re.compile(r"""FooDeathTest\.
00057 Test1
00058 Foo\.
00059 Bar1
00060 Bar2
00061 DISABLED_Bar3
00062 Abc\.
00063 Xyz
00064 Def
00065 FooBar\.
00066 Baz
00067 FooTest\.
00068 Test1
00069 DISABLED_Test2
00070 Test3
00071 TypedTest/0\. # TypeParam = (VeryLo{245}|class VeryLo{239})\.\.\.
00072 TestA
00073 TestB
00074 TypedTest/1\. # TypeParam = int\s*\*
00075 TestA
00076 TestB
00077 TypedTest/2\. # TypeParam = .*MyArray<bool,\s*42>
00078 TestA
00079 TestB
00080 My/TypeParamTest/0\. # TypeParam = (VeryLo{245}|class VeryLo{239})\.\.\.
00081 TestA
00082 TestB
00083 My/TypeParamTest/1\. # TypeParam = int\s*\*
00084 TestA
00085 TestB
00086 My/TypeParamTest/2\. # TypeParam = .*MyArray<bool,\s*42>
00087 TestA
00088 TestB
00089 MyInstantiation/ValueParamTest\.
00090 TestA/0 # GetParam\(\) = one line
00091 TestA/1 # GetParam\(\) = two\\nlines
00092 TestA/2 # GetParam\(\) = a very\\nlo{241}\.\.\.
00093 TestB/0 # GetParam\(\) = one line
00094 TestB/1 # GetParam\(\) = two\\nlines
00095 TestB/2 # GetParam\(\) = a very\\nlo{241}\.\.\.
00096 """)
00097
00098
00099
00100 EXPECTED_OUTPUT_FILTER_FOO_RE = re.compile(r"""FooDeathTest\.
00101 Test1
00102 Foo\.
00103 Bar1
00104 Bar2
00105 DISABLED_Bar3
00106 FooBar\.
00107 Baz
00108 FooTest\.
00109 Test1
00110 DISABLED_Test2
00111 Test3
00112 """)
00113
00114
00115
00116
00117 def Run(args):
00118 """Runs gtest_list_tests_unittest_ and returns the list of tests printed."""
00119
00120 return gtest_test_utils.Subprocess([EXE_PATH] + args,
00121 capture_stderr=False).output
00122
00123
00124
00125
00126 class GTestListTestsUnitTest(gtest_test_utils.TestCase):
00127 """Tests using the --gtest_list_tests flag to list all tests."""
00128
00129 def RunAndVerify(self, flag_value, expected_output_re, other_flag):
00130 """Runs gtest_list_tests_unittest_ and verifies that it prints
00131 the correct tests.
00132
00133 Args:
00134 flag_value: value of the --gtest_list_tests flag;
00135 None if the flag should not be present.
00136 expected_output_re: regular expression that matches the expected
00137 output after running command;
00138 other_flag: a different flag to be passed to command
00139 along with gtest_list_tests;
00140 None if the flag should not be present.
00141 """
00142
00143 if flag_value is None:
00144 flag = ''
00145 flag_expression = 'not set'
00146 elif flag_value == '0':
00147 flag = '--%s=0' % LIST_TESTS_FLAG
00148 flag_expression = '0'
00149 else:
00150 flag = '--%s' % LIST_TESTS_FLAG
00151 flag_expression = '1'
00152
00153 args = [flag]
00154
00155 if other_flag is not None:
00156 args += [other_flag]
00157
00158 output = Run(args)
00159
00160 if expected_output_re:
00161 self.assert_(
00162 expected_output_re.match(output),
00163 ('when %s is %s, the output of "%s" is "%s",\n'
00164 'which does not match regex "%s"' %
00165 (LIST_TESTS_FLAG, flag_expression, ' '.join(args), output,
00166 expected_output_re.pattern)))
00167 else:
00168 self.assert_(
00169 not EXPECTED_OUTPUT_NO_FILTER_RE.match(output),
00170 ('when %s is %s, the output of "%s" is "%s"'%
00171 (LIST_TESTS_FLAG, flag_expression, ' '.join(args), output)))
00172
00173 def testDefaultBehavior(self):
00174 """Tests the behavior of the default mode."""
00175
00176 self.RunAndVerify(flag_value=None,
00177 expected_output_re=None,
00178 other_flag=None)
00179
00180 def testFlag(self):
00181 """Tests using the --gtest_list_tests flag."""
00182
00183 self.RunAndVerify(flag_value='0',
00184 expected_output_re=None,
00185 other_flag=None)
00186 self.RunAndVerify(flag_value='1',
00187 expected_output_re=EXPECTED_OUTPUT_NO_FILTER_RE,
00188 other_flag=None)
00189
00190 def testOverrideNonFilterFlags(self):
00191 """Tests that --gtest_list_tests overrides the non-filter flags."""
00192
00193 self.RunAndVerify(flag_value='1',
00194 expected_output_re=EXPECTED_OUTPUT_NO_FILTER_RE,
00195 other_flag='--gtest_break_on_failure')
00196
00197 def testWithFilterFlags(self):
00198 """Tests that --gtest_list_tests takes into account the
00199 --gtest_filter flag."""
00200
00201 self.RunAndVerify(flag_value='1',
00202 expected_output_re=EXPECTED_OUTPUT_FILTER_FOO_RE,
00203 other_flag='--gtest_filter=Foo*')
00204
00205
00206 if __name__ == '__main__':
00207 gtest_test_utils.Main()