googletest-catch-exceptions-test.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2010 Google Inc. All Rights Reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are
  7. # met:
  8. #
  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
  14. # distribution.
  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.
  18. #
  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.
  30. """Tests Google Test's exception catching behavior.
  31. This script invokes googletest-catch-exceptions-test_ and
  32. googletest-catch-exceptions-ex-test_ (programs written with
  33. Google Test) and verifies their output.
  34. """
  35. import gtest_test_utils
  36. # Constants.
  37. FLAG_PREFIX = '--gtest_'
  38. LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests'
  39. NO_CATCH_EXCEPTIONS_FLAG = FLAG_PREFIX + 'catch_exceptions=0'
  40. FILTER_FLAG = FLAG_PREFIX + 'filter'
  41. # Path to the googletest-catch-exceptions-ex-test_ binary, compiled with
  42. # exceptions enabled.
  43. EX_EXE_PATH = gtest_test_utils.GetTestExecutablePath(
  44. 'googletest-catch-exceptions-ex-test_')
  45. # Path to the googletest-catch-exceptions-test_ binary, compiled with
  46. # exceptions disabled.
  47. EXE_PATH = gtest_test_utils.GetTestExecutablePath(
  48. 'googletest-catch-exceptions-no-ex-test_')
  49. environ = gtest_test_utils.environ
  50. SetEnvVar = gtest_test_utils.SetEnvVar
  51. # Tests in this file run a Google-Test-based test program and expect it
  52. # to terminate prematurely. Therefore they are incompatible with
  53. # the premature-exit-file protocol by design. Unset the
  54. # premature-exit filepath to prevent Google Test from creating
  55. # the file.
  56. SetEnvVar(gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR, None)
  57. TEST_LIST = gtest_test_utils.Subprocess(
  58. [EXE_PATH, LIST_TESTS_FLAG], env=environ).output
  59. SUPPORTS_SEH_EXCEPTIONS = 'ThrowsSehException' in TEST_LIST
  60. if SUPPORTS_SEH_EXCEPTIONS:
  61. BINARY_OUTPUT = gtest_test_utils.Subprocess([EXE_PATH], env=environ).output
  62. EX_BINARY_OUTPUT = gtest_test_utils.Subprocess(
  63. [EX_EXE_PATH], env=environ).output
  64. # The tests.
  65. if SUPPORTS_SEH_EXCEPTIONS:
  66. # pylint:disable-msg=C6302
  67. class CatchSehExceptionsTest(gtest_test_utils.TestCase):
  68. """Tests exception-catching behavior."""
  69. def TestSehExceptions(self, test_output):
  70. self.assert_('SEH exception with code 0x2a thrown '
  71. 'in the test fixture\'s constructor'
  72. in test_output)
  73. self.assert_('SEH exception with code 0x2a thrown '
  74. 'in the test fixture\'s destructor'
  75. in test_output)
  76. self.assert_('SEH exception with code 0x2a thrown in SetUpTestCase()'
  77. in test_output)
  78. self.assert_('SEH exception with code 0x2a thrown in TearDownTestCase()'
  79. in test_output)
  80. self.assert_('SEH exception with code 0x2a thrown in SetUp()'
  81. in test_output)
  82. self.assert_('SEH exception with code 0x2a thrown in TearDown()'
  83. in test_output)
  84. self.assert_('SEH exception with code 0x2a thrown in the test body'
  85. in test_output)
  86. def testCatchesSehExceptionsWithCxxExceptionsEnabled(self):
  87. self.TestSehExceptions(EX_BINARY_OUTPUT)
  88. def testCatchesSehExceptionsWithCxxExceptionsDisabled(self):
  89. self.TestSehExceptions(BINARY_OUTPUT)
  90. class CatchCxxExceptionsTest(gtest_test_utils.TestCase):
  91. """Tests C++ exception-catching behavior.
  92. Tests in this test case verify that:
  93. * C++ exceptions are caught and logged as C++ (not SEH) exceptions
  94. * Exception thrown affect the remainder of the test work flow in the
  95. expected manner.
  96. """
  97. def testCatchesCxxExceptionsInFixtureConstructor(self):
  98. self.assert_('C++ exception with description '
  99. '"Standard C++ exception" thrown '
  100. 'in the test fixture\'s constructor'
  101. in EX_BINARY_OUTPUT)
  102. self.assert_('unexpected' not in EX_BINARY_OUTPUT,
  103. 'This failure belongs in this test only if '
  104. '"CxxExceptionInConstructorTest" (no quotes) '
  105. 'appears on the same line as words "called unexpectedly"')
  106. if ('CxxExceptionInDestructorTest.ThrowsExceptionInDestructor' in
  107. EX_BINARY_OUTPUT):
  108. def testCatchesCxxExceptionsInFixtureDestructor(self):
  109. self.assert_('C++ exception with description '
  110. '"Standard C++ exception" thrown '
  111. 'in the test fixture\'s destructor'
  112. in EX_BINARY_OUTPUT)
  113. self.assert_('CxxExceptionInDestructorTest::TearDownTestCase() '
  114. 'called as expected.'
  115. in EX_BINARY_OUTPUT)
  116. def testCatchesCxxExceptionsInSetUpTestCase(self):
  117. self.assert_('C++ exception with description "Standard C++ exception"'
  118. ' thrown in SetUpTestCase()'
  119. in EX_BINARY_OUTPUT)
  120. self.assert_('CxxExceptionInConstructorTest::TearDownTestCase() '
  121. 'called as expected.'
  122. in EX_BINARY_OUTPUT)
  123. self.assert_('CxxExceptionInSetUpTestCaseTest constructor '
  124. 'called as expected.'
  125. in EX_BINARY_OUTPUT)
  126. self.assert_('CxxExceptionInSetUpTestCaseTest destructor '
  127. 'called as expected.'
  128. in EX_BINARY_OUTPUT)
  129. self.assert_('CxxExceptionInSetUpTestCaseTest::SetUp() '
  130. 'called as expected.'
  131. in EX_BINARY_OUTPUT)
  132. self.assert_('CxxExceptionInSetUpTestCaseTest::TearDown() '
  133. 'called as expected.'
  134. in EX_BINARY_OUTPUT)
  135. self.assert_('CxxExceptionInSetUpTestCaseTest test body '
  136. 'called as expected.'
  137. in EX_BINARY_OUTPUT)
  138. def testCatchesCxxExceptionsInTearDownTestCase(self):
  139. self.assert_('C++ exception with description "Standard C++ exception"'
  140. ' thrown in TearDownTestCase()'
  141. in EX_BINARY_OUTPUT)
  142. def testCatchesCxxExceptionsInSetUp(self):
  143. self.assert_('C++ exception with description "Standard C++ exception"'
  144. ' thrown in SetUp()'
  145. in EX_BINARY_OUTPUT)
  146. self.assert_('CxxExceptionInSetUpTest::TearDownTestCase() '
  147. 'called as expected.'
  148. in EX_BINARY_OUTPUT)
  149. self.assert_('CxxExceptionInSetUpTest destructor '
  150. 'called as expected.'
  151. in EX_BINARY_OUTPUT)
  152. self.assert_('CxxExceptionInSetUpTest::TearDown() '
  153. 'called as expected.'
  154. in EX_BINARY_OUTPUT)
  155. self.assert_('unexpected' not in EX_BINARY_OUTPUT,
  156. 'This failure belongs in this test only if '
  157. '"CxxExceptionInSetUpTest" (no quotes) '
  158. 'appears on the same line as words "called unexpectedly"')
  159. def testCatchesCxxExceptionsInTearDown(self):
  160. self.assert_('C++ exception with description "Standard C++ exception"'
  161. ' thrown in TearDown()'
  162. in EX_BINARY_OUTPUT)
  163. self.assert_('CxxExceptionInTearDownTest::TearDownTestCase() '
  164. 'called as expected.'
  165. in EX_BINARY_OUTPUT)
  166. self.assert_('CxxExceptionInTearDownTest destructor '
  167. 'called as expected.'
  168. in EX_BINARY_OUTPUT)
  169. def testCatchesCxxExceptionsInTestBody(self):
  170. self.assert_('C++ exception with description "Standard C++ exception"'
  171. ' thrown in the test body'
  172. in EX_BINARY_OUTPUT)
  173. self.assert_('CxxExceptionInTestBodyTest::TearDownTestCase() '
  174. 'called as expected.'
  175. in EX_BINARY_OUTPUT)
  176. self.assert_('CxxExceptionInTestBodyTest destructor '
  177. 'called as expected.'
  178. in EX_BINARY_OUTPUT)
  179. self.assert_('CxxExceptionInTestBodyTest::TearDown() '
  180. 'called as expected.'
  181. in EX_BINARY_OUTPUT)
  182. def testCatchesNonStdCxxExceptions(self):
  183. self.assert_('Unknown C++ exception thrown in the test body'
  184. in EX_BINARY_OUTPUT)
  185. def testUnhandledCxxExceptionsAbortTheProgram(self):
  186. # Filters out SEH exception tests on Windows. Unhandled SEH exceptions
  187. # cause tests to show pop-up windows there.
  188. FITLER_OUT_SEH_TESTS_FLAG = FILTER_FLAG + '=-*Seh*'
  189. # By default, Google Test doesn't catch the exceptions.
  190. uncaught_exceptions_ex_binary_output = gtest_test_utils.Subprocess(
  191. [EX_EXE_PATH,
  192. NO_CATCH_EXCEPTIONS_FLAG,
  193. FITLER_OUT_SEH_TESTS_FLAG],
  194. env=environ).output
  195. self.assert_('Unhandled C++ exception terminating the program'
  196. in uncaught_exceptions_ex_binary_output)
  197. self.assert_('unexpected' not in uncaught_exceptions_ex_binary_output)
  198. if __name__ == '__main__':
  199. gtest_test_utils.Main()