googletest-list-tests-unittest.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2006, Google Inc.
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. #
  10. # * Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # * Redistributions in binary form must reproduce the above
  13. # copyright notice, this list of conditions and the following disclaimer
  14. # in the documentation and/or other materials provided with the
  15. # distribution.
  16. # * Neither the name of Google Inc. nor the names of its
  17. # contributors may be used to endorse or promote products derived from
  18. # this software without specific prior written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. """Unit test for Google Test's --gtest_list_tests flag.
  32. A user can ask Google Test to list all tests by specifying the
  33. --gtest_list_tests flag. This script tests such functionality
  34. by invoking googletest-list-tests-unittest_ (a program written with
  35. Google Test) the command line flags.
  36. """
  37. import re
  38. import gtest_test_utils
  39. # Constants.
  40. # The command line flag for enabling/disabling listing all tests.
  41. LIST_TESTS_FLAG = 'gtest_list_tests'
  42. # Path to the googletest-list-tests-unittest_ program.
  43. EXE_PATH = gtest_test_utils.GetTestExecutablePath('googletest-list-tests-unittest_')
  44. # The expected output when running googletest-list-tests-unittest_ with
  45. # --gtest_list_tests
  46. EXPECTED_OUTPUT_NO_FILTER_RE = re.compile(r"""FooDeathTest\.
  47. Test1
  48. Foo\.
  49. Bar1
  50. Bar2
  51. DISABLED_Bar3
  52. Abc\.
  53. Xyz
  54. Def
  55. FooBar\.
  56. Baz
  57. FooTest\.
  58. Test1
  59. DISABLED_Test2
  60. Test3
  61. TypedTest/0\. # TypeParam = (VeryLo{245}|class VeryLo{239})\.\.\.
  62. TestA
  63. TestB
  64. TypedTest/1\. # TypeParam = int\s*\*( __ptr64)?
  65. TestA
  66. TestB
  67. TypedTest/2\. # TypeParam = .*MyArray<bool,\s*42>
  68. TestA
  69. TestB
  70. My/TypeParamTest/0\. # TypeParam = (VeryLo{245}|class VeryLo{239})\.\.\.
  71. TestA
  72. TestB
  73. My/TypeParamTest/1\. # TypeParam = int\s*\*( __ptr64)?
  74. TestA
  75. TestB
  76. My/TypeParamTest/2\. # TypeParam = .*MyArray<bool,\s*42>
  77. TestA
  78. TestB
  79. MyInstantiation/ValueParamTest\.
  80. TestA/0 # GetParam\(\) = one line
  81. TestA/1 # GetParam\(\) = two\\nlines
  82. TestA/2 # GetParam\(\) = a very\\nlo{241}\.\.\.
  83. TestB/0 # GetParam\(\) = one line
  84. TestB/1 # GetParam\(\) = two\\nlines
  85. TestB/2 # GetParam\(\) = a very\\nlo{241}\.\.\.
  86. """)
  87. # The expected output when running googletest-list-tests-unittest_ with
  88. # --gtest_list_tests and --gtest_filter=Foo*.
  89. EXPECTED_OUTPUT_FILTER_FOO_RE = re.compile(r"""FooDeathTest\.
  90. Test1
  91. Foo\.
  92. Bar1
  93. Bar2
  94. DISABLED_Bar3
  95. FooBar\.
  96. Baz
  97. FooTest\.
  98. Test1
  99. DISABLED_Test2
  100. Test3
  101. """)
  102. # Utilities.
  103. def Run(args):
  104. """Runs googletest-list-tests-unittest_ and returns the list of tests printed."""
  105. return gtest_test_utils.Subprocess([EXE_PATH] + args,
  106. capture_stderr=False).output
  107. # The unit test.
  108. class GTestListTestsUnitTest(gtest_test_utils.TestCase):
  109. """Tests using the --gtest_list_tests flag to list all tests."""
  110. def RunAndVerify(self, flag_value, expected_output_re, other_flag):
  111. """Runs googletest-list-tests-unittest_ and verifies that it prints
  112. the correct tests.
  113. Args:
  114. flag_value: value of the --gtest_list_tests flag;
  115. None if the flag should not be present.
  116. expected_output_re: regular expression that matches the expected
  117. output after running command;
  118. other_flag: a different flag to be passed to command
  119. along with gtest_list_tests;
  120. None if the flag should not be present.
  121. """
  122. if flag_value is None:
  123. flag = ''
  124. flag_expression = 'not set'
  125. elif flag_value == '0':
  126. flag = '--%s=0' % LIST_TESTS_FLAG
  127. flag_expression = '0'
  128. else:
  129. flag = '--%s' % LIST_TESTS_FLAG
  130. flag_expression = '1'
  131. args = [flag]
  132. if other_flag is not None:
  133. args += [other_flag]
  134. output = Run(args)
  135. if expected_output_re:
  136. self.assert_(
  137. expected_output_re.match(output),
  138. ('when %s is %s, the output of "%s" is "%s",\n'
  139. 'which does not match regex "%s"' %
  140. (LIST_TESTS_FLAG, flag_expression, ' '.join(args), output,
  141. expected_output_re.pattern)))
  142. else:
  143. self.assert_(
  144. not EXPECTED_OUTPUT_NO_FILTER_RE.match(output),
  145. ('when %s is %s, the output of "%s" is "%s"'%
  146. (LIST_TESTS_FLAG, flag_expression, ' '.join(args), output)))
  147. def testDefaultBehavior(self):
  148. """Tests the behavior of the default mode."""
  149. self.RunAndVerify(flag_value=None,
  150. expected_output_re=None,
  151. other_flag=None)
  152. def testFlag(self):
  153. """Tests using the --gtest_list_tests flag."""
  154. self.RunAndVerify(flag_value='0',
  155. expected_output_re=None,
  156. other_flag=None)
  157. self.RunAndVerify(flag_value='1',
  158. expected_output_re=EXPECTED_OUTPUT_NO_FILTER_RE,
  159. other_flag=None)
  160. def testOverrideNonFilterFlags(self):
  161. """Tests that --gtest_list_tests overrides the non-filter flags."""
  162. self.RunAndVerify(flag_value='1',
  163. expected_output_re=EXPECTED_OUTPUT_NO_FILTER_RE,
  164. other_flag='--gtest_break_on_failure')
  165. def testWithFilterFlags(self):
  166. """Tests that --gtest_list_tests takes into account the
  167. --gtest_filter flag."""
  168. self.RunAndVerify(flag_value='1',
  169. expected_output_re=EXPECTED_OUTPUT_FILTER_FOO_RE,
  170. other_flag='--gtest_filter=Foo*')
  171. if __name__ == '__main__':
  172. gtest_test_utils.Main()