googletest-catch-exceptions-test.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. from googletest.test 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. )
  46. # Path to the googletest-catch-exceptions-test_ binary, compiled with
  47. # exceptions disabled.
  48. EXE_PATH = gtest_test_utils.GetTestExecutablePath(
  49. 'googletest-catch-exceptions-no-ex-test_'
  50. )
  51. environ = gtest_test_utils.environ
  52. SetEnvVar = gtest_test_utils.SetEnvVar
  53. # Tests in this file run a Google-Test-based test program and expect it
  54. # to terminate prematurely. Therefore they are incompatible with
  55. # the premature-exit-file protocol by design. Unset the
  56. # premature-exit filepath to prevent Google Test from creating
  57. # the file.
  58. SetEnvVar(gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR, None)
  59. TEST_LIST = gtest_test_utils.Subprocess(
  60. [EXE_PATH, LIST_TESTS_FLAG], env=environ
  61. ).output
  62. SUPPORTS_SEH_EXCEPTIONS = 'ThrowsSehException' in TEST_LIST
  63. if SUPPORTS_SEH_EXCEPTIONS:
  64. BINARY_OUTPUT = gtest_test_utils.Subprocess([EXE_PATH], env=environ).output
  65. EX_BINARY_OUTPUT = gtest_test_utils.Subprocess(
  66. [EX_EXE_PATH], env=environ
  67. ).output
  68. # The tests.
  69. if SUPPORTS_SEH_EXCEPTIONS:
  70. class CatchSehExceptionsTest(gtest_test_utils.TestCase):
  71. """Tests exception-catching behavior."""
  72. def TestSehExceptions(self, test_output):
  73. self.assertIn(
  74. (
  75. 'SEH exception with code 0x2a thrown '
  76. "in the test fixture's constructor"
  77. ),
  78. test_output,
  79. )
  80. self.assertIn(
  81. (
  82. 'SEH exception with code 0x2a thrown '
  83. "in the test fixture's destructor"
  84. ),
  85. test_output,
  86. )
  87. self.assertIn(
  88. 'SEH exception with code 0x2a thrown in SetUpTestSuite()', test_output
  89. )
  90. self.assertIn(
  91. 'SEH exception with code 0x2a thrown in TearDownTestSuite()',
  92. test_output,
  93. )
  94. self.assertIn(
  95. 'SEH exception with code 0x2a thrown in SetUp()', test_output
  96. )
  97. self.assertIn(
  98. 'SEH exception with code 0x2a thrown in TearDown()', test_output
  99. )
  100. self.assertIn(
  101. 'SEH exception with code 0x2a thrown in the test body', test_output
  102. )
  103. def testCatchesSehExceptionsWithCxxExceptionsEnabled(self):
  104. self.TestSehExceptions(EX_BINARY_OUTPUT)
  105. def testCatchesSehExceptionsWithCxxExceptionsDisabled(self):
  106. self.TestSehExceptions(BINARY_OUTPUT)
  107. class CatchCxxExceptionsTest(gtest_test_utils.TestCase):
  108. """Tests C++ exception-catching behavior.
  109. Tests in this test case verify that:
  110. * C++ exceptions are caught and logged as C++ (not SEH) exceptions
  111. * Exception thrown affect the remainder of the test work flow in the
  112. expected manner.
  113. """
  114. def testCatchesCxxExceptionsInFixtureConstructor(self):
  115. self.assertTrue(
  116. 'C++ exception with description '
  117. '"Standard C++ exception" thrown '
  118. "in the test fixture's constructor"
  119. in EX_BINARY_OUTPUT,
  120. EX_BINARY_OUTPUT,
  121. )
  122. self.assertTrue(
  123. 'unexpected' not in EX_BINARY_OUTPUT,
  124. (
  125. 'This failure belongs in this test only if '
  126. '"CxxExceptionInConstructorTest" (no quotes) '
  127. 'appears on the same line as words "called unexpectedly"'
  128. ),
  129. )
  130. if (
  131. 'CxxExceptionInDestructorTest.ThrowsExceptionInDestructor'
  132. in EX_BINARY_OUTPUT
  133. ):
  134. def testCatchesCxxExceptionsInFixtureDestructor(self):
  135. self.assertTrue(
  136. 'C++ exception with description '
  137. '"Standard C++ exception" thrown '
  138. "in the test fixture's destructor"
  139. in EX_BINARY_OUTPUT,
  140. EX_BINARY_OUTPUT,
  141. )
  142. self.assertTrue(
  143. 'CxxExceptionInDestructorTest::TearDownTestSuite() '
  144. 'called as expected.'
  145. in EX_BINARY_OUTPUT,
  146. EX_BINARY_OUTPUT,
  147. )
  148. def testCatchesCxxExceptionsInSetUpTestCase(self):
  149. self.assertTrue(
  150. 'C++ exception with description "Standard C++ exception"'
  151. ' thrown in SetUpTestSuite()'
  152. in EX_BINARY_OUTPUT,
  153. EX_BINARY_OUTPUT,
  154. )
  155. self.assertTrue(
  156. 'CxxExceptionInConstructorTest::TearDownTestSuite() called as expected.'
  157. in EX_BINARY_OUTPUT,
  158. EX_BINARY_OUTPUT,
  159. )
  160. self.assertFalse(
  161. 'CxxExceptionInSetUpTestSuiteTest constructor called as expected.'
  162. in EX_BINARY_OUTPUT,
  163. EX_BINARY_OUTPUT,
  164. )
  165. self.assertFalse(
  166. 'CxxExceptionInSetUpTestSuiteTest destructor called as expected.'
  167. in EX_BINARY_OUTPUT,
  168. EX_BINARY_OUTPUT,
  169. )
  170. self.assertFalse(
  171. 'CxxExceptionInSetUpTestSuiteTest::SetUp() called as expected.'
  172. in EX_BINARY_OUTPUT,
  173. EX_BINARY_OUTPUT,
  174. )
  175. self.assertFalse(
  176. 'CxxExceptionInSetUpTestSuiteTest::TearDown() called as expected.'
  177. in EX_BINARY_OUTPUT,
  178. EX_BINARY_OUTPUT,
  179. )
  180. self.assertFalse(
  181. 'CxxExceptionInSetUpTestSuiteTest test body called as expected.'
  182. in EX_BINARY_OUTPUT,
  183. EX_BINARY_OUTPUT,
  184. )
  185. def testCatchesCxxExceptionsInTearDownTestCase(self):
  186. self.assertTrue(
  187. 'C++ exception with description "Standard C++ exception"'
  188. ' thrown in TearDownTestSuite()'
  189. in EX_BINARY_OUTPUT,
  190. EX_BINARY_OUTPUT,
  191. )
  192. def testCatchesCxxExceptionsInSetUp(self):
  193. self.assertTrue(
  194. 'C++ exception with description "Standard C++ exception"'
  195. ' thrown in SetUp()'
  196. in EX_BINARY_OUTPUT,
  197. EX_BINARY_OUTPUT,
  198. )
  199. self.assertTrue(
  200. 'CxxExceptionInSetUpTest::TearDownTestSuite() called as expected.'
  201. in EX_BINARY_OUTPUT,
  202. EX_BINARY_OUTPUT,
  203. )
  204. self.assertTrue(
  205. 'CxxExceptionInSetUpTest destructor called as expected.'
  206. in EX_BINARY_OUTPUT,
  207. EX_BINARY_OUTPUT,
  208. )
  209. self.assertTrue(
  210. 'CxxExceptionInSetUpTest::TearDown() called as expected.'
  211. in EX_BINARY_OUTPUT,
  212. EX_BINARY_OUTPUT,
  213. )
  214. self.assertTrue(
  215. 'unexpected' not in EX_BINARY_OUTPUT,
  216. (
  217. 'This failure belongs in this test only if '
  218. '"CxxExceptionInSetUpTest" (no quotes) '
  219. 'appears on the same line as words "called unexpectedly"'
  220. ),
  221. )
  222. def testCatchesCxxExceptionsInTearDown(self):
  223. self.assertTrue(
  224. 'C++ exception with description "Standard C++ exception"'
  225. ' thrown in TearDown()'
  226. in EX_BINARY_OUTPUT,
  227. EX_BINARY_OUTPUT,
  228. )
  229. self.assertTrue(
  230. 'CxxExceptionInTearDownTest::TearDownTestSuite() called as expected.'
  231. in EX_BINARY_OUTPUT,
  232. EX_BINARY_OUTPUT,
  233. )
  234. self.assertTrue(
  235. 'CxxExceptionInTearDownTest destructor called as expected.'
  236. in EX_BINARY_OUTPUT,
  237. EX_BINARY_OUTPUT,
  238. )
  239. def testCatchesCxxExceptionsInTestBody(self):
  240. self.assertTrue(
  241. 'C++ exception with description "Standard C++ exception"'
  242. ' thrown in the test body'
  243. in EX_BINARY_OUTPUT,
  244. EX_BINARY_OUTPUT,
  245. )
  246. self.assertTrue(
  247. 'CxxExceptionInTestBodyTest::TearDownTestSuite() called as expected.'
  248. in EX_BINARY_OUTPUT,
  249. EX_BINARY_OUTPUT,
  250. )
  251. self.assertTrue(
  252. 'CxxExceptionInTestBodyTest destructor called as expected.'
  253. in EX_BINARY_OUTPUT,
  254. EX_BINARY_OUTPUT,
  255. )
  256. self.assertTrue(
  257. 'CxxExceptionInTestBodyTest::TearDown() called as expected.'
  258. in EX_BINARY_OUTPUT,
  259. EX_BINARY_OUTPUT,
  260. )
  261. def testCatchesNonStdCxxExceptions(self):
  262. self.assertTrue(
  263. 'Unknown C++ exception thrown in the test body' in EX_BINARY_OUTPUT,
  264. EX_BINARY_OUTPUT,
  265. )
  266. def testUnhandledCxxExceptionsAbortTheProgram(self):
  267. # Filters out SEH exception tests on Windows. Unhandled SEH exceptions
  268. # cause tests to show pop-up windows there.
  269. filter_out_seh_tests_flag = FILTER_FLAG + '=-*Seh*'
  270. # By default, Google Test doesn't catch the exceptions.
  271. uncaught_exceptions_ex_binary_output = gtest_test_utils.Subprocess(
  272. [EX_EXE_PATH, NO_CATCH_EXCEPTIONS_FLAG, filter_out_seh_tests_flag],
  273. env=environ,
  274. ).output
  275. self.assertIn(
  276. 'Unhandled C++ exception terminating the program',
  277. uncaught_exceptions_ex_binary_output,
  278. )
  279. self.assertNotIn('unexpected', uncaught_exceptions_ex_binary_output)
  280. if __name__ == '__main__':
  281. gtest_test_utils.Main()