gtest_list_tests_unittest.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 gtest_list_tests_unittest_ (a program written with
  35. Google Test) the command line flags.
  36. """
  37. __author__ = 'phanna@google.com (Patrick Hanna)'
  38. import gtest_test_utils
  39. import re
  40. # Constants.
  41. # The command line flag for enabling/disabling listing all tests.
  42. LIST_TESTS_FLAG = 'gtest_list_tests'
  43. # Path to the gtest_list_tests_unittest_ program.
  44. EXE_PATH = gtest_test_utils.GetTestExecutablePath('gtest_list_tests_unittest_')
  45. # The expected output when running gtest_list_tests_unittest_ with
  46. # --gtest_list_tests
  47. EXPECTED_OUTPUT_NO_FILTER_RE = re.compile(r"""FooDeathTest\.
  48. Test1
  49. Foo\.
  50. Bar1
  51. Bar2
  52. DISABLED_Bar3
  53. Abc\.
  54. Xyz
  55. Def
  56. FooBar\.
  57. Baz
  58. FooTest\.
  59. Test1
  60. DISABLED_Test2
  61. Test3
  62. TypedTest/0\. # TypeParam = (VeryLo{245}|class VeryLo{239})\.\.\.
  63. TestA
  64. TestB
  65. TypedTest/1\. # TypeParam = int\s*\*
  66. TestA
  67. TestB
  68. TypedTest/2\. # TypeParam = .*MyArray<bool,\s*42>
  69. TestA
  70. TestB
  71. My/TypeParamTest/0\. # TypeParam = (VeryLo{245}|class VeryLo{239})\.\.\.
  72. TestA
  73. TestB
  74. My/TypeParamTest/1\. # TypeParam = int\s*\*
  75. TestA
  76. TestB
  77. My/TypeParamTest/2\. # TypeParam = .*MyArray<bool,\s*42>
  78. TestA
  79. TestB
  80. MyInstantiation/ValueParamTest\.
  81. TestA/0 # GetParam\(\) = one line
  82. TestA/1 # GetParam\(\) = two\\nlines
  83. TestA/2 # GetParam\(\) = a very\\nlo{241}\.\.\.
  84. TestB/0 # GetParam\(\) = one line
  85. TestB/1 # GetParam\(\) = two\\nlines
  86. TestB/2 # GetParam\(\) = a very\\nlo{241}\.\.\.
  87. """)
  88. # The expected output when running gtest_list_tests_unittest_ with
  89. # --gtest_list_tests and --gtest_filter=Foo*.
  90. EXPECTED_OUTPUT_FILTER_FOO_RE = re.compile(r"""FooDeathTest\.
  91. Test1
  92. Foo\.
  93. Bar1
  94. Bar2
  95. DISABLED_Bar3
  96. FooBar\.
  97. Baz
  98. FooTest\.
  99. Test1
  100. DISABLED_Test2
  101. Test3
  102. """)
  103. # Utilities.
  104. def Run(args):
  105. """Runs gtest_list_tests_unittest_ and returns the list of tests printed."""
  106. return gtest_test_utils.Subprocess([EXE_PATH] + args,
  107. capture_stderr=False).output
  108. # The unit test.
  109. class GTestListTestsUnitTest(gtest_test_utils.TestCase):
  110. """Tests using the --gtest_list_tests flag to list all tests."""
  111. def RunAndVerify(self, flag_value, expected_output_re, other_flag):
  112. """Runs gtest_list_tests_unittest_ and verifies that it prints
  113. the correct tests.
  114. Args:
  115. flag_value: value of the --gtest_list_tests flag;
  116. None if the flag should not be present.
  117. expected_output_re: regular expression that matches the expected
  118. output after running command;
  119. other_flag: a different flag to be passed to command
  120. along with gtest_list_tests;
  121. None if the flag should not be present.
  122. """
  123. if flag_value is None:
  124. flag = ''
  125. flag_expression = 'not set'
  126. elif flag_value == '0':
  127. flag = '--%s=0' % LIST_TESTS_FLAG
  128. flag_expression = '0'
  129. else:
  130. flag = '--%s' % LIST_TESTS_FLAG
  131. flag_expression = '1'
  132. args = [flag]
  133. if other_flag is not None:
  134. args += [other_flag]
  135. output = Run(args)
  136. if expected_output_re:
  137. self.assert_(
  138. expected_output_re.match(output),
  139. ('when %s is %s, the output of "%s" is "%s",\n'
  140. 'which does not match regex "%s"' %
  141. (LIST_TESTS_FLAG, flag_expression, ' '.join(args), output,
  142. expected_output_re.pattern)))
  143. else:
  144. self.assert_(
  145. not EXPECTED_OUTPUT_NO_FILTER_RE.match(output),
  146. ('when %s is %s, the output of "%s" is "%s"'%
  147. (LIST_TESTS_FLAG, flag_expression, ' '.join(args), output)))
  148. def testDefaultBehavior(self):
  149. """Tests the behavior of the default mode."""
  150. self.RunAndVerify(flag_value=None,
  151. expected_output_re=None,
  152. other_flag=None)
  153. def testFlag(self):
  154. """Tests using the --gtest_list_tests flag."""
  155. self.RunAndVerify(flag_value='0',
  156. expected_output_re=None,
  157. other_flag=None)
  158. self.RunAndVerify(flag_value='1',
  159. expected_output_re=EXPECTED_OUTPUT_NO_FILTER_RE,
  160. other_flag=None)
  161. def testOverrideNonFilterFlags(self):
  162. """Tests that --gtest_list_tests overrides the non-filter flags."""
  163. self.RunAndVerify(flag_value='1',
  164. expected_output_re=EXPECTED_OUTPUT_NO_FILTER_RE,
  165. other_flag='--gtest_break_on_failure')
  166. def testWithFilterFlags(self):
  167. """Tests that --gtest_list_tests takes into account the
  168. --gtest_filter flag."""
  169. self.RunAndVerify(flag_value='1',
  170. expected_output_re=EXPECTED_OUTPUT_FILTER_FOO_RE,
  171. other_flag='--gtest_filter=Foo*')
  172. if __name__ == '__main__':
  173. gtest_test_utils.Main()