3
# Copyright 2006, Google Inc.
6
# Redistribution and use in source and binary forms, with or without
7
# modification, are permitted provided that the following conditions are
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
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.
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.
32
"""Unit test for the gtest_xml_output module"""
34
__author__ = 'eefacm@gmail.com (Sean Mcafee)'
37
import gtest_test_utils
43
from xml.dom import minidom, Node
45
import gtest_xml_test_utils
47
GTEST_OUTPUT_FLAG = "--gtest_output"
48
GTEST_DEFAULT_OUTPUT_FILE = "test_detail.xml"
50
EXPECTED_NON_EMPTY_XML = """<?xml version="1.0" encoding="UTF-8"?>
51
<testsuites 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"/>
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
Expected: 1" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
59
Expected: 1]]></failure>
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
Expected: 1" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
67
Expected: 1]]></failure>
68
<failure message="Value of: 3
Expected: 2" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
70
Expected: 2]]></failure>
72
<testcase name="DISABLED_test" status="notrun" time="*" classname="MixedResultTest"/>
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"/>
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"/>
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"/>
91
EXPECTED_EMPTY_XML = """<?xml version="1.0" encoding="UTF-8"?>
92
<testsuites tests="0" failures="0" disabled="0" errors="0" time="*" name="AllTests">
96
class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
98
Unit test for Google Test's XML output functionality.
101
def testNonEmptyXmlOutput(self):
103
Runs a test program that generates a non-empty XML output, and
104
tests that the XML output is expected.
106
self._TestXmlOutput("gtest_xml_output_unittest_",
107
EXPECTED_NON_EMPTY_XML, 1)
109
def testEmptyXmlOutput(self):
111
Runs a test program that generates an empty XML output, and
112
tests that the XML output is expected.
115
self._TestXmlOutput("gtest_no_test_unittest",
116
EXPECTED_EMPTY_XML, 0)
118
def testDefaultOutputFile(self):
120
Confirms that Google Test produces an XML output file with the expected
121
default name if no name is explicitly specified.
123
temp_dir = tempfile.mkdtemp()
124
output_file = os.path.join(temp_dir, GTEST_DEFAULT_OUTPUT_FILE)
125
gtest_prog_path = gtest_test_utils.GetTestExecutablePath(
126
"gtest_no_test_unittest")
128
os.remove(output_file)
130
if e.errno != errno.ENOENT:
133
p = gtest_test_utils.Subprocess(
134
[gtest_prog_path, "%s=xml" % GTEST_OUTPUT_FLAG],
135
working_dir=temp_dir)
136
self.assert_(p.exited)
137
self.assertEquals(0, p.exit_code)
138
self.assert_(os.path.isfile(output_file))
141
def _TestXmlOutput(self, gtest_prog_name, expected_xml, expected_exit_code):
143
Asserts that the XML document generated by running the program
144
gtest_prog_name matches expected_xml, a string containing another
145
XML document. Furthermore, the program's exit code must be
149
xml_path = os.path.join(tempfile.mkdtemp(), gtest_prog_name + "out.xml")
150
gtest_prog_path = gtest_test_utils.GetTestExecutablePath(gtest_prog_name)
152
command = [gtest_prog_path, "%s=xml:%s" % (GTEST_OUTPUT_FLAG, xml_path)]
153
p = gtest_test_utils.Subprocess(command)
154
if p.terminated_by_signal:
156
"%s was killed by signal %d" % (gtest_prog_name, p.signal))
158
self.assert_(p.exited)
159
self.assertEquals(expected_exit_code, p.exit_code,
160
"'%s' exited with code %s, which doesn't match "
161
"the expected exit code %s."
162
% (command, p.exit_code, expected_exit_code))
164
expected = minidom.parseString(expected_xml)
165
actual = minidom.parse(xml_path)
166
self.NormalizeXml(actual.documentElement)
167
self.AssertEquivalentNodes(expected.documentElement,
168
actual.documentElement)
174
if __name__ == '__main__':
175
os.environ['GTEST_STACK_TRACE_DEPTH'] = '1'
176
gtest_test_utils.Main()