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 __author__ = "keith.ray (at] gmail.com (Keith Ray)" 35 36 import gtest_test_utils 37 import os 38 import sys 39 import tempfile 40 import unittest 41 42 from xml.dom import minidom, Node 43 44 import gtest_xml_test_utils 45 46 47 GTEST_OUTPUT_1_TEST = "gtest_xml_outfile1_test_" 48 GTEST_OUTPUT_2_TEST = "gtest_xml_outfile2_test_" 49 50 EXPECTED_XML_1 = """<?xml version="1.0" encoding="UTF-8"?> 51 <testsuite tests="1" failures="0" disabled="0" errors="0" time="*" name="AllTests"> 52 <testsuite name="PropertyOne" tests="1" failures="0" disabled="0" errors="0" time="*"> 53 <testcase name="TestSomeProperties" status="run" time="*" classname="PropertyOne" SetUpProp="1" TestSomeProperty="1" TearDownProp="1" /> 54 </testsuite> 55 </testsuite> 56 """ 57 58 EXPECTED_XML_2 = """<?xml version="1.0" encoding="UTF-8"?> 59 <testsuite tests="1" failures="0" disabled="0" errors="0" time="*" name="AllTests"> 60 <testsuite name="PropertyTwo" tests="1" failures="0" disabled="0" errors="0" time="*"> 61 <testcase name="TestSomeProperties" status="run" time="*" classname="PropertyTwo" SetUpProp="2" TestSomeProperty="2" TearDownProp="2" /> 62 </testsuite> 63 </testsuite> 64 """ 65 66 67 class GTestXMLOutFilesTest(gtest_xml_test_utils.GTestXMLTestCase): 68 """Unit test for Google Test's XML output functionality.""" 69 70 def setUp(self): 71 # We want the trailing '/' that the last "" provides in os.path.join, for 72 # telling Google Test to create an output directory instead of a single file 73 # for xml output. 74 self.output_dir_ = os.path.join(tempfile.mkdtemp(), "") 75 self.DeleteFilesAndDir() 76 77 def tearDown(self): 78 self.DeleteFilesAndDir() 79 80 def DeleteFilesAndDir(self): 81 try: 82 os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_1_TEST + ".xml")) 83 except os.error: 84 pass 85 try: 86 os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_2_TEST + ".xml")) 87 except os.error: 88 pass 89 try: 90 os.removedirs(self.output_dir_) 91 except os.error: 92 pass 93 94 def testOutfile1(self): 95 self._TestOutFile(GTEST_OUTPUT_1_TEST, EXPECTED_XML_1) 96 97 def testOutfile2(self): 98 self._TestOutFile(GTEST_OUTPUT_2_TEST, EXPECTED_XML_2) 99 100 def _TestOutFile(self, test_name, expected_xml): 101 gtest_prog_path = os.path.join(gtest_test_utils.GetBuildDir(), 102 test_name) 103 command = [gtest_prog_path, "--gtest_output=xml:%s" % self.output_dir_] 104 p = gtest_test_utils.Subprocess(command, working_dir=tempfile.mkdtemp()) 105 self.assert_(p.exited) 106 self.assertEquals(0, p.exit_code) 107 108 # TODO(wan (at] google.com): libtool causes the built test binary to be 109 # named lt-gtest_xml_outfiles_test_ instead of 110 # gtest_xml_outfiles_test_. To account for this possibillity, we 111 # allow both names in the following code. We should remove this 112 # hack when Chandler Carruth's libtool replacement tool is ready. 113 output_file_name1 = test_name + ".xml" 114 output_file1 = os.path.join(self.output_dir_, output_file_name1) 115 output_file_name2 = 'lt-' + output_file_name1 116 output_file2 = os.path.join(self.output_dir_, output_file_name2) 117 self.assert_(os.path.isfile(output_file1) or os.path.isfile(output_file2), 118 output_file1) 119 120 expected = minidom.parseString(expected_xml) 121 if os.path.isfile(output_file1): 122 actual = minidom.parse(output_file1) 123 else: 124 actual = minidom.parse(output_file2) 125 self.NormalizeXml(actual.documentElement) 126 self.AssertEquivalentNodes(expected.documentElement, 127 actual.documentElement) 128 expected.unlink() 129 actual.unlink() 130 131 132 if __name__ == "__main__": 133 os.environ["GTEST_STACK_TRACE_DEPTH"] = "0" 134 gtest_test_utils.Main() 135