googletest-json-outfiles-test.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. import gtest_json_test_utils
  34. 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': 1,
  40. u'failures': 0,
  41. u'disabled': 0,
  42. u'errors': 0,
  43. u'time': u'*',
  44. u'timestamp': u'*',
  45. u'name': u'AllTests',
  46. u'testsuites': [{
  47. u'name': u'PropertyOne',
  48. u'tests': 1,
  49. u'failures': 0,
  50. u'disabled': 0,
  51. u'errors': 0,
  52. u'time': u'*',
  53. u'testsuite': [{
  54. u'name': u'TestSomeProperties',
  55. u'status': u'RUN',
  56. u'time': u'*',
  57. u'classname': u'PropertyOne',
  58. u'SetUpProp': u'1',
  59. u'TestSomeProperty': u'1',
  60. u'TearDownProp': u'1',
  61. }],
  62. }],
  63. }
  64. EXPECTED_2 = {
  65. u'tests': 1,
  66. u'failures': 0,
  67. u'disabled': 0,
  68. u'errors': 0,
  69. u'time': u'*',
  70. u'timestamp': u'*',
  71. u'name': u'AllTests',
  72. u'testsuites': [{
  73. u'name': u'PropertyTwo',
  74. u'tests': 1,
  75. u'failures': 0,
  76. u'disabled': 0,
  77. u'errors': 0,
  78. u'time': u'*',
  79. u'testsuite': [{
  80. u'name': u'TestSomeProperties',
  81. u'status': u'RUN',
  82. u'time': u'*',
  83. u'classname': u'PropertyTwo',
  84. u'SetUpProp': u'2',
  85. u'TestSomeProperty': u'2',
  86. u'TearDownProp': u'2',
  87. }],
  88. }],
  89. }
  90. class GTestJsonOutFilesTest(gtest_test_utils.TestCase):
  91. """Unit test for Google Test's JSON output functionality."""
  92. def setUp(self):
  93. # We want the trailing '/' that the last "" provides in os.path.join, for
  94. # telling Google Test to create an output directory instead of a single file
  95. # for xml output.
  96. self.output_dir_ = os.path.join(gtest_test_utils.GetTempDir(),
  97. GTEST_OUTPUT_SUBDIR, '')
  98. self.DeleteFilesAndDir()
  99. def tearDown(self):
  100. self.DeleteFilesAndDir()
  101. def DeleteFilesAndDir(self):
  102. try:
  103. os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_1_TEST + '.json'))
  104. except os.error:
  105. pass
  106. try:
  107. os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_2_TEST + '.json'))
  108. except os.error:
  109. pass
  110. try:
  111. os.rmdir(self.output_dir_)
  112. except os.error:
  113. pass
  114. def testOutfile1(self):
  115. self._TestOutFile(GTEST_OUTPUT_1_TEST, EXPECTED_1)
  116. def testOutfile2(self):
  117. self._TestOutFile(GTEST_OUTPUT_2_TEST, EXPECTED_2)
  118. def _TestOutFile(self, test_name, expected):
  119. gtest_prog_path = gtest_test_utils.GetTestExecutablePath(test_name)
  120. command = [gtest_prog_path, '--gtest_output=json:%s' % self.output_dir_]
  121. p = gtest_test_utils.Subprocess(command,
  122. working_dir=gtest_test_utils.GetTempDir())
  123. self.assert_(p.exited)
  124. self.assertEquals(0, p.exit_code)
  125. # FIXME: libtool causes the built test binary to be
  126. # named lt-gtest_xml_outfiles_test_ instead of
  127. # gtest_xml_outfiles_test_. To account for this possibility, we
  128. # allow both names in the following code. We should remove this
  129. # when libtool replacement tool is ready.
  130. output_file_name1 = test_name + '.json'
  131. output_file1 = os.path.join(self.output_dir_, output_file_name1)
  132. output_file_name2 = 'lt-' + output_file_name1
  133. output_file2 = os.path.join(self.output_dir_, output_file_name2)
  134. self.assert_(os.path.isfile(output_file1) or os.path.isfile(output_file2),
  135. output_file1)
  136. if os.path.isfile(output_file1):
  137. with open(output_file1) as f:
  138. actual = json.load(f)
  139. else:
  140. with open(output_file2) as f:
  141. actual = json.load(f)
  142. self.assertEqual(expected, gtest_json_test_utils.normalize(actual))
  143. if __name__ == '__main__':
  144. os.environ['GTEST_STACK_TRACE_DEPTH'] = '0'
  145. gtest_test_utils.Main()