michael@0: #!/usr/bin/env python
michael@0: #
michael@0: # Copyright 2006, 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__ = 'eefacm@gmail.com (Sean Mcafee)'
michael@0:
michael@0: import datetime
michael@0: import errno
michael@0: import os
michael@0: import re
michael@0: import sys
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_LIST_TESTS_FLAG = '--gtest_list_tests'
michael@0: GTEST_OUTPUT_FLAG = "--gtest_output"
michael@0: GTEST_DEFAULT_OUTPUT_FILE = "test_detail.xml"
michael@0: GTEST_PROGRAM_NAME = "gtest_xml_output_unittest_"
michael@0:
michael@0: SUPPORTS_STACK_TRACES = False
michael@0:
michael@0: if SUPPORTS_STACK_TRACES:
michael@0: STACK_TRACE_TEMPLATE = '\nStack trace:\n*'
michael@0: else:
michael@0: STACK_TRACE_TEMPLATE = ''
michael@0:
michael@0: EXPECTED_NON_EMPTY_XML = """
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0: ]]>%(stack)s]]>
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0: """ % {'stack': STACK_TRACE_TEMPLATE}
michael@0:
michael@0:
michael@0: EXPECTED_EMPTY_XML = """
michael@0:
michael@0: """
michael@0:
michael@0: GTEST_PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath(GTEST_PROGRAM_NAME)
michael@0:
michael@0: SUPPORTS_TYPED_TESTS = 'TypedTest' in gtest_test_utils.Subprocess(
michael@0: [GTEST_PROGRAM_PATH, GTEST_LIST_TESTS_FLAG], capture_stderr=False).output
michael@0:
michael@0:
michael@0: class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
michael@0: """
michael@0: Unit test for Google Test's XML output functionality.
michael@0: """
michael@0:
michael@0: # This test currently breaks on platforms that do not support typed and
michael@0: # type-parameterized tests, so we don't run it under them.
michael@0: if SUPPORTS_TYPED_TESTS:
michael@0: def testNonEmptyXmlOutput(self):
michael@0: """
michael@0: Runs a test program that generates a non-empty XML output, and
michael@0: tests that the XML output is expected.
michael@0: """
michael@0: self._TestXmlOutput(GTEST_PROGRAM_NAME, EXPECTED_NON_EMPTY_XML, 1)
michael@0:
michael@0: def testEmptyXmlOutput(self):
michael@0: """Verifies XML output for a Google Test binary without actual tests.
michael@0:
michael@0: Runs a test program that generates an empty XML output, and
michael@0: tests that the XML output is expected.
michael@0: """
michael@0:
michael@0: self._TestXmlOutput('gtest_no_test_unittest', EXPECTED_EMPTY_XML, 0)
michael@0:
michael@0: def testTimestampValue(self):
michael@0: """Checks whether the timestamp attribute in the XML output is valid.
michael@0:
michael@0: Runs a test program that generates an empty XML output, and checks if
michael@0: the timestamp attribute in the testsuites tag is valid.
michael@0: """
michael@0: actual = self._GetXmlOutput('gtest_no_test_unittest', 0)
michael@0: date_time_str = actual.documentElement.getAttributeNode('timestamp').value
michael@0: # datetime.strptime() is only available in Python 2.5+ so we have to
michael@0: # parse the expected datetime manually.
michael@0: match = re.match(r'(\d+)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)', date_time_str)
michael@0: self.assertTrue(
michael@0: re.match,
michael@0: 'XML datettime string %s has incorrect format' % date_time_str)
michael@0: date_time_from_xml = datetime.datetime(
michael@0: year=int(match.group(1)), month=int(match.group(2)),
michael@0: day=int(match.group(3)), hour=int(match.group(4)),
michael@0: minute=int(match.group(5)), second=int(match.group(6)))
michael@0:
michael@0: time_delta = abs(datetime.datetime.now() - date_time_from_xml)
michael@0: # timestamp value should be near the current local time
michael@0: self.assertTrue(time_delta < datetime.timedelta(seconds=600),
michael@0: 'time_delta is %s' % time_delta)
michael@0: actual.unlink()
michael@0:
michael@0: def testDefaultOutputFile(self):
michael@0: """
michael@0: Confirms that Google Test produces an XML output file with the expected
michael@0: default name if no name is explicitly specified.
michael@0: """
michael@0: output_file = os.path.join(gtest_test_utils.GetTempDir(),
michael@0: GTEST_DEFAULT_OUTPUT_FILE)
michael@0: gtest_prog_path = gtest_test_utils.GetTestExecutablePath(
michael@0: 'gtest_no_test_unittest')
michael@0: try:
michael@0: os.remove(output_file)
michael@0: except OSError, e:
michael@0: if e.errno != errno.ENOENT:
michael@0: raise
michael@0:
michael@0: p = gtest_test_utils.Subprocess(
michael@0: [gtest_prog_path, '%s=xml' % GTEST_OUTPUT_FLAG],
michael@0: working_dir=gtest_test_utils.GetTempDir())
michael@0: self.assert_(p.exited)
michael@0: self.assertEquals(0, p.exit_code)
michael@0: self.assert_(os.path.isfile(output_file))
michael@0:
michael@0: def testSuppressedXmlOutput(self):
michael@0: """
michael@0: Tests that no XML file is generated if the default XML listener is
michael@0: shut down before RUN_ALL_TESTS is invoked.
michael@0: """
michael@0:
michael@0: xml_path = os.path.join(gtest_test_utils.GetTempDir(),
michael@0: GTEST_PROGRAM_NAME + 'out.xml')
michael@0: if os.path.isfile(xml_path):
michael@0: os.remove(xml_path)
michael@0:
michael@0: command = [GTEST_PROGRAM_PATH,
michael@0: '%s=xml:%s' % (GTEST_OUTPUT_FLAG, xml_path),
michael@0: '--shut_down_xml']
michael@0: p = gtest_test_utils.Subprocess(command)
michael@0: if p.terminated_by_signal:
michael@0: # p.signal is avalable only if p.terminated_by_signal is True.
michael@0: self.assertFalse(
michael@0: p.terminated_by_signal,
michael@0: '%s was killed by signal %d' % (GTEST_PROGRAM_NAME, p.signal))
michael@0: else:
michael@0: self.assert_(p.exited)
michael@0: self.assertEquals(1, p.exit_code,
michael@0: "'%s' exited with code %s, which doesn't match "
michael@0: 'the expected exit code %s.'
michael@0: % (command, p.exit_code, 1))
michael@0:
michael@0: self.assert_(not os.path.isfile(xml_path))
michael@0:
michael@0: def _GetXmlOutput(self, gtest_prog_name, expected_exit_code):
michael@0: """
michael@0: Returns the xml output generated by running the program gtest_prog_name.
michael@0: Furthermore, the program's exit code must be expected_exit_code.
michael@0: """
michael@0: xml_path = os.path.join(gtest_test_utils.GetTempDir(),
michael@0: gtest_prog_name + 'out.xml')
michael@0: gtest_prog_path = gtest_test_utils.GetTestExecutablePath(gtest_prog_name)
michael@0:
michael@0: command = [gtest_prog_path, '%s=xml:%s' % (GTEST_OUTPUT_FLAG, xml_path)]
michael@0: p = gtest_test_utils.Subprocess(command)
michael@0: if p.terminated_by_signal:
michael@0: self.assert_(False,
michael@0: '%s was killed by signal %d' % (gtest_prog_name, p.signal))
michael@0: else:
michael@0: self.assert_(p.exited)
michael@0: self.assertEquals(expected_exit_code, p.exit_code,
michael@0: "'%s' exited with code %s, which doesn't match "
michael@0: 'the expected exit code %s.'
michael@0: % (command, p.exit_code, expected_exit_code))
michael@0: actual = minidom.parse(xml_path)
michael@0: return actual
michael@0:
michael@0: def _TestXmlOutput(self, gtest_prog_name, expected_xml, expected_exit_code):
michael@0: """
michael@0: Asserts that the XML document generated by running the program
michael@0: gtest_prog_name matches expected_xml, a string containing another
michael@0: XML document. Furthermore, the program's exit code must be
michael@0: expected_exit_code.
michael@0: """
michael@0:
michael@0: actual = self._GetXmlOutput(gtest_prog_name, expected_exit_code)
michael@0: expected = minidom.parseString(expected_xml)
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'] = '1'
michael@0: gtest_test_utils.Main()