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 """Tests Google Test's exception catching behavior.
00032
00033 This script invokes gtest_catch_exceptions_test_ and
00034 gtest_catch_exceptions_ex_test_ (programs written with
00035 Google Test) and verifies their output.
00036 """
00037
00038 __author__ = 'vladl@google.com (Vlad Losev)'
00039
00040 import os
00041
00042 import gtest_test_utils
00043
00044
00045 FLAG_PREFIX = '--gtest_'
00046 LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests'
00047 NO_CATCH_EXCEPTIONS_FLAG = FLAG_PREFIX + 'catch_exceptions=0'
00048 FILTER_FLAG = FLAG_PREFIX + 'filter'
00049
00050
00051
00052 EX_EXE_PATH = gtest_test_utils.GetTestExecutablePath(
00053 'gtest_catch_exceptions_ex_test_')
00054
00055
00056
00057 EXE_PATH = gtest_test_utils.GetTestExecutablePath(
00058 'gtest_catch_exceptions_no_ex_test_')
00059
00060 environ = gtest_test_utils.environ
00061 SetEnvVar = gtest_test_utils.SetEnvVar
00062
00063
00064
00065
00066
00067
00068 SetEnvVar(gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR, None)
00069
00070 TEST_LIST = gtest_test_utils.Subprocess(
00071 [EXE_PATH, LIST_TESTS_FLAG], env=environ).output
00072
00073 SUPPORTS_SEH_EXCEPTIONS = 'ThrowsSehException' in TEST_LIST
00074
00075 if SUPPORTS_SEH_EXCEPTIONS:
00076 BINARY_OUTPUT = gtest_test_utils.Subprocess([EXE_PATH], env=environ).output
00077
00078 EX_BINARY_OUTPUT = gtest_test_utils.Subprocess(
00079 [EX_EXE_PATH], env=environ).output
00080
00081
00082
00083 if SUPPORTS_SEH_EXCEPTIONS:
00084
00085 class CatchSehExceptionsTest(gtest_test_utils.TestCase):
00086 """Tests exception-catching behavior."""
00087
00088
00089 def TestSehExceptions(self, test_output):
00090 self.assert_('SEH exception with code 0x2a thrown '
00091 'in the test fixture\'s constructor'
00092 in test_output)
00093 self.assert_('SEH exception with code 0x2a thrown '
00094 'in the test fixture\'s destructor'
00095 in test_output)
00096 self.assert_('SEH exception with code 0x2a thrown in SetUpTestCase()'
00097 in test_output)
00098 self.assert_('SEH exception with code 0x2a thrown in TearDownTestCase()'
00099 in test_output)
00100 self.assert_('SEH exception with code 0x2a thrown in SetUp()'
00101 in test_output)
00102 self.assert_('SEH exception with code 0x2a thrown in TearDown()'
00103 in test_output)
00104 self.assert_('SEH exception with code 0x2a thrown in the test body'
00105 in test_output)
00106
00107 def testCatchesSehExceptionsWithCxxExceptionsEnabled(self):
00108 self.TestSehExceptions(EX_BINARY_OUTPUT)
00109
00110 def testCatchesSehExceptionsWithCxxExceptionsDisabled(self):
00111 self.TestSehExceptions(BINARY_OUTPUT)
00112
00113
00114 class CatchCxxExceptionsTest(gtest_test_utils.TestCase):
00115 """Tests C++ exception-catching behavior.
00116
00117 Tests in this test case verify that:
00118 * C++ exceptions are caught and logged as C++ (not SEH) exceptions
00119 * Exception thrown affect the remainder of the test work flow in the
00120 expected manner.
00121 """
00122
00123 def testCatchesCxxExceptionsInFixtureConstructor(self):
00124 self.assert_('C++ exception with description '
00125 '"Standard C++ exception" thrown '
00126 'in the test fixture\'s constructor'
00127 in EX_BINARY_OUTPUT)
00128 self.assert_('unexpected' not in EX_BINARY_OUTPUT,
00129 'This failure belongs in this test only if '
00130 '"CxxExceptionInConstructorTest" (no quotes) '
00131 'appears on the same line as words "called unexpectedly"')
00132
00133 if ('CxxExceptionInDestructorTest.ThrowsExceptionInDestructor' in
00134 EX_BINARY_OUTPUT):
00135
00136 def testCatchesCxxExceptionsInFixtureDestructor(self):
00137 self.assert_('C++ exception with description '
00138 '"Standard C++ exception" thrown '
00139 'in the test fixture\'s destructor'
00140 in EX_BINARY_OUTPUT)
00141 self.assert_('CxxExceptionInDestructorTest::TearDownTestCase() '
00142 'called as expected.'
00143 in EX_BINARY_OUTPUT)
00144
00145 def testCatchesCxxExceptionsInSetUpTestCase(self):
00146 self.assert_('C++ exception with description "Standard C++ exception"'
00147 ' thrown in SetUpTestCase()'
00148 in EX_BINARY_OUTPUT)
00149 self.assert_('CxxExceptionInConstructorTest::TearDownTestCase() '
00150 'called as expected.'
00151 in EX_BINARY_OUTPUT)
00152 self.assert_('CxxExceptionInSetUpTestCaseTest constructor '
00153 'called as expected.'
00154 in EX_BINARY_OUTPUT)
00155 self.assert_('CxxExceptionInSetUpTestCaseTest destructor '
00156 'called as expected.'
00157 in EX_BINARY_OUTPUT)
00158 self.assert_('CxxExceptionInSetUpTestCaseTest::SetUp() '
00159 'called as expected.'
00160 in EX_BINARY_OUTPUT)
00161 self.assert_('CxxExceptionInSetUpTestCaseTest::TearDown() '
00162 'called as expected.'
00163 in EX_BINARY_OUTPUT)
00164 self.assert_('CxxExceptionInSetUpTestCaseTest test body '
00165 'called as expected.'
00166 in EX_BINARY_OUTPUT)
00167
00168 def testCatchesCxxExceptionsInTearDownTestCase(self):
00169 self.assert_('C++ exception with description "Standard C++ exception"'
00170 ' thrown in TearDownTestCase()'
00171 in EX_BINARY_OUTPUT)
00172
00173 def testCatchesCxxExceptionsInSetUp(self):
00174 self.assert_('C++ exception with description "Standard C++ exception"'
00175 ' thrown in SetUp()'
00176 in EX_BINARY_OUTPUT)
00177 self.assert_('CxxExceptionInSetUpTest::TearDownTestCase() '
00178 'called as expected.'
00179 in EX_BINARY_OUTPUT)
00180 self.assert_('CxxExceptionInSetUpTest destructor '
00181 'called as expected.'
00182 in EX_BINARY_OUTPUT)
00183 self.assert_('CxxExceptionInSetUpTest::TearDown() '
00184 'called as expected.'
00185 in EX_BINARY_OUTPUT)
00186 self.assert_('unexpected' not in EX_BINARY_OUTPUT,
00187 'This failure belongs in this test only if '
00188 '"CxxExceptionInSetUpTest" (no quotes) '
00189 'appears on the same line as words "called unexpectedly"')
00190
00191 def testCatchesCxxExceptionsInTearDown(self):
00192 self.assert_('C++ exception with description "Standard C++ exception"'
00193 ' thrown in TearDown()'
00194 in EX_BINARY_OUTPUT)
00195 self.assert_('CxxExceptionInTearDownTest::TearDownTestCase() '
00196 'called as expected.'
00197 in EX_BINARY_OUTPUT)
00198 self.assert_('CxxExceptionInTearDownTest destructor '
00199 'called as expected.'
00200 in EX_BINARY_OUTPUT)
00201
00202 def testCatchesCxxExceptionsInTestBody(self):
00203 self.assert_('C++ exception with description "Standard C++ exception"'
00204 ' thrown in the test body'
00205 in EX_BINARY_OUTPUT)
00206 self.assert_('CxxExceptionInTestBodyTest::TearDownTestCase() '
00207 'called as expected.'
00208 in EX_BINARY_OUTPUT)
00209 self.assert_('CxxExceptionInTestBodyTest destructor '
00210 'called as expected.'
00211 in EX_BINARY_OUTPUT)
00212 self.assert_('CxxExceptionInTestBodyTest::TearDown() '
00213 'called as expected.'
00214 in EX_BINARY_OUTPUT)
00215
00216 def testCatchesNonStdCxxExceptions(self):
00217 self.assert_('Unknown C++ exception thrown in the test body'
00218 in EX_BINARY_OUTPUT)
00219
00220 def testUnhandledCxxExceptionsAbortTheProgram(self):
00221
00222
00223 FITLER_OUT_SEH_TESTS_FLAG = FILTER_FLAG + '=-*Seh*'
00224
00225 uncaught_exceptions_ex_binary_output = gtest_test_utils.Subprocess(
00226 [EX_EXE_PATH,
00227 NO_CATCH_EXCEPTIONS_FLAG,
00228 FITLER_OUT_SEH_TESTS_FLAG],
00229 env=environ).output
00230
00231 self.assert_('Unhandled C++ exception terminating the program'
00232 in uncaught_exceptions_ex_binary_output)
00233 self.assert_('unexpected' not in uncaught_exceptions_ex_binary_output)
00234
00235
00236 if __name__ == '__main__':
00237 gtest_test_utils.Main()