michael@0: #!/usr/bin/env python
michael@0: #
michael@0: # Copyright 2008, Google Inc.
michael@0: # All rights reserved.
michael@0: #
michael@0: # Redistribution and use in source and binary forms, with or without
michael@0: # modification, are permitted provided that the following conditions are
michael@0: # met:
michael@0: #
michael@0: # * Redistributions of source code must retain the above copyright
michael@0: # notice, this list of conditions and the following disclaimer.
michael@0: # * Redistributions in binary form must reproduce the above
michael@0: # copyright notice, this list of conditions and the following disclaimer
michael@0: # in the documentation and/or other materials provided with the
michael@0: # distribution.
michael@0: # * Neither the name of Google Inc. nor the names of its
michael@0: # contributors may be used to endorse or promote products derived from
michael@0: # this software without specific prior written permission.
michael@0: #
michael@0: # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0: # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0: # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0: # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0: # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0: # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0: # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0: # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0: # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0: # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0: # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0:
michael@0: """Unit test for the gtest_xml_output module."""
michael@0:
michael@0: __author__ = "keith.ray@gmail.com (Keith Ray)"
michael@0:
michael@0: import os
michael@0: from xml.dom import minidom, Node
michael@0:
michael@0: import gtest_test_utils
michael@0: import gtest_xml_test_utils
michael@0:
michael@0:
michael@0: GTEST_OUTPUT_SUBDIR = "xml_outfiles"
michael@0: GTEST_OUTPUT_1_TEST = "gtest_xml_outfile1_test_"
michael@0: GTEST_OUTPUT_2_TEST = "gtest_xml_outfile2_test_"
michael@0:
michael@0: EXPECTED_XML_1 = """
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0: """
michael@0:
michael@0: EXPECTED_XML_2 = """
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0: """
michael@0:
michael@0:
michael@0: class GTestXMLOutFilesTest(gtest_xml_test_utils.GTestXMLTestCase):
michael@0: """Unit test for Google Test's XML output functionality."""
michael@0:
michael@0: def setUp(self):
michael@0: # We want the trailing '/' that the last "" provides in os.path.join, for
michael@0: # telling Google Test to create an output directory instead of a single file
michael@0: # for xml output.
michael@0: self.output_dir_ = os.path.join(gtest_test_utils.GetTempDir(),
michael@0: GTEST_OUTPUT_SUBDIR, "")
michael@0: self.DeleteFilesAndDir()
michael@0:
michael@0: def tearDown(self):
michael@0: self.DeleteFilesAndDir()
michael@0:
michael@0: def DeleteFilesAndDir(self):
michael@0: try:
michael@0: os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_1_TEST + ".xml"))
michael@0: except os.error:
michael@0: pass
michael@0: try:
michael@0: os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_2_TEST + ".xml"))
michael@0: except os.error:
michael@0: pass
michael@0: try:
michael@0: os.rmdir(self.output_dir_)
michael@0: except os.error:
michael@0: pass
michael@0:
michael@0: def testOutfile1(self):
michael@0: self._TestOutFile(GTEST_OUTPUT_1_TEST, EXPECTED_XML_1)
michael@0:
michael@0: def testOutfile2(self):
michael@0: self._TestOutFile(GTEST_OUTPUT_2_TEST, EXPECTED_XML_2)
michael@0:
michael@0: def _TestOutFile(self, test_name, expected_xml):
michael@0: gtest_prog_path = gtest_test_utils.GetTestExecutablePath(test_name)
michael@0: command = [gtest_prog_path, "--gtest_output=xml:%s" % self.output_dir_]
michael@0: p = gtest_test_utils.Subprocess(command,
michael@0: working_dir=gtest_test_utils.GetTempDir())
michael@0: self.assert_(p.exited)
michael@0: self.assertEquals(0, p.exit_code)
michael@0:
michael@0: # TODO(wan@google.com): libtool causes the built test binary to be
michael@0: # named lt-gtest_xml_outfiles_test_ instead of
michael@0: # gtest_xml_outfiles_test_. To account for this possibillity, we
michael@0: # allow both names in the following code. We should remove this
michael@0: # hack when Chandler Carruth's libtool replacement tool is ready.
michael@0: output_file_name1 = test_name + ".xml"
michael@0: output_file1 = os.path.join(self.output_dir_, output_file_name1)
michael@0: output_file_name2 = 'lt-' + output_file_name1
michael@0: output_file2 = os.path.join(self.output_dir_, output_file_name2)
michael@0: self.assert_(os.path.isfile(output_file1) or os.path.isfile(output_file2),
michael@0: output_file1)
michael@0:
michael@0: expected = minidom.parseString(expected_xml)
michael@0: if os.path.isfile(output_file1):
michael@0: actual = minidom.parse(output_file1)
michael@0: else:
michael@0: actual = minidom.parse(output_file2)
michael@0: self.NormalizeXml(actual.documentElement)
michael@0: self.AssertEquivalentNodes(expected.documentElement,
michael@0: actual.documentElement)
michael@0: expected.unlink()
michael@0: actual.unlink()
michael@0:
michael@0:
michael@0: if __name__ == "__main__":
michael@0: os.environ["GTEST_STACK_TRACE_DEPTH"] = "0"
michael@0: gtest_test_utils.Main()