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 """Tests the --help flag of Google C++ Testing Framework.
00033
00034 SYNOPSIS
00035 gtest_help_test.py --build_dir=BUILD/DIR
00036 # where BUILD/DIR contains the built gtest_help_test_ file.
00037 gtest_help_test.py
00038 """
00039
00040 __author__ = 'wan@google.com (Zhanyong Wan)'
00041
00042 import os
00043 import re
00044 import gtest_test_utils
00045
00046
00047 IS_LINUX = os.name == 'posix' and os.uname()[0] == 'Linux'
00048 IS_WINDOWS = os.name == 'nt'
00049
00050 PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath('gtest_help_test_')
00051 FLAG_PREFIX = '--gtest_'
00052 DEATH_TEST_STYLE_FLAG = FLAG_PREFIX + 'death_test_style'
00053 STREAM_RESULT_TO_FLAG = FLAG_PREFIX + 'stream_result_to'
00054 UNKNOWN_FLAG = FLAG_PREFIX + 'unknown_flag_for_testing'
00055 LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests'
00056 INCORRECT_FLAG_VARIANTS = [re.sub('^--', '-', LIST_TESTS_FLAG),
00057 re.sub('^--', '/', LIST_TESTS_FLAG),
00058 re.sub('_', '-', LIST_TESTS_FLAG)]
00059 INTERNAL_FLAG_FOR_TESTING = FLAG_PREFIX + 'internal_flag_for_testing'
00060
00061 SUPPORTS_DEATH_TESTS = "DeathTest" in gtest_test_utils.Subprocess(
00062 [PROGRAM_PATH, LIST_TESTS_FLAG]).output
00063
00064
00065 HELP_REGEX = re.compile(
00066 FLAG_PREFIX + r'list_tests.*' +
00067 FLAG_PREFIX + r'filter=.*' +
00068 FLAG_PREFIX + r'also_run_disabled_tests.*' +
00069 FLAG_PREFIX + r'repeat=.*' +
00070 FLAG_PREFIX + r'shuffle.*' +
00071 FLAG_PREFIX + r'random_seed=.*' +
00072 FLAG_PREFIX + r'color=.*' +
00073 FLAG_PREFIX + r'print_time.*' +
00074 FLAG_PREFIX + r'output=.*' +
00075 FLAG_PREFIX + r'break_on_failure.*' +
00076 FLAG_PREFIX + r'throw_on_failure.*' +
00077 FLAG_PREFIX + r'catch_exceptions=0.*',
00078 re.DOTALL)
00079
00080
00081 def RunWithFlag(flag):
00082 """Runs gtest_help_test_ with the given flag.
00083
00084 Returns:
00085 the exit code and the text output as a tuple.
00086 Args:
00087 flag: the command-line flag to pass to gtest_help_test_, or None.
00088 """
00089
00090 if flag is None:
00091 command = [PROGRAM_PATH]
00092 else:
00093 command = [PROGRAM_PATH, flag]
00094 child = gtest_test_utils.Subprocess(command)
00095 return child.exit_code, child.output
00096
00097
00098 class GTestHelpTest(gtest_test_utils.TestCase):
00099 """Tests the --help flag and its equivalent forms."""
00100
00101 def TestHelpFlag(self, flag):
00102 """Verifies correct behavior when help flag is specified.
00103
00104 The right message must be printed and the tests must
00105 skipped when the given flag is specified.
00106
00107 Args:
00108 flag: A flag to pass to the binary or None.
00109 """
00110
00111 exit_code, output = RunWithFlag(flag)
00112 self.assertEquals(0, exit_code)
00113 self.assert_(HELP_REGEX.search(output), output)
00114
00115 if IS_LINUX:
00116 self.assert_(STREAM_RESULT_TO_FLAG in output, output)
00117 else:
00118 self.assert_(STREAM_RESULT_TO_FLAG not in output, output)
00119
00120 if SUPPORTS_DEATH_TESTS and not IS_WINDOWS:
00121 self.assert_(DEATH_TEST_STYLE_FLAG in output, output)
00122 else:
00123 self.assert_(DEATH_TEST_STYLE_FLAG not in output, output)
00124
00125 def TestNonHelpFlag(self, flag):
00126 """Verifies correct behavior when no help flag is specified.
00127
00128 Verifies that when no help flag is specified, the tests are run
00129 and the help message is not printed.
00130
00131 Args:
00132 flag: A flag to pass to the binary or None.
00133 """
00134
00135 exit_code, output = RunWithFlag(flag)
00136 self.assert_(exit_code != 0)
00137 self.assert_(not HELP_REGEX.search(output), output)
00138
00139 def testPrintsHelpWithFullFlag(self):
00140 self.TestHelpFlag('--help')
00141
00142 def testPrintsHelpWithShortFlag(self):
00143 self.TestHelpFlag('-h')
00144
00145 def testPrintsHelpWithQuestionFlag(self):
00146 self.TestHelpFlag('-?')
00147
00148 def testPrintsHelpWithWindowsStyleQuestionFlag(self):
00149 self.TestHelpFlag('/?')
00150
00151 def testPrintsHelpWithUnrecognizedGoogleTestFlag(self):
00152 self.TestHelpFlag(UNKNOWN_FLAG)
00153
00154 def testPrintsHelpWithIncorrectFlagStyle(self):
00155 for incorrect_flag in INCORRECT_FLAG_VARIANTS:
00156 self.TestHelpFlag(incorrect_flag)
00157
00158 def testRunsTestsWithoutHelpFlag(self):
00159 """Verifies that when no help flag is specified, the tests are run
00160 and the help message is not printed."""
00161
00162 self.TestNonHelpFlag(None)
00163
00164 def testRunsTestsWithGtestInternalFlag(self):
00165 """Verifies that the tests are run and no help message is printed when
00166 a flag starting with Google Test prefix and 'internal_' is supplied."""
00167
00168 self.TestNonHelpFlag(INTERNAL_FLAG_FOR_TESTING)
00169
00170
00171 if __name__ == '__main__':
00172 gtest_test_utils.Main()