ac66feb66719e62cfa16c79a17f63de990f277c1
[platform/upstream/gtest.git] / googletest / test / gtest_xml_outfiles_test.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2008, 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 import os
35 from xml.dom import minidom, Node
36 import gtest_test_utils
37 import gtest_xml_test_utils
38
39 GTEST_OUTPUT_SUBDIR = "xml_outfiles"
40 GTEST_OUTPUT_1_TEST = "gtest_xml_outfile1_test_"
41 GTEST_OUTPUT_2_TEST = "gtest_xml_outfile2_test_"
42
43 EXPECTED_XML_1 = """<?xml version="1.0" encoding="UTF-8"?>
44 <testsuites tests="1" failures="0" disabled="0" errors="0" time="*" timestamp="*" name="AllTests">
45   <testsuite name="PropertyOne" tests="1" failures="0" skipped="0" disabled="0" errors="0" time="*" timestamp="*">
46     <testcase name="TestSomeProperties" status="run" result="completed" time="*" timestamp="*" classname="PropertyOne">
47       <properties>
48         <property name="SetUpProp" value="1"/>
49         <property name="TestSomeProperty" value="1"/>
50         <property name="TearDownProp" value="1"/>
51       </properties>
52     </testcase>
53   </testsuite>
54 </testsuites>
55 """
56
57 EXPECTED_XML_2 = """<?xml version="1.0" encoding="UTF-8"?>
58 <testsuites tests="1" failures="0" disabled="0" errors="0" time="*" timestamp="*" name="AllTests">
59   <testsuite name="PropertyTwo" tests="1" failures="0" skipped="0" disabled="0" errors="0" time="*" timestamp="*">
60     <testcase name="TestSomeProperties" status="run" result="completed" time="*" timestamp="*" classname="PropertyTwo">
61       <properties>
62         <property name="SetUpProp" value="2"/>
63         <property name="TestSomeProperty" value="2"/>
64         <property name="TearDownProp" value="2"/>
65       </properties>
66     </testcase>
67   </testsuite>
68 </testsuites>
69 """
70
71
72 class GTestXMLOutFilesTest(gtest_xml_test_utils.GTestXMLTestCase):
73   """Unit test for Google Test's XML output functionality."""
74
75   def setUp(self):
76     # We want the trailing '/' that the last "" provides in os.path.join, for
77     # telling Google Test to create an output directory instead of a single file
78     # for xml output.
79     self.output_dir_ = os.path.join(gtest_test_utils.GetTempDir(),
80                                     GTEST_OUTPUT_SUBDIR, "")
81     self.DeleteFilesAndDir()
82
83   def tearDown(self):
84     self.DeleteFilesAndDir()
85
86   def DeleteFilesAndDir(self):
87     try:
88       os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_1_TEST + ".xml"))
89     except os.error:
90       pass
91     try:
92       os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_2_TEST + ".xml"))
93     except os.error:
94       pass
95     try:
96       os.rmdir(self.output_dir_)
97     except os.error:
98       pass
99
100   def testOutfile1(self):
101     self._TestOutFile(GTEST_OUTPUT_1_TEST, EXPECTED_XML_1)
102
103   def testOutfile2(self):
104     self._TestOutFile(GTEST_OUTPUT_2_TEST, EXPECTED_XML_2)
105
106   def _TestOutFile(self, test_name, expected_xml):
107     gtest_prog_path = gtest_test_utils.GetTestExecutablePath(test_name)
108     command = [gtest_prog_path, "--gtest_output=xml:%s" % self.output_dir_]
109     p = gtest_test_utils.Subprocess(command,
110                                     working_dir=gtest_test_utils.GetTempDir())
111     self.assert_(p.exited)
112     self.assertEquals(0, p.exit_code)
113
114     output_file_name1 = test_name + ".xml"
115     output_file1 = os.path.join(self.output_dir_, output_file_name1)
116     output_file_name2 = 'lt-' + output_file_name1
117     output_file2 = os.path.join(self.output_dir_, output_file_name2)
118     self.assert_(os.path.isfile(output_file1) or os.path.isfile(output_file2),
119                  output_file1)
120
121     expected = minidom.parseString(expected_xml)
122     if os.path.isfile(output_file1):
123       actual = minidom.parse(output_file1)
124     else:
125       actual = minidom.parse(output_file2)
126     self.NormalizeXml(actual.documentElement)
127     self.AssertEquivalentNodes(expected.documentElement,
128                                actual.documentElement)
129     expected.unlink()
130     actual.unlink()
131
132
133 if __name__ == "__main__":
134   os.environ["GTEST_STACK_TRACE_DEPTH"] = "0"
135   gtest_test_utils.Main()