3 # Copyright 2010 Google Inc. All Rights Reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following disclaimer
13 # in the documentation and/or other materials provided with the
15 # * Neither the name of Google Inc. nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 """Tests Google Test's exception catching behavior.
33 This script invokes gtest_catch_exceptions_test_ and
34 gtest_catch_exceptions_ex_test_ (programs written with
35 Google Test) and verifies their output.
38 __author__ = 'vladl@google.com (Vlad Losev)'
42 import gtest_test_utils
45 FLAG_PREFIX = '--gtest_'
46 LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests'
47 NO_CATCH_EXCEPTIONS_FLAG = FLAG_PREFIX + 'catch_exceptions=0'
48 FILTER_FLAG = FLAG_PREFIX + 'filter'
50 # Path to the gtest_catch_exceptions_ex_test_ binary, compiled with
52 EX_EXE_PATH = gtest_test_utils.GetTestExecutablePath(
53 'gtest_catch_exceptions_ex_test_')
55 # Path to the gtest_catch_exceptions_test_ binary, compiled with
56 # exceptions disabled.
57 EXE_PATH = gtest_test_utils.GetTestExecutablePath(
58 'gtest_catch_exceptions_no_ex_test_')
60 environ = gtest_test_utils.environ
61 SetEnvVar = gtest_test_utils.SetEnvVar
63 # Tests in this file run a Google-Test-based test program and expect it
64 # to terminate prematurely. Therefore they are incompatible with
65 # the premature-exit-file protocol by design. Unset the
66 # premature-exit filepath to prevent Google Test from creating
68 SetEnvVar(gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR, None)
70 TEST_LIST = gtest_test_utils.Subprocess(
71 [EXE_PATH, LIST_TESTS_FLAG], env=environ).output
73 SUPPORTS_SEH_EXCEPTIONS = 'ThrowsSehException' in TEST_LIST
75 if SUPPORTS_SEH_EXCEPTIONS:
76 BINARY_OUTPUT = gtest_test_utils.Subprocess([EXE_PATH], env=environ).output
78 EX_BINARY_OUTPUT = gtest_test_utils.Subprocess(
79 [EX_EXE_PATH], env=environ).output
83 if SUPPORTS_SEH_EXCEPTIONS:
84 # pylint:disable-msg=C6302
85 class CatchSehExceptionsTest(gtest_test_utils.TestCase):
86 """Tests exception-catching behavior."""
89 def TestSehExceptions(self, test_output):
90 self.assert_('SEH exception with code 0x2a thrown '
91 'in the test fixture\'s constructor'
93 self.assert_('SEH exception with code 0x2a thrown '
94 'in the test fixture\'s destructor'
96 self.assert_('SEH exception with code 0x2a thrown in SetUpTestCase()'
98 self.assert_('SEH exception with code 0x2a thrown in TearDownTestCase()'
100 self.assert_('SEH exception with code 0x2a thrown in SetUp()'
102 self.assert_('SEH exception with code 0x2a thrown in TearDown()'
104 self.assert_('SEH exception with code 0x2a thrown in the test body'
107 def testCatchesSehExceptionsWithCxxExceptionsEnabled(self):
108 self.TestSehExceptions(EX_BINARY_OUTPUT)
110 def testCatchesSehExceptionsWithCxxExceptionsDisabled(self):
111 self.TestSehExceptions(BINARY_OUTPUT)
114 class CatchCxxExceptionsTest(gtest_test_utils.TestCase):
115 """Tests C++ exception-catching behavior.
117 Tests in this test case verify that:
118 * C++ exceptions are caught and logged as C++ (not SEH) exceptions
119 * Exception thrown affect the remainder of the test work flow in the
123 def testCatchesCxxExceptionsInFixtureConstructor(self):
124 self.assert_('C++ exception with description '
125 '"Standard C++ exception" thrown '
126 'in the test fixture\'s constructor'
128 self.assert_('unexpected' not in EX_BINARY_OUTPUT,
129 'This failure belongs in this test only if '
130 '"CxxExceptionInConstructorTest" (no quotes) '
131 'appears on the same line as words "called unexpectedly"')
133 if ('CxxExceptionInDestructorTest.ThrowsExceptionInDestructor' in
136 def testCatchesCxxExceptionsInFixtureDestructor(self):
137 self.assert_('C++ exception with description '
138 '"Standard C++ exception" thrown '
139 'in the test fixture\'s destructor'
141 self.assert_('CxxExceptionInDestructorTest::TearDownTestCase() '
142 'called as expected.'
145 def testCatchesCxxExceptionsInSetUpTestCase(self):
146 self.assert_('C++ exception with description "Standard C++ exception"'
147 ' thrown in SetUpTestCase()'
149 self.assert_('CxxExceptionInConstructorTest::TearDownTestCase() '
150 'called as expected.'
152 self.assert_('CxxExceptionInSetUpTestCaseTest constructor '
153 'called as expected.'
155 self.assert_('CxxExceptionInSetUpTestCaseTest destructor '
156 'called as expected.'
158 self.assert_('CxxExceptionInSetUpTestCaseTest::SetUp() '
159 'called as expected.'
161 self.assert_('CxxExceptionInSetUpTestCaseTest::TearDown() '
162 'called as expected.'
164 self.assert_('CxxExceptionInSetUpTestCaseTest test body '
165 'called as expected.'
168 def testCatchesCxxExceptionsInTearDownTestCase(self):
169 self.assert_('C++ exception with description "Standard C++ exception"'
170 ' thrown in TearDownTestCase()'
173 def testCatchesCxxExceptionsInSetUp(self):
174 self.assert_('C++ exception with description "Standard C++ exception"'
177 self.assert_('CxxExceptionInSetUpTest::TearDownTestCase() '
178 'called as expected.'
180 self.assert_('CxxExceptionInSetUpTest destructor '
181 'called as expected.'
183 self.assert_('CxxExceptionInSetUpTest::TearDown() '
184 'called as expected.'
186 self.assert_('unexpected' not in EX_BINARY_OUTPUT,
187 'This failure belongs in this test only if '
188 '"CxxExceptionInSetUpTest" (no quotes) '
189 'appears on the same line as words "called unexpectedly"')
191 def testCatchesCxxExceptionsInTearDown(self):
192 self.assert_('C++ exception with description "Standard C++ exception"'
193 ' thrown in TearDown()'
195 self.assert_('CxxExceptionInTearDownTest::TearDownTestCase() '
196 'called as expected.'
198 self.assert_('CxxExceptionInTearDownTest destructor '
199 'called as expected.'
202 def testCatchesCxxExceptionsInTestBody(self):
203 self.assert_('C++ exception with description "Standard C++ exception"'
204 ' thrown in the test body'
206 self.assert_('CxxExceptionInTestBodyTest::TearDownTestCase() '
207 'called as expected.'
209 self.assert_('CxxExceptionInTestBodyTest destructor '
210 'called as expected.'
212 self.assert_('CxxExceptionInTestBodyTest::TearDown() '
213 'called as expected.'
216 def testCatchesNonStdCxxExceptions(self):
217 self.assert_('Unknown C++ exception thrown in the test body'
220 def testUnhandledCxxExceptionsAbortTheProgram(self):
221 # Filters out SEH exception tests on Windows. Unhandled SEH exceptions
222 # cause tests to show pop-up windows there.
223 FITLER_OUT_SEH_TESTS_FLAG = FILTER_FLAG + '=-*Seh*'
224 # By default, Google Test doesn't catch the exceptions.
225 uncaught_exceptions_ex_binary_output = gtest_test_utils.Subprocess(
227 NO_CATCH_EXCEPTIONS_FLAG,
228 FITLER_OUT_SEH_TESTS_FLAG],
231 self.assert_('Unhandled C++ exception terminating the program'
232 in uncaught_exceptions_ex_binary_output)
233 self.assert_('unexpected' not in uncaught_exceptions_ex_binary_output)
236 if __name__ == '__main__':
237 gtest_test_utils.Main()