googletest-json-outfiles-test.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #!/usr/bin/env python
  2. # Copyright 2018, Google Inc.
  3. # 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. """Unit test for the gtest_json_output module."""
  31. import json
  32. import os
  33. from googletest.test import gtest_json_test_utils
  34. from googletest.test import gtest_test_utils
  35. GTEST_OUTPUT_SUBDIR = 'json_outfiles'
  36. GTEST_OUTPUT_1_TEST = 'gtest_xml_outfile1_test_'
  37. GTEST_OUTPUT_2_TEST = 'gtest_xml_outfile2_test_'
  38. EXPECTED_1 = {
  39. u'tests':
  40. 1,
  41. u'failures':
  42. 0,
  43. u'disabled':
  44. 0,
  45. u'errors':
  46. 0,
  47. u'time':
  48. u'*',
  49. u'timestamp':
  50. u'*',
  51. u'name':
  52. u'AllTests',
  53. u'testsuites': [{
  54. u'name':
  55. u'PropertyOne',
  56. u'tests':
  57. 1,
  58. u'failures':
  59. 0,
  60. u'disabled':
  61. 0,
  62. u'errors':
  63. 0,
  64. u'time':
  65. u'*',
  66. u'timestamp':
  67. u'*',
  68. u'testsuite': [{
  69. u'name': u'TestSomeProperties',
  70. u'file': u'gtest_xml_outfile1_test_.cc',
  71. u'line': 41,
  72. u'status': u'RUN',
  73. u'result': u'COMPLETED',
  74. u'time': u'*',
  75. u'timestamp': u'*',
  76. u'classname': u'PropertyOne',
  77. u'SetUpProp': u'1',
  78. u'TestSomeProperty': u'1',
  79. u'TearDownProp': u'1',
  80. }],
  81. }],
  82. }
  83. EXPECTED_2 = {
  84. u'tests':
  85. 1,
  86. u'failures':
  87. 0,
  88. u'disabled':
  89. 0,
  90. u'errors':
  91. 0,
  92. u'time':
  93. u'*',
  94. u'timestamp':
  95. u'*',
  96. u'name':
  97. u'AllTests',
  98. u'testsuites': [{
  99. u'name':
  100. u'PropertyTwo',
  101. u'tests':
  102. 1,
  103. u'failures':
  104. 0,
  105. u'disabled':
  106. 0,
  107. u'errors':
  108. 0,
  109. u'time':
  110. u'*',
  111. u'timestamp':
  112. u'*',
  113. u'testsuite': [{
  114. u'name': u'TestSomeProperties',
  115. u'file': u'gtest_xml_outfile2_test_.cc',
  116. u'line': 41,
  117. u'status': u'RUN',
  118. u'result': u'COMPLETED',
  119. u'timestamp': u'*',
  120. u'time': u'*',
  121. u'classname': u'PropertyTwo',
  122. u'SetUpProp': u'2',
  123. u'TestSomeProperty': u'2',
  124. u'TearDownProp': u'2',
  125. }],
  126. }],
  127. }
  128. class GTestJsonOutFilesTest(gtest_test_utils.TestCase):
  129. """Unit test for Google Test's JSON output functionality."""
  130. def setUp(self):
  131. # We want the trailing '/' that the last "" provides in os.path.join, for
  132. # telling Google Test to create an output directory instead of a single file
  133. # for xml output.
  134. self.output_dir_ = os.path.join(gtest_test_utils.GetTempDir(),
  135. GTEST_OUTPUT_SUBDIR, '')
  136. self.DeleteFilesAndDir()
  137. def tearDown(self):
  138. self.DeleteFilesAndDir()
  139. def DeleteFilesAndDir(self):
  140. try:
  141. os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_1_TEST + '.json'))
  142. except os.error:
  143. pass
  144. try:
  145. os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_2_TEST + '.json'))
  146. except os.error:
  147. pass
  148. try:
  149. os.rmdir(self.output_dir_)
  150. except os.error:
  151. pass
  152. def testOutfile1(self):
  153. self._TestOutFile(GTEST_OUTPUT_1_TEST, EXPECTED_1)
  154. def testOutfile2(self):
  155. self._TestOutFile(GTEST_OUTPUT_2_TEST, EXPECTED_2)
  156. def _TestOutFile(self, test_name, expected):
  157. gtest_prog_path = gtest_test_utils.GetTestExecutablePath(test_name)
  158. command = [gtest_prog_path, '--gtest_output=json:%s' % self.output_dir_]
  159. p = gtest_test_utils.Subprocess(command,
  160. working_dir=gtest_test_utils.GetTempDir())
  161. self.assert_(p.exited)
  162. self.assertEquals(0, p.exit_code)
  163. output_file_name1 = test_name + '.json'
  164. output_file1 = os.path.join(self.output_dir_, output_file_name1)
  165. output_file_name2 = 'lt-' + output_file_name1
  166. output_file2 = os.path.join(self.output_dir_, output_file_name2)
  167. self.assert_(os.path.isfile(output_file1) or os.path.isfile(output_file2),
  168. output_file1)
  169. if os.path.isfile(output_file1):
  170. with open(output_file1) as f:
  171. actual = json.load(f)
  172. else:
  173. with open(output_file2) as f:
  174. actual = json.load(f)
  175. self.assertEqual(expected, gtest_json_test_utils.normalize(actual))
  176. if __name__ == '__main__':
  177. os.environ['GTEST_STACK_TRACE_DEPTH'] = '0'
  178. gtest_test_utils.Main()