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