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 gtest_test_utils
     38 import os
     39 import sys
     40 import tempfile
     41 import unittest
     42 
     43 from xml.dom import minidom, Node
     44 
     45 import gtest_xml_test_utils
     46 
     47 GTEST_OUTPUT_FLAG         = "--gtest_output"
     48 GTEST_DEFAULT_OUTPUT_FILE = "test_detail.xml"
     49 
     50 EXPECTED_NON_EMPTY_XML = """<?xml version="1.0" encoding="UTF-8"?>
     51 <testsuite tests="13" failures="2" disabled="2" errors="0" time="*" name="AllTests">
     52   <testsuite name="SuccessfulTest" tests="1" failures="0" disabled="0" errors="0" time="*">
     53     <testcase name="Succeeds" status="run" time="*" classname="SuccessfulTest"/>
     54   </testsuite>
     55   <testsuite name="FailedTest" tests="1" failures="1" disabled="0" errors="0" time="*">
     56     <testcase name="Fails" status="run" time="*" classname="FailedTest">
     57       <failure message="Value of: 2&#x0A;Expected: 1" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
     58 Value of: 2
     59 Expected: 1]]></failure>
     60     </testcase>
     61   </testsuite>
     62   <testsuite name="MixedResultTest" tests="3" failures="1" disabled="1" errors="0" time="*">
     63     <testcase name="Succeeds" status="run" time="*" classname="MixedResultTest"/>
     64     <testcase name="Fails" status="run" time="*" classname="MixedResultTest">
     65       <failure message="Value of: 2&#x0A;Expected: 1" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
     66 Value of: 2
     67 Expected: 1]]></failure>
     68       <failure message="Value of: 3&#x0A;Expected: 2" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
     69 Value of: 3
     70 Expected: 2]]></failure>
     71     </testcase>
     72     <testcase name="DISABLED_test" status="notrun" time="*" classname="MixedResultTest"/>
     73   </testsuite>
     74   <testsuite name="DisabledTest" tests="1" failures="0" disabled="1" errors="0" time="*">
     75     <testcase name="DISABLED_test_not_run" status="notrun" time="*" classname="DisabledTest"/>
     76   </testsuite>
     77   <testsuite name="PropertyRecordingTest" tests="4" failures="0" disabled="0" errors="0" time="*">
     78     <testcase name="OneProperty" status="run" time="*" classname="PropertyRecordingTest" key_1="1"/>
     79     <testcase name="IntValuedProperty" status="run" time="*" classname="PropertyRecordingTest" key_int="1"/>
     80     <testcase name="ThreeProperties" status="run" time="*" classname="PropertyRecordingTest" key_1="1" key_2="2" key_3="3"/>
     81     <testcase name="TwoValuesForOneKeyUsesLastValue" status="run" time="*" classname="PropertyRecordingTest" key_1="2"/>
     82   </testsuite>
     83   <testsuite name="NoFixtureTest" tests="3" failures="0" disabled="0" errors="0" time="*">
     84      <testcase name="RecordProperty" status="run" time="*" classname="NoFixtureTest" key="1"/>
     85      <testcase name="ExternalUtilityThatCallsRecordIntValuedProperty" status="run" time="*" classname="NoFixtureTest" key_for_utility_int="1"/>
     86      <testcase name="ExternalUtilityThatCallsRecordStringValuedProperty" status="run" time="*" classname="NoFixtureTest" key_for_utility_string="1"/>
     87   </testsuite>
     88 </testsuite>"""
     89 
     90 
     91 EXPECTED_EMPTY_XML = """<?xml version="1.0" encoding="UTF-8"?>
     92 <testsuite tests="0" failures="0" disabled="0" errors="0" time="*" name="AllTests">
     93 </testsuite>"""
     94 
     95 
     96 class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
     97   """
     98   Unit test for Google Test's XML output functionality.
     99   """
    100 
    101   def testNonEmptyXmlOutput(self):
    102     """
    103     Runs a test program that generates a non-empty XML output, and
    104     tests that the XML output is expected.
    105     """
    106     self._TestXmlOutput("gtest_xml_output_unittest_",
    107                         EXPECTED_NON_EMPTY_XML, 1)
    108 
    109   def testEmptyXmlOutput(self):
    110     """
    111     Runs a test program that generates an empty XML output, and
    112     tests that the XML output is expected.
    113     """
    114 
    115     self._TestXmlOutput("gtest_no_test_unittest",
    116                         EXPECTED_EMPTY_XML, 0)
    117 
    118   def testDefaultOutputFile(self):
    119     """
    120     Confirms that Google Test produces an XML output file with the expected
    121     default name if no name is explicitly specified.
    122     """
    123     temp_dir = tempfile.mkdtemp()
    124     output_file     = os.path.join(temp_dir,
    125                                    GTEST_DEFAULT_OUTPUT_FILE)
    126     gtest_prog_path = os.path.join(gtest_test_utils.GetBuildDir(),
    127                                    "gtest_no_test_unittest")
    128     try:
    129       os.remove(output_file)
    130     except OSError, e:
    131       if e.errno != errno.ENOENT:
    132         raise
    133 
    134     p = gtest_test_utils.Subprocess(
    135         [gtest_prog_path, "%s=xml" % GTEST_OUTPUT_FLAG],
    136         working_dir=temp_dir)
    137     self.assert_(p.exited)
    138     self.assertEquals(0, p.exit_code)
    139     self.assert_(os.path.isfile(output_file))
    140 
    141 
    142   def _TestXmlOutput(self, gtest_prog_name, expected_xml, expected_exit_code):
    143     """
    144     Asserts that the XML document generated by running the program
    145     gtest_prog_name matches expected_xml, a string containing another
    146     XML document.  Furthermore, the program's exit code must be
    147     expected_exit_code.
    148     """
    149 
    150     xml_path = os.path.join(tempfile.mkdtemp(), gtest_prog_name + "out.xml")
    151     gtest_prog_path = os.path.join(gtest_test_utils.GetBuildDir(),
    152                                    gtest_prog_name)
    153 
    154     command = [gtest_prog_path, "%s=xml:%s" % (GTEST_OUTPUT_FLAG, xml_path)]
    155     p = gtest_test_utils.Subprocess(command)
    156     if p.terminated_by_signal:
    157       self.assert_(False,
    158                    "%s was killed by signal %d" % (gtest_prog_name, p.signal))
    159     else:
    160       self.assert_(p.exited)
    161       self.assertEquals(expected_exit_code, p.exit_code,
    162                         "'%s' exited with code %s, which doesn't match "
    163                         "the expected exit code %s."
    164                         % (command, p.exit_code, expected_exit_code))
    165 
    166     expected = minidom.parseString(expected_xml)
    167     actual   = minidom.parse(xml_path)
    168     self.NormalizeXml(actual.documentElement)
    169     self.AssertEquivalentNodes(expected.documentElement,
    170                                actual.documentElement)
    171     expected.unlink()
    172     actual  .unlink()
    173 
    174 
    175 
    176 if __name__ == '__main__':
    177   os.environ['GTEST_STACK_TRACE_DEPTH'] = '1'
    178   gtest_test_utils.Main()
    179