googletest-json-outfiles-test.py 5.7 KB

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