gtest_catch_exceptions_test.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2010 Google Inc.  All Rights Reserved.
00004 #
00005 # Redistribution and use in source and binary forms, with or without
00006 # modification, are permitted provided that the following conditions are
00007 # met:
00008 #
00009 #     * Redistributions of source code must retain the above copyright
00010 # notice, this list of conditions and the following disclaimer.
00011 #     * Redistributions in binary form must reproduce the above
00012 # copyright notice, this list of conditions and the following disclaimer
00013 # in the documentation and/or other materials provided with the
00014 # distribution.
00015 #     * Neither the name of Google Inc. nor the names of its
00016 # contributors may be used to endorse or promote products derived from
00017 # this software without specific prior written permission.
00018 #
00019 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00022 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00023 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00025 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00026 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00027 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00029 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 # Constants.
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 # Path to the gtest_catch_exceptions_ex_test_ binary, compiled with
00051 # exceptions enabled.
00052 EX_EXE_PATH = gtest_test_utils.GetTestExecutablePath(
00053     'gtest_catch_exceptions_ex_test_')
00054 
00055 # Path to the gtest_catch_exceptions_test_ binary, compiled with
00056 # exceptions disabled.
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 # Tests in this file run a Google-Test-based test program and expect it
00064 # to terminate prematurely.  Therefore they are incompatible with
00065 # the premature-exit-file protocol by design.  Unset the
00066 # premature-exit filepath to prevent Google Test from creating
00067 # the file.
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 # The tests.
00083 if SUPPORTS_SEH_EXCEPTIONS:
00084   # pylint:disable-msg=C6302
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     # Filters out SEH exception tests on Windows. Unhandled SEH exceptions
00222     # cause tests to show pop-up windows there.
00223     FITLER_OUT_SEH_TESTS_FLAG = FILTER_FLAG + '=-*Seh*'
00224     # By default, Google Test doesn't catch the exceptions.
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()


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:04