Blame nss/external_tests/google_test/gtest/test/gtest_xml_output_unittest.py

Packit 40b132
#!/usr/bin/env python
Packit 40b132
#
Packit 40b132
# Copyright 2006, Google Inc.
Packit 40b132
# All rights reserved.
Packit 40b132
#
Packit 40b132
# Redistribution and use in source and binary forms, with or without
Packit 40b132
# modification, are permitted provided that the following conditions are
Packit 40b132
# met:
Packit 40b132
#
Packit 40b132
#     * Redistributions of source code must retain the above copyright
Packit 40b132
# notice, this list of conditions and the following disclaimer.
Packit 40b132
#     * Redistributions in binary form must reproduce the above
Packit 40b132
# copyright notice, this list of conditions and the following disclaimer
Packit 40b132
# in the documentation and/or other materials provided with the
Packit 40b132
# distribution.
Packit 40b132
#     * Neither the name of Google Inc. nor the names of its
Packit 40b132
# contributors may be used to endorse or promote products derived from
Packit 40b132
# this software without specific prior written permission.
Packit 40b132
#
Packit 40b132
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 40b132
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 40b132
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 40b132
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 40b132
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 40b132
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 40b132
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 40b132
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 40b132
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 40b132
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 40b132
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 40b132
Packit 40b132
"""Unit test for the gtest_xml_output module"""
Packit 40b132
Packit 40b132
__author__ = 'eefacm@gmail.com (Sean Mcafee)'
Packit 40b132
Packit 40b132
import datetime
Packit 40b132
import errno
Packit 40b132
import os
Packit 40b132
import re
Packit 40b132
import sys
Packit 40b132
from xml.dom import minidom, Node
Packit 40b132
Packit 40b132
import gtest_test_utils
Packit 40b132
import gtest_xml_test_utils
Packit 40b132
Packit 40b132
Packit 40b132
GTEST_FILTER_FLAG = '--gtest_filter'
Packit 40b132
GTEST_LIST_TESTS_FLAG = '--gtest_list_tests'
Packit 40b132
GTEST_OUTPUT_FLAG         = "--gtest_output"
Packit 40b132
GTEST_DEFAULT_OUTPUT_FILE = "test_detail.xml"
Packit 40b132
GTEST_PROGRAM_NAME = "gtest_xml_output_unittest_"
Packit 40b132
Packit 40b132
SUPPORTS_STACK_TRACES = False
Packit 40b132
Packit 40b132
if SUPPORTS_STACK_TRACES:
Packit 40b132
  STACK_TRACE_TEMPLATE = '\nStack trace:\n*'
Packit 40b132
else:
Packit 40b132
  STACK_TRACE_TEMPLATE = ''
Packit 40b132
Packit 40b132
EXPECTED_NON_EMPTY_XML = """
Packit 40b132
<testsuites tests="23" failures="4" disabled="2" errors="0" time="*" timestamp="*" name="AllTests" ad_hoc_property="42">
Packit 40b132
  <testsuite name="SuccessfulTest" tests="1" failures="0" disabled="0" errors="0" time="*">
Packit 40b132
    <testcase name="Succeeds" status="run" time="*" classname="SuccessfulTest"/>
Packit 40b132
  </testsuite>
Packit 40b132
  <testsuite name="FailedTest" tests="1" failures="1" disabled="0" errors="0" time="*">
Packit 40b132
    <testcase name="Fails" status="run" time="*" classname="FailedTest">
Packit 40b132
      <failure message="gtest_xml_output_unittest_.cc:*
Value of: 2
Expected: 1" type="">
Packit 40b132
Value of: 2
Packit 40b132
Expected: 1%(stack)s]]></failure>
Packit 40b132
    </testcase>
Packit 40b132
  </testsuite>
Packit 40b132
  <testsuite name="MixedResultTest" tests="3" failures="1" disabled="1" errors="0" time="*">
Packit 40b132
    <testcase name="Succeeds" status="run" time="*" classname="MixedResultTest"/>
Packit 40b132
    <testcase name="Fails" status="run" time="*" classname="MixedResultTest">
Packit 40b132
      <failure message="gtest_xml_output_unittest_.cc:*
Value of: 2
Expected: 1" type="">
Packit 40b132
Value of: 2
Packit 40b132
Expected: 1%(stack)s]]></failure>
Packit 40b132
      <failure message="gtest_xml_output_unittest_.cc:*
Value of: 3
Expected: 2" type="">
Packit 40b132
Value of: 3
Packit 40b132
Expected: 2%(stack)s]]></failure>
Packit 40b132
    </testcase>
Packit 40b132
    <testcase name="DISABLED_test" status="notrun" time="*" classname="MixedResultTest"/>
Packit 40b132
  </testsuite>
Packit 40b132
  <testsuite name="XmlQuotingTest" tests="1" failures="1" disabled="0" errors="0" time="*">
Packit 40b132
    <testcase name="OutputsCData" status="run" time="*" classname="XmlQuotingTest">
Packit 40b132
      <failure message="gtest_xml_output_unittest_.cc:*
Failed
XML output: <?xml encoding="utf-8"><top><![CDATA[cdata text]]></top>" type="">
Packit 40b132
Failed
Packit 40b132
XML output: <top>]]>%(stack)s]]></failure>
Packit 40b132
    </testcase>
Packit 40b132
  </testsuite>
Packit 40b132
  <testsuite name="InvalidCharactersTest" tests="1" failures="1" disabled="0" errors="0" time="*">
Packit 40b132
    <testcase name="InvalidCharactersInMessage" status="run" time="*" classname="InvalidCharactersTest">
Packit 40b132
      <failure message="gtest_xml_output_unittest_.cc:*
Failed
Invalid characters in brackets []" type="">
Packit 40b132
Failed
Packit 40b132
Invalid characters in brackets []%(stack)s]]></failure>
Packit 40b132
    </testcase>
Packit 40b132
  </testsuite>
Packit 40b132
  <testsuite name="DisabledTest" tests="1" failures="0" disabled="1" errors="0" time="*">
Packit 40b132
    <testcase name="DISABLED_test_not_run" status="notrun" time="*" classname="DisabledTest"/>
Packit 40b132
  </testsuite>
Packit 40b132
  <testsuite name="PropertyRecordingTest" tests="4" failures="0" disabled="0" errors="0" time="*" SetUpTestCase="yes" TearDownTestCase="aye">
Packit 40b132
    <testcase name="OneProperty" status="run" time="*" classname="PropertyRecordingTest" key_1="1"/>
Packit 40b132
    <testcase name="IntValuedProperty" status="run" time="*" classname="PropertyRecordingTest" key_int="1"/>
Packit 40b132
    <testcase name="ThreeProperties" status="run" time="*" classname="PropertyRecordingTest" key_1="1" key_2="2" key_3="3"/>
Packit 40b132
    <testcase name="TwoValuesForOneKeyUsesLastValue" status="run" time="*" classname="PropertyRecordingTest" key_1="2"/>
Packit 40b132
  </testsuite>
Packit 40b132
  <testsuite name="NoFixtureTest" tests="3" failures="0" disabled="0" errors="0" time="*">
Packit 40b132
     <testcase name="RecordProperty" status="run" time="*" classname="NoFixtureTest" key="1"/>
Packit 40b132
     <testcase name="ExternalUtilityThatCallsRecordIntValuedProperty" status="run" time="*" classname="NoFixtureTest" key_for_utility_int="1"/>
Packit 40b132
     <testcase name="ExternalUtilityThatCallsRecordStringValuedProperty" status="run" time="*" classname="NoFixtureTest" key_for_utility_string="1"/>
Packit 40b132
  </testsuite>
Packit 40b132
  <testsuite name="Single/ValueParamTest" tests="4" failures="0" disabled="0" errors="0" time="*">
Packit 40b132
    <testcase name="HasValueParamAttribute/0" value_param="33" status="run" time="*" classname="Single/ValueParamTest" />
Packit 40b132
    <testcase name="HasValueParamAttribute/1" value_param="42" status="run" time="*" classname="Single/ValueParamTest" />
Packit 40b132
    <testcase name="AnotherTestThatHasValueParamAttribute/0" value_param="33" status="run" time="*" classname="Single/ValueParamTest" />
Packit 40b132
    <testcase name="AnotherTestThatHasValueParamAttribute/1" value_param="42" status="run" time="*" classname="Single/ValueParamTest" />
Packit 40b132
  </testsuite>
Packit 40b132
  <testsuite name="TypedTest/0" tests="1" failures="0" disabled="0" errors="0" time="*">
Packit 40b132
    <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="TypedTest/0" />
Packit 40b132
  </testsuite>
Packit 40b132
  <testsuite name="TypedTest/1" tests="1" failures="0" disabled="0" errors="0" time="*">
Packit 40b132
    <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="TypedTest/1" />
Packit 40b132
  </testsuite>
Packit 40b132
  <testsuite name="Single/TypeParameterizedTestCase/0" tests="1" failures="0" disabled="0" errors="0" time="*">
Packit 40b132
    <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="Single/TypeParameterizedTestCase/0" />
Packit 40b132
  </testsuite>
Packit 40b132
  <testsuite name="Single/TypeParameterizedTestCase/1" tests="1" failures="0" disabled="0" errors="0" time="*">
Packit 40b132
    <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="Single/TypeParameterizedTestCase/1" />
Packit 40b132
  </testsuite>
Packit 40b132
</testsuites>""" % {'stack': STACK_TRACE_TEMPLATE}
Packit 40b132
Packit 40b132
EXPECTED_FILTERED_TEST_XML = """
Packit 40b132
Packit 40b132
            timestamp="*" name="AllTests" ad_hoc_property="42">
Packit 40b132
  
Packit 40b132
             errors="0" time="*">
Packit 40b132
    <testcase name="Succeeds" status="run" time="*" classname="SuccessfulTest"/>
Packit 40b132
  </testsuite>
Packit 40b132
</testsuites>"""
Packit 40b132
Packit 40b132
EXPECTED_EMPTY_XML = """
Packit 40b132
Packit 40b132
            timestamp="*" name="AllTests">
Packit 40b132
</testsuites>"""
Packit 40b132
Packit 40b132
GTEST_PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath(GTEST_PROGRAM_NAME)
Packit 40b132
Packit 40b132
SUPPORTS_TYPED_TESTS = 'TypedTest' in gtest_test_utils.Subprocess(
Packit 40b132
    [GTEST_PROGRAM_PATH, GTEST_LIST_TESTS_FLAG], capture_stderr=False).output
Packit 40b132
Packit 40b132
Packit 40b132
class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
Packit 40b132
  """
Packit 40b132
  Unit test for Google Test's XML output functionality.
Packit 40b132
  """
Packit 40b132
Packit 40b132
  # This test currently breaks on platforms that do not support typed and
Packit 40b132
  # type-parameterized tests, so we don't run it under them.
Packit 40b132
  if SUPPORTS_TYPED_TESTS:
Packit 40b132
    def testNonEmptyXmlOutput(self):
Packit 40b132
      """
Packit 40b132
      Runs a test program that generates a non-empty XML output, and
Packit 40b132
      tests that the XML output is expected.
Packit 40b132
      """
Packit 40b132
      self._TestXmlOutput(GTEST_PROGRAM_NAME, EXPECTED_NON_EMPTY_XML, 1)
Packit 40b132
Packit 40b132
  def testEmptyXmlOutput(self):
Packit 40b132
    """Verifies XML output for a Google Test binary without actual tests.
Packit 40b132
Packit 40b132
    Runs a test program that generates an empty XML output, and
Packit 40b132
    tests that the XML output is expected.
Packit 40b132
    """
Packit 40b132
Packit 40b132
    self._TestXmlOutput('gtest_no_test_unittest', EXPECTED_EMPTY_XML, 0)
Packit 40b132
Packit 40b132
  def testTimestampValue(self):
Packit 40b132
    """Checks whether the timestamp attribute in the XML output is valid.
Packit 40b132
Packit 40b132
    Runs a test program that generates an empty XML output, and checks if
Packit 40b132
    the timestamp attribute in the testsuites tag is valid.
Packit 40b132
    """
Packit 40b132
    actual = self._GetXmlOutput('gtest_no_test_unittest', [], 0)
Packit 40b132
    date_time_str = actual.documentElement.getAttributeNode('timestamp').value
Packit 40b132
    # datetime.strptime() is only available in Python 2.5+ so we have to
Packit 40b132
    # parse the expected datetime manually.
Packit 40b132
    match = re.match(r'(\d+)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)', date_time_str)
Packit 40b132
    self.assertTrue(
Packit 40b132
        re.match,
Packit 40b132
        'XML datettime string %s has incorrect format' % date_time_str)
Packit 40b132
    date_time_from_xml = datetime.datetime(
Packit 40b132
        year=int(match.group(1)), month=int(match.group(2)),
Packit 40b132
        day=int(match.group(3)), hour=int(match.group(4)),
Packit 40b132
        minute=int(match.group(5)), second=int(match.group(6)))
Packit 40b132
Packit 40b132
    time_delta = abs(datetime.datetime.now() - date_time_from_xml)
Packit 40b132
    # timestamp value should be near the current local time
Packit 40b132
    self.assertTrue(time_delta < datetime.timedelta(seconds=600),
Packit 40b132
                    'time_delta is %s' % time_delta)
Packit 40b132
    actual.unlink()
Packit 40b132
Packit 40b132
  def testDefaultOutputFile(self):
Packit 40b132
    """
Packit 40b132
    Confirms that Google Test produces an XML output file with the expected
Packit 40b132
    default name if no name is explicitly specified.
Packit 40b132
    """
Packit 40b132
    output_file = os.path.join(gtest_test_utils.GetTempDir(),
Packit 40b132
                               GTEST_DEFAULT_OUTPUT_FILE)
Packit 40b132
    gtest_prog_path = gtest_test_utils.GetTestExecutablePath(
Packit 40b132
        'gtest_no_test_unittest')
Packit 40b132
    try:
Packit 40b132
      os.remove(output_file)
Packit 40b132
    except OSError, e:
Packit 40b132
      if e.errno != errno.ENOENT:
Packit 40b132
        raise
Packit 40b132
Packit 40b132
    p = gtest_test_utils.Subprocess(
Packit 40b132
        [gtest_prog_path, '%s=xml' % GTEST_OUTPUT_FLAG],
Packit 40b132
        working_dir=gtest_test_utils.GetTempDir())
Packit 40b132
    self.assert_(p.exited)
Packit 40b132
    self.assertEquals(0, p.exit_code)
Packit 40b132
    self.assert_(os.path.isfile(output_file))
Packit 40b132
Packit 40b132
  def testSuppressedXmlOutput(self):
Packit 40b132
    """
Packit 40b132
    Tests that no XML file is generated if the default XML listener is
Packit 40b132
    shut down before RUN_ALL_TESTS is invoked.
Packit 40b132
    """
Packit 40b132
Packit 40b132
    xml_path = os.path.join(gtest_test_utils.GetTempDir(),
Packit 40b132
                            GTEST_PROGRAM_NAME + 'out.xml')
Packit 40b132
    if os.path.isfile(xml_path):
Packit 40b132
      os.remove(xml_path)
Packit 40b132
Packit 40b132
    command = [GTEST_PROGRAM_PATH,
Packit 40b132
               '%s=xml:%s' % (GTEST_OUTPUT_FLAG, xml_path),
Packit 40b132
               '--shut_down_xml']
Packit 40b132
    p = gtest_test_utils.Subprocess(command)
Packit 40b132
    if p.terminated_by_signal:
Packit 40b132
      # p.signal is avalable only if p.terminated_by_signal is True.
Packit 40b132
      self.assertFalse(
Packit 40b132
          p.terminated_by_signal,
Packit 40b132
          '%s was killed by signal %d' % (GTEST_PROGRAM_NAME, p.signal))
Packit 40b132
    else:
Packit 40b132
      self.assert_(p.exited)
Packit 40b132
      self.assertEquals(1, p.exit_code,
Packit 40b132
                        "'%s' exited with code %s, which doesn't match "
Packit 40b132
                        'the expected exit code %s.'
Packit 40b132
                        % (command, p.exit_code, 1))
Packit 40b132
Packit 40b132
    self.assert_(not os.path.isfile(xml_path))
Packit 40b132
Packit 40b132
  def testFilteredTestXmlOutput(self):
Packit 40b132
    """Verifies XML output when a filter is applied.
Packit 40b132
Packit 40b132
    Runs a test program that executes only some tests and verifies that
Packit 40b132
    non-selected tests do not show up in the XML output.
Packit 40b132
    """
Packit 40b132
Packit 40b132
    self._TestXmlOutput(GTEST_PROGRAM_NAME, EXPECTED_FILTERED_TEST_XML, 0,
Packit 40b132
                        extra_args=['%s=SuccessfulTest.*' % GTEST_FILTER_FLAG])
Packit 40b132
Packit 40b132
  def _GetXmlOutput(self, gtest_prog_name, extra_args, expected_exit_code):
Packit 40b132
    """
Packit 40b132
    Returns the xml output generated by running the program gtest_prog_name.
Packit 40b132
    Furthermore, the program's exit code must be expected_exit_code.
Packit 40b132
    """
Packit 40b132
    xml_path = os.path.join(gtest_test_utils.GetTempDir(),
Packit 40b132
                            gtest_prog_name + 'out.xml')
Packit 40b132
    gtest_prog_path = gtest_test_utils.GetTestExecutablePath(gtest_prog_name)
Packit 40b132
Packit 40b132
    command = ([gtest_prog_path, '%s=xml:%s' % (GTEST_OUTPUT_FLAG, xml_path)] +
Packit 40b132
               extra_args)
Packit 40b132
    p = gtest_test_utils.Subprocess(command)
Packit 40b132
    if p.terminated_by_signal:
Packit 40b132
      self.assert_(False,
Packit 40b132
                   '%s was killed by signal %d' % (gtest_prog_name, p.signal))
Packit 40b132
    else:
Packit 40b132
      self.assert_(p.exited)
Packit 40b132
      self.assertEquals(expected_exit_code, p.exit_code,
Packit 40b132
                        "'%s' exited with code %s, which doesn't match "
Packit 40b132
                        'the expected exit code %s.'
Packit 40b132
                        % (command, p.exit_code, expected_exit_code))
Packit 40b132
    actual = minidom.parse(xml_path)
Packit 40b132
    return actual
Packit 40b132
Packit 40b132
  def _TestXmlOutput(self, gtest_prog_name, expected_xml,
Packit 40b132
                     expected_exit_code, extra_args=None):
Packit 40b132
    """
Packit 40b132
    Asserts that the XML document generated by running the program
Packit 40b132
    gtest_prog_name matches expected_xml, a string containing another
Packit 40b132
    XML document.  Furthermore, the program's exit code must be
Packit 40b132
    expected_exit_code.
Packit 40b132
    """
Packit 40b132
Packit 40b132
    actual = self._GetXmlOutput(gtest_prog_name, extra_args or [],
Packit 40b132
                                expected_exit_code)
Packit 40b132
    expected = minidom.parseString(expected_xml)
Packit 40b132
    self.NormalizeXml(actual.documentElement)
Packit 40b132
    self.AssertEquivalentNodes(expected.documentElement,
Packit 40b132
                               actual.documentElement)
Packit 40b132
    expected.unlink()
Packit 40b132
    actual.unlink()
Packit 40b132
Packit 40b132
Packit 40b132
if __name__ == '__main__':
Packit 40b132
  os.environ['GTEST_STACK_TRACE_DEPTH'] = '1'
Packit 40b132
  gtest_test_utils.Main()