gtest_xml_outfiles_test.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2008, 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 the gtest_xml_output module."""
  32. import os
  33. from xml.dom import minidom, Node
  34. import gtest_test_utils
  35. import gtest_xml_test_utils
  36. GTEST_OUTPUT_SUBDIR = "xml_outfiles"
  37. GTEST_OUTPUT_1_TEST = "gtest_xml_outfile1_test_"
  38. GTEST_OUTPUT_2_TEST = "gtest_xml_outfile2_test_"
  39. EXPECTED_XML_1 = """<?xml version="1.0" encoding="UTF-8"?>
  40. <testsuites tests="1" failures="0" disabled="0" errors="0" time="*" timestamp="*" name="AllTests">
  41. <testsuite name="PropertyOne" tests="1" failures="0" disabled="0" errors="0" time="*">
  42. <testcase name="TestSomeProperties" status="run" time="*" classname="PropertyOne">
  43. <properties>
  44. <property name="SetUpProp" value="1"/>
  45. <property name="TestSomeProperty" value="1"/>
  46. <property name="TearDownProp" value="1"/>
  47. </properties>
  48. </testcase>
  49. </testsuite>
  50. </testsuites>
  51. """
  52. EXPECTED_XML_2 = """<?xml version="1.0" encoding="UTF-8"?>
  53. <testsuites tests="1" failures="0" disabled="0" errors="0" time="*" timestamp="*" name="AllTests">
  54. <testsuite name="PropertyTwo" tests="1" failures="0" disabled="0" errors="0" time="*">
  55. <testcase name="TestSomeProperties" status="run" time="*" classname="PropertyTwo">
  56. <properties>
  57. <property name="SetUpProp" value="2"/>
  58. <property name="TestSomeProperty" value="2"/>
  59. <property name="TearDownProp" value="2"/>
  60. </properties>
  61. </testcase>
  62. </testsuite>
  63. </testsuites>
  64. """
  65. class GTestXMLOutFilesTest(gtest_xml_test_utils.GTestXMLTestCase):
  66. """Unit test for Google Test's XML output functionality."""
  67. def setUp(self):
  68. # We want the trailing '/' that the last "" provides in os.path.join, for
  69. # telling Google Test to create an output directory instead of a single file
  70. # for xml output.
  71. self.output_dir_ = os.path.join(gtest_test_utils.GetTempDir(),
  72. GTEST_OUTPUT_SUBDIR, "")
  73. self.DeleteFilesAndDir()
  74. def tearDown(self):
  75. self.DeleteFilesAndDir()
  76. def DeleteFilesAndDir(self):
  77. try:
  78. os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_1_TEST + ".xml"))
  79. except os.error:
  80. pass
  81. try:
  82. os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_2_TEST + ".xml"))
  83. except os.error:
  84. pass
  85. try:
  86. os.rmdir(self.output_dir_)
  87. except os.error:
  88. pass
  89. def testOutfile1(self):
  90. self._TestOutFile(GTEST_OUTPUT_1_TEST, EXPECTED_XML_1)
  91. def testOutfile2(self):
  92. self._TestOutFile(GTEST_OUTPUT_2_TEST, EXPECTED_XML_2)
  93. def _TestOutFile(self, test_name, expected_xml):
  94. gtest_prog_path = gtest_test_utils.GetTestExecutablePath(test_name)
  95. command = [gtest_prog_path, "--gtest_output=xml:%s" % self.output_dir_]
  96. p = gtest_test_utils.Subprocess(command,
  97. working_dir=gtest_test_utils.GetTempDir())
  98. self.assert_(p.exited)
  99. self.assertEquals(0, p.exit_code)
  100. # FIXME: libtool causes the built test binary to be
  101. # named lt-gtest_xml_outfiles_test_ instead of
  102. # gtest_xml_outfiles_test_. To account for this possibility, we
  103. # allow both names in the following code. We should remove this
  104. # when libtool replacement tool is ready.
  105. output_file_name1 = test_name + ".xml"
  106. output_file1 = os.path.join(self.output_dir_, output_file_name1)
  107. output_file_name2 = 'lt-' + output_file_name1
  108. output_file2 = os.path.join(self.output_dir_, output_file_name2)
  109. self.assert_(os.path.isfile(output_file1) or os.path.isfile(output_file2),
  110. output_file1)
  111. expected = minidom.parseString(expected_xml)
  112. if os.path.isfile(output_file1):
  113. actual = minidom.parse(output_file1)
  114. else:
  115. actual = minidom.parse(output_file2)
  116. self.NormalizeXml(actual.documentElement)
  117. self.AssertEquivalentNodes(expected.documentElement,
  118. actual.documentElement)
  119. expected.unlink()
  120. actual.unlink()
  121. if __name__ == "__main__":
  122. os.environ["GTEST_STACK_TRACE_DEPTH"] = "0"
  123. gtest_test_utils.Main()