media/webrtc/trunk/testing/gtest/test/gtest_xml_output_unittest.py

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rwxr-xr-x

Correct small whitespace inconsistency, lost while renaming variables.

michael@0 1 #!/usr/bin/env python
michael@0 2 #
michael@0 3 # Copyright 2006, Google Inc.
michael@0 4 # All rights reserved.
michael@0 5 #
michael@0 6 # Redistribution and use in source and binary forms, with or without
michael@0 7 # modification, are permitted provided that the following conditions are
michael@0 8 # met:
michael@0 9 #
michael@0 10 # * Redistributions of source code must retain the above copyright
michael@0 11 # notice, this list of conditions and the following disclaimer.
michael@0 12 # * Redistributions in binary form must reproduce the above
michael@0 13 # copyright notice, this list of conditions and the following disclaimer
michael@0 14 # in the documentation and/or other materials provided with the
michael@0 15 # distribution.
michael@0 16 # * Neither the name of Google Inc. nor the names of its
michael@0 17 # contributors may be used to endorse or promote products derived from
michael@0 18 # this software without specific prior written permission.
michael@0 19 #
michael@0 20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 24 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 31
michael@0 32 """Unit test for the gtest_xml_output module"""
michael@0 33
michael@0 34 __author__ = 'eefacm@gmail.com (Sean Mcafee)'
michael@0 35
michael@0 36 import datetime
michael@0 37 import errno
michael@0 38 import os
michael@0 39 import re
michael@0 40 import sys
michael@0 41 from xml.dom import minidom, Node
michael@0 42
michael@0 43 import gtest_test_utils
michael@0 44 import gtest_xml_test_utils
michael@0 45
michael@0 46
michael@0 47 GTEST_LIST_TESTS_FLAG = '--gtest_list_tests'
michael@0 48 GTEST_OUTPUT_FLAG = "--gtest_output"
michael@0 49 GTEST_DEFAULT_OUTPUT_FILE = "test_detail.xml"
michael@0 50 GTEST_PROGRAM_NAME = "gtest_xml_output_unittest_"
michael@0 51
michael@0 52 SUPPORTS_STACK_TRACES = False
michael@0 53
michael@0 54 if SUPPORTS_STACK_TRACES:
michael@0 55 STACK_TRACE_TEMPLATE = '\nStack trace:\n*'
michael@0 56 else:
michael@0 57 STACK_TRACE_TEMPLATE = ''
michael@0 58
michael@0 59 EXPECTED_NON_EMPTY_XML = """<?xml version="1.0" encoding="UTF-8"?>
michael@0 60 <testsuites tests="23" failures="4" disabled="2" errors="0" time="*" timestamp="*" name="AllTests">
michael@0 61 <testsuite name="SuccessfulTest" tests="1" failures="0" disabled="0" errors="0" time="*">
michael@0 62 <testcase name="Succeeds" status="run" time="*" classname="SuccessfulTest"/>
michael@0 63 </testsuite>
michael@0 64 <testsuite name="FailedTest" tests="1" failures="1" disabled="0" errors="0" time="*">
michael@0 65 <testcase name="Fails" status="run" time="*" classname="FailedTest">
michael@0 66 <failure message="gtest_xml_output_unittest_.cc:*&#x0A;Value of: 2&#x0A;Expected: 1" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
michael@0 67 Value of: 2
michael@0 68 Expected: 1%(stack)s]]></failure>
michael@0 69 </testcase>
michael@0 70 </testsuite>
michael@0 71 <testsuite name="MixedResultTest" tests="3" failures="1" disabled="1" errors="0" time="*">
michael@0 72 <testcase name="Succeeds" status="run" time="*" classname="MixedResultTest"/>
michael@0 73 <testcase name="Fails" status="run" time="*" classname="MixedResultTest">
michael@0 74 <failure message="gtest_xml_output_unittest_.cc:*&#x0A;Value of: 2&#x0A;Expected: 1" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
michael@0 75 Value of: 2
michael@0 76 Expected: 1%(stack)s]]></failure>
michael@0 77 <failure message="gtest_xml_output_unittest_.cc:*&#x0A;Value of: 3&#x0A;Expected: 2" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
michael@0 78 Value of: 3
michael@0 79 Expected: 2%(stack)s]]></failure>
michael@0 80 </testcase>
michael@0 81 <testcase name="DISABLED_test" status="notrun" time="*" classname="MixedResultTest"/>
michael@0 82 </testsuite>
michael@0 83 <testsuite name="XmlQuotingTest" tests="1" failures="1" disabled="0" errors="0" time="*">
michael@0 84 <testcase name="OutputsCData" status="run" time="*" classname="XmlQuotingTest">
michael@0 85 <failure message="gtest_xml_output_unittest_.cc:*&#x0A;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:*
michael@0 86 Failed
michael@0 87 XML output: <?xml encoding="utf-8"><top><![CDATA[cdata text]]>]]&gt;<![CDATA[</top>%(stack)s]]></failure>
michael@0 88 </testcase>
michael@0 89 </testsuite>
michael@0 90 <testsuite name="InvalidCharactersTest" tests="1" failures="1" disabled="0" errors="0" time="*">
michael@0 91 <testcase name="InvalidCharactersInMessage" status="run" time="*" classname="InvalidCharactersTest">
michael@0 92 <failure message="gtest_xml_output_unittest_.cc:*&#x0A;Failed&#x0A;Invalid characters in brackets []" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
michael@0 93 Failed
michael@0 94 Invalid characters in brackets []%(stack)s]]></failure>
michael@0 95 </testcase>
michael@0 96 </testsuite>
michael@0 97 <testsuite name="DisabledTest" tests="1" failures="0" disabled="1" errors="0" time="*">
michael@0 98 <testcase name="DISABLED_test_not_run" status="notrun" time="*" classname="DisabledTest"/>
michael@0 99 </testsuite>
michael@0 100 <testsuite name="PropertyRecordingTest" tests="4" failures="0" disabled="0" errors="0" time="*">
michael@0 101 <testcase name="OneProperty" status="run" time="*" classname="PropertyRecordingTest" key_1="1"/>
michael@0 102 <testcase name="IntValuedProperty" status="run" time="*" classname="PropertyRecordingTest" key_int="1"/>
michael@0 103 <testcase name="ThreeProperties" status="run" time="*" classname="PropertyRecordingTest" key_1="1" key_2="2" key_3="3"/>
michael@0 104 <testcase name="TwoValuesForOneKeyUsesLastValue" status="run" time="*" classname="PropertyRecordingTest" key_1="2"/>
michael@0 105 </testsuite>
michael@0 106 <testsuite name="NoFixtureTest" tests="3" failures="0" disabled="0" errors="0" time="*">
michael@0 107 <testcase name="RecordProperty" status="run" time="*" classname="NoFixtureTest" key="1"/>
michael@0 108 <testcase name="ExternalUtilityThatCallsRecordIntValuedProperty" status="run" time="*" classname="NoFixtureTest" key_for_utility_int="1"/>
michael@0 109 <testcase name="ExternalUtilityThatCallsRecordStringValuedProperty" status="run" time="*" classname="NoFixtureTest" key_for_utility_string="1"/>
michael@0 110 </testsuite>
michael@0 111 <testsuite name="Single/ValueParamTest" tests="4" failures="0" disabled="0" errors="0" time="*">
michael@0 112 <testcase name="HasValueParamAttribute/0" value_param="33" status="run" time="*" classname="Single/ValueParamTest" />
michael@0 113 <testcase name="HasValueParamAttribute/1" value_param="42" status="run" time="*" classname="Single/ValueParamTest" />
michael@0 114 <testcase name="AnotherTestThatHasValueParamAttribute/0" value_param="33" status="run" time="*" classname="Single/ValueParamTest" />
michael@0 115 <testcase name="AnotherTestThatHasValueParamAttribute/1" value_param="42" status="run" time="*" classname="Single/ValueParamTest" />
michael@0 116 </testsuite>
michael@0 117 <testsuite name="TypedTest/0" tests="1" failures="0" disabled="0" errors="0" time="*">
michael@0 118 <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="TypedTest/0" />
michael@0 119 </testsuite>
michael@0 120 <testsuite name="TypedTest/1" tests="1" failures="0" disabled="0" errors="0" time="*">
michael@0 121 <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="TypedTest/1" />
michael@0 122 </testsuite>
michael@0 123 <testsuite name="Single/TypeParameterizedTestCase/0" tests="1" failures="0" disabled="0" errors="0" time="*">
michael@0 124 <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="Single/TypeParameterizedTestCase/0" />
michael@0 125 </testsuite>
michael@0 126 <testsuite name="Single/TypeParameterizedTestCase/1" tests="1" failures="0" disabled="0" errors="0" time="*">
michael@0 127 <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="Single/TypeParameterizedTestCase/1" />
michael@0 128 </testsuite>
michael@0 129 </testsuites>""" % {'stack': STACK_TRACE_TEMPLATE}
michael@0 130
michael@0 131
michael@0 132 EXPECTED_EMPTY_XML = """<?xml version="1.0" encoding="UTF-8"?>
michael@0 133 <testsuites tests="0" failures="0" disabled="0" errors="0" time="*" timestamp="*" name="AllTests">
michael@0 134 </testsuites>"""
michael@0 135
michael@0 136 GTEST_PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath(GTEST_PROGRAM_NAME)
michael@0 137
michael@0 138 SUPPORTS_TYPED_TESTS = 'TypedTest' in gtest_test_utils.Subprocess(
michael@0 139 [GTEST_PROGRAM_PATH, GTEST_LIST_TESTS_FLAG], capture_stderr=False).output
michael@0 140
michael@0 141
michael@0 142 class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
michael@0 143 """
michael@0 144 Unit test for Google Test's XML output functionality.
michael@0 145 """
michael@0 146
michael@0 147 # This test currently breaks on platforms that do not support typed and
michael@0 148 # type-parameterized tests, so we don't run it under them.
michael@0 149 if SUPPORTS_TYPED_TESTS:
michael@0 150 def testNonEmptyXmlOutput(self):
michael@0 151 """
michael@0 152 Runs a test program that generates a non-empty XML output, and
michael@0 153 tests that the XML output is expected.
michael@0 154 """
michael@0 155 self._TestXmlOutput(GTEST_PROGRAM_NAME, EXPECTED_NON_EMPTY_XML, 1)
michael@0 156
michael@0 157 def testEmptyXmlOutput(self):
michael@0 158 """Verifies XML output for a Google Test binary without actual tests.
michael@0 159
michael@0 160 Runs a test program that generates an empty XML output, and
michael@0 161 tests that the XML output is expected.
michael@0 162 """
michael@0 163
michael@0 164 self._TestXmlOutput('gtest_no_test_unittest', EXPECTED_EMPTY_XML, 0)
michael@0 165
michael@0 166 def testTimestampValue(self):
michael@0 167 """Checks whether the timestamp attribute in the XML output is valid.
michael@0 168
michael@0 169 Runs a test program that generates an empty XML output, and checks if
michael@0 170 the timestamp attribute in the testsuites tag is valid.
michael@0 171 """
michael@0 172 actual = self._GetXmlOutput('gtest_no_test_unittest', 0)
michael@0 173 date_time_str = actual.documentElement.getAttributeNode('timestamp').value
michael@0 174 # datetime.strptime() is only available in Python 2.5+ so we have to
michael@0 175 # parse the expected datetime manually.
michael@0 176 match = re.match(r'(\d+)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)', date_time_str)
michael@0 177 self.assertTrue(
michael@0 178 re.match,
michael@0 179 'XML datettime string %s has incorrect format' % date_time_str)
michael@0 180 date_time_from_xml = datetime.datetime(
michael@0 181 year=int(match.group(1)), month=int(match.group(2)),
michael@0 182 day=int(match.group(3)), hour=int(match.group(4)),
michael@0 183 minute=int(match.group(5)), second=int(match.group(6)))
michael@0 184
michael@0 185 time_delta = abs(datetime.datetime.now() - date_time_from_xml)
michael@0 186 # timestamp value should be near the current local time
michael@0 187 self.assertTrue(time_delta < datetime.timedelta(seconds=600),
michael@0 188 'time_delta is %s' % time_delta)
michael@0 189 actual.unlink()
michael@0 190
michael@0 191 def testDefaultOutputFile(self):
michael@0 192 """
michael@0 193 Confirms that Google Test produces an XML output file with the expected
michael@0 194 default name if no name is explicitly specified.
michael@0 195 """
michael@0 196 output_file = os.path.join(gtest_test_utils.GetTempDir(),
michael@0 197 GTEST_DEFAULT_OUTPUT_FILE)
michael@0 198 gtest_prog_path = gtest_test_utils.GetTestExecutablePath(
michael@0 199 'gtest_no_test_unittest')
michael@0 200 try:
michael@0 201 os.remove(output_file)
michael@0 202 except OSError, e:
michael@0 203 if e.errno != errno.ENOENT:
michael@0 204 raise
michael@0 205
michael@0 206 p = gtest_test_utils.Subprocess(
michael@0 207 [gtest_prog_path, '%s=xml' % GTEST_OUTPUT_FLAG],
michael@0 208 working_dir=gtest_test_utils.GetTempDir())
michael@0 209 self.assert_(p.exited)
michael@0 210 self.assertEquals(0, p.exit_code)
michael@0 211 self.assert_(os.path.isfile(output_file))
michael@0 212
michael@0 213 def testSuppressedXmlOutput(self):
michael@0 214 """
michael@0 215 Tests that no XML file is generated if the default XML listener is
michael@0 216 shut down before RUN_ALL_TESTS is invoked.
michael@0 217 """
michael@0 218
michael@0 219 xml_path = os.path.join(gtest_test_utils.GetTempDir(),
michael@0 220 GTEST_PROGRAM_NAME + 'out.xml')
michael@0 221 if os.path.isfile(xml_path):
michael@0 222 os.remove(xml_path)
michael@0 223
michael@0 224 command = [GTEST_PROGRAM_PATH,
michael@0 225 '%s=xml:%s' % (GTEST_OUTPUT_FLAG, xml_path),
michael@0 226 '--shut_down_xml']
michael@0 227 p = gtest_test_utils.Subprocess(command)
michael@0 228 if p.terminated_by_signal:
michael@0 229 # p.signal is avalable only if p.terminated_by_signal is True.
michael@0 230 self.assertFalse(
michael@0 231 p.terminated_by_signal,
michael@0 232 '%s was killed by signal %d' % (GTEST_PROGRAM_NAME, p.signal))
michael@0 233 else:
michael@0 234 self.assert_(p.exited)
michael@0 235 self.assertEquals(1, p.exit_code,
michael@0 236 "'%s' exited with code %s, which doesn't match "
michael@0 237 'the expected exit code %s.'
michael@0 238 % (command, p.exit_code, 1))
michael@0 239
michael@0 240 self.assert_(not os.path.isfile(xml_path))
michael@0 241
michael@0 242 def _GetXmlOutput(self, gtest_prog_name, expected_exit_code):
michael@0 243 """
michael@0 244 Returns the xml output generated by running the program gtest_prog_name.
michael@0 245 Furthermore, the program's exit code must be expected_exit_code.
michael@0 246 """
michael@0 247 xml_path = os.path.join(gtest_test_utils.GetTempDir(),
michael@0 248 gtest_prog_name + 'out.xml')
michael@0 249 gtest_prog_path = gtest_test_utils.GetTestExecutablePath(gtest_prog_name)
michael@0 250
michael@0 251 command = [gtest_prog_path, '%s=xml:%s' % (GTEST_OUTPUT_FLAG, xml_path)]
michael@0 252 p = gtest_test_utils.Subprocess(command)
michael@0 253 if p.terminated_by_signal:
michael@0 254 self.assert_(False,
michael@0 255 '%s was killed by signal %d' % (gtest_prog_name, p.signal))
michael@0 256 else:
michael@0 257 self.assert_(p.exited)
michael@0 258 self.assertEquals(expected_exit_code, p.exit_code,
michael@0 259 "'%s' exited with code %s, which doesn't match "
michael@0 260 'the expected exit code %s.'
michael@0 261 % (command, p.exit_code, expected_exit_code))
michael@0 262 actual = minidom.parse(xml_path)
michael@0 263 return actual
michael@0 264
michael@0 265 def _TestXmlOutput(self, gtest_prog_name, expected_xml, expected_exit_code):
michael@0 266 """
michael@0 267 Asserts that the XML document generated by running the program
michael@0 268 gtest_prog_name matches expected_xml, a string containing another
michael@0 269 XML document. Furthermore, the program's exit code must be
michael@0 270 expected_exit_code.
michael@0 271 """
michael@0 272
michael@0 273 actual = self._GetXmlOutput(gtest_prog_name, expected_exit_code)
michael@0 274 expected = minidom.parseString(expected_xml)
michael@0 275 self.NormalizeXml(actual.documentElement)
michael@0 276 self.AssertEquivalentNodes(expected.documentElement,
michael@0 277 actual.documentElement)
michael@0 278 expected.unlink()
michael@0 279 actual.unlink()
michael@0 280
michael@0 281
michael@0 282 if __name__ == '__main__':
michael@0 283 os.environ['GTEST_STACK_TRACE_DEPTH'] = '1'
michael@0 284 gtest_test_utils.Main()

mercurial