You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
11 KiB

  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 the gtest_xml_output module"""
  32. __author__ = 'eefacm@gmail.com (Sean Mcafee)'
  33. import errno
  34. import os
  35. import sys
  36. from xml.dom import minidom, Node
  37. import gtest_test_utils
  38. import gtest_xml_test_utils
  39. GTEST_OUTPUT_FLAG = "--gtest_output"
  40. GTEST_DEFAULT_OUTPUT_FILE = "test_detail.xml"
  41. GTEST_PROGRAM_NAME = "gtest_xml_output_unittest_"
  42. SUPPORTS_STACK_TRACES = False
  43. if SUPPORTS_STACK_TRACES:
  44. STACK_TRACE_TEMPLATE = "\nStack trace:\n*"
  45. else:
  46. STACK_TRACE_TEMPLATE = ""
  47. EXPECTED_NON_EMPTY_XML = """<?xml version="1.0" encoding="UTF-8"?>
  48. <testsuites tests="23" failures="4" disabled="2" errors="0" time="*" name="AllTests">
  49. <testsuite name="SuccessfulTest" tests="1" failures="0" disabled="0" errors="0" time="*">
  50. <testcase name="Succeeds" status="run" time="*" classname="SuccessfulTest"/>
  51. </testsuite>
  52. <testsuite name="FailedTest" tests="1" failures="1" disabled="0" errors="0" time="*">
  53. <testcase name="Fails" status="run" time="*" classname="FailedTest">
  54. <failure message="Value of: 2&#x0A;Expected: 1" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
  55. Value of: 2
  56. Expected: 1%(stack)s]]></failure>
  57. </testcase>
  58. </testsuite>
  59. <testsuite name="MixedResultTest" tests="3" failures="1" disabled="1" errors="0" time="*">
  60. <testcase name="Succeeds" status="run" time="*" classname="MixedResultTest"/>
  61. <testcase name="Fails" status="run" time="*" classname="MixedResultTest">
  62. <failure message="Value of: 2&#x0A;Expected: 1" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
  63. Value of: 2
  64. Expected: 1%(stack)s]]></failure>
  65. <failure message="Value of: 3&#x0A;Expected: 2" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
  66. Value of: 3
  67. Expected: 2%(stack)s]]></failure>
  68. </testcase>
  69. <testcase name="DISABLED_test" status="notrun" time="*" classname="MixedResultTest"/>
  70. </testsuite>
  71. <testsuite name="XmlQuotingTest" tests="1" failures="1" disabled="0" errors="0" time="*">
  72. <testcase name="OutputsCData" status="run" time="*" classname="XmlQuotingTest">
  73. <failure message="Failed&#x0A;XML output: &lt;?xml encoding=&quot;utf-8&quot;&gt;&lt;top&gt;&lt;![CDATA[cdata text]]&gt;&lt;/top&gt;" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
  74. Failed
  75. XML output: <?xml encoding="utf-8"><top><![CDATA[cdata text]]>]]&gt;<![CDATA[</top>%(stack)s]]></failure>
  76. </testcase>
  77. </testsuite>
  78. <testsuite name="InvalidCharactersTest" tests="1" failures="1" disabled="0" errors="0" time="*">
  79. <testcase name="InvalidCharactersInMessage" status="run" time="*" classname="InvalidCharactersTest">
  80. <failure message="Failed&#x0A;Invalid characters in brackets []" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
  81. Failed
  82. Invalid characters in brackets []%(stack)s]]></failure>
  83. </testcase>
  84. </testsuite>
  85. <testsuite name="DisabledTest" tests="1" failures="0" disabled="1" errors="0" time="*">
  86. <testcase name="DISABLED_test_not_run" status="notrun" time="*" classname="DisabledTest"/>
  87. </testsuite>
  88. <testsuite name="PropertyRecordingTest" tests="4" failures="0" disabled="0" errors="0" time="*">
  89. <testcase name="OneProperty" status="run" time="*" classname="PropertyRecordingTest" key_1="1"/>
  90. <testcase name="IntValuedProperty" status="run" time="*" classname="PropertyRecordingTest" key_int="1"/>
  91. <testcase name="ThreeProperties" status="run" time="*" classname="PropertyRecordingTest" key_1="1" key_2="2" key_3="3"/>
  92. <testcase name="TwoValuesForOneKeyUsesLastValue" status="run" time="*" classname="PropertyRecordingTest" key_1="2"/>
  93. </testsuite>
  94. <testsuite name="NoFixtureTest" tests="3" failures="0" disabled="0" errors="0" time="*">
  95. <testcase name="RecordProperty" status="run" time="*" classname="NoFixtureTest" key="1"/>
  96. <testcase name="ExternalUtilityThatCallsRecordIntValuedProperty" status="run" time="*" classname="NoFixtureTest" key_for_utility_int="1"/>
  97. <testcase name="ExternalUtilityThatCallsRecordStringValuedProperty" status="run" time="*" classname="NoFixtureTest" key_for_utility_string="1"/>
  98. </testsuite>
  99. <testsuite name="Single/ValueParamTest" tests="4" failures="0" disabled="0" errors="0" time="*">
  100. <testcase name="HasValueParamAttribute/0" value_param="33" status="run" time="*" classname="Single/ValueParamTest" />
  101. <testcase name="HasValueParamAttribute/1" value_param="42" status="run" time="*" classname="Single/ValueParamTest" />
  102. <testcase name="AnotherTestThatHasValueParamAttribute/0" value_param="33" status="run" time="*" classname="Single/ValueParamTest" />
  103. <testcase name="AnotherTestThatHasValueParamAttribute/1" value_param="42" status="run" time="*" classname="Single/ValueParamTest" />
  104. </testsuite>
  105. <testsuite name="TypedTest/0" tests="1" failures="0" disabled="0" errors="0" time="*">
  106. <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="TypedTest/0" />
  107. </testsuite>
  108. <testsuite name="TypedTest/1" tests="1" failures="0" disabled="0" errors="0" time="*">
  109. <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="TypedTest/1" />
  110. </testsuite>
  111. <testsuite name="Single/TypeParameterizedTestCase/0" tests="1" failures="0" disabled="0" errors="0" time="*">
  112. <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="Single/TypeParameterizedTestCase/0" />
  113. </testsuite>
  114. <testsuite name="Single/TypeParameterizedTestCase/1" tests="1" failures="0" disabled="0" errors="0" time="*">
  115. <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="Single/TypeParameterizedTestCase/1" />
  116. </testsuite>
  117. </testsuites>""" % {'stack': STACK_TRACE_TEMPLATE}
  118. EXPECTED_EMPTY_XML = """<?xml version="1.0" encoding="UTF-8"?>
  119. <testsuites tests="0" failures="0" disabled="0" errors="0" time="*" name="AllTests">
  120. </testsuites>"""
  121. class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
  122. """
  123. Unit test for Google Test's XML output functionality.
  124. """
  125. def testNonEmptyXmlOutput(self):
  126. """
  127. Runs a test program that generates a non-empty XML output, and
  128. tests that the XML output is expected.
  129. """
  130. self._TestXmlOutput(GTEST_PROGRAM_NAME, EXPECTED_NON_EMPTY_XML, 1)
  131. def testEmptyXmlOutput(self):
  132. """
  133. Runs a test program that generates an empty XML output, and
  134. tests that the XML output is expected.
  135. """
  136. self._TestXmlOutput("gtest_no_test_unittest",
  137. EXPECTED_EMPTY_XML, 0)
  138. def testDefaultOutputFile(self):
  139. """
  140. Confirms that Google Test produces an XML output file with the expected
  141. default name if no name is explicitly specified.
  142. """
  143. output_file = os.path.join(gtest_test_utils.GetTempDir(),
  144. GTEST_DEFAULT_OUTPUT_FILE)
  145. gtest_prog_path = gtest_test_utils.GetTestExecutablePath(
  146. "gtest_no_test_unittest")
  147. try:
  148. os.remove(output_file)
  149. except OSError, e:
  150. if e.errno != errno.ENOENT:
  151. raise
  152. p = gtest_test_utils.Subprocess(
  153. [gtest_prog_path, "%s=xml" % GTEST_OUTPUT_FLAG],
  154. working_dir=gtest_test_utils.GetTempDir())
  155. self.assert_(p.exited)
  156. self.assertEquals(0, p.exit_code)
  157. self.assert_(os.path.isfile(output_file))
  158. def testSuppressedXmlOutput(self):
  159. """
  160. Tests that no XML file is generated if the default XML listener is
  161. shut down before RUN_ALL_TESTS is invoked.
  162. """
  163. xml_path = os.path.join(gtest_test_utils.GetTempDir(),
  164. GTEST_PROGRAM_NAME + "out.xml")
  165. if os.path.isfile(xml_path):
  166. os.remove(xml_path)
  167. gtest_prog_path = gtest_test_utils.GetTestExecutablePath(GTEST_PROGRAM_NAME)
  168. command = [gtest_prog_path,
  169. "%s=xml:%s" % (GTEST_OUTPUT_FLAG, xml_path),
  170. "--shut_down_xml"]
  171. p = gtest_test_utils.Subprocess(command)
  172. if p.terminated_by_signal:
  173. self.assert_(False,
  174. "%s was killed by signal %d" % (gtest_prog_name, p.signal))
  175. else:
  176. self.assert_(p.exited)
  177. self.assertEquals(1, p.exit_code,
  178. "'%s' exited with code %s, which doesn't match "
  179. "the expected exit code %s."
  180. % (command, p.exit_code, 1))
  181. self.assert_(not os.path.isfile(xml_path))
  182. def _TestXmlOutput(self, gtest_prog_name, expected_xml, expected_exit_code):
  183. """
  184. Asserts that the XML document generated by running the program
  185. gtest_prog_name matches expected_xml, a string containing another
  186. XML document. Furthermore, the program's exit code must be
  187. expected_exit_code.
  188. """
  189. xml_path = os.path.join(gtest_test_utils.GetTempDir(),
  190. gtest_prog_name + "out.xml")
  191. gtest_prog_path = gtest_test_utils.GetTestExecutablePath(gtest_prog_name)
  192. command = [gtest_prog_path, "%s=xml:%s" % (GTEST_OUTPUT_FLAG, xml_path)]
  193. p = gtest_test_utils.Subprocess(command)
  194. if p.terminated_by_signal:
  195. self.assert_(False,
  196. "%s was killed by signal %d" % (gtest_prog_name, p.signal))
  197. else:
  198. self.assert_(p.exited)
  199. self.assertEquals(expected_exit_code, p.exit_code,
  200. "'%s' exited with code %s, which doesn't match "
  201. "the expected exit code %s."
  202. % (command, p.exit_code, expected_exit_code))
  203. expected = minidom.parseString(expected_xml)
  204. actual = minidom.parse(xml_path)
  205. self.NormalizeXml(actual.documentElement)
  206. self.AssertEquivalentNodes(expected.documentElement,
  207. actual.documentElement)
  208. expected.unlink()
  209. actual .unlink()
  210. if __name__ == '__main__':
  211. os.environ['GTEST_STACK_TRACE_DEPTH'] = '1'
  212. gtest_test_utils.Main()