11be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#!/usr/bin/env python
21be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#
31be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# Copyright 2006, Google Inc.
41be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# All rights reserved.
51be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#
61be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# Redistribution and use in source and binary forms, with or without
71be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# modification, are permitted provided that the following conditions are
81be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# met:
91be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#
101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#     * Redistributions of source code must retain the above copyright
111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# notice, this list of conditions and the following disclaimer.
121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#     * Redistributions in binary form must reproduce the above
131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# copyright notice, this list of conditions and the following disclaimer
141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# in the documentation and/or other materials provided with the
151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# distribution.
161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#     * Neither the name of Google Inc. nor the names of its
171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# contributors may be used to endorse or promote products derived from
181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# this software without specific prior written permission.
191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#
201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"""Unit test for the gtest_xml_output module"""
331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania__author__ = 'eefacm@gmail.com (Sean Mcafee)'
351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
36fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesimport datetime
371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaimport errno
381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaimport os
39fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesimport re
401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaimport sys
411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniafrom xml.dom import minidom, Node
421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotimport gtest_test_utils
441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaimport gtest_xml_test_utils
451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
47fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesGTEST_FILTER_FLAG = '--gtest_filter'
48fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesGTEST_LIST_TESTS_FLAG = '--gtest_list_tests'
491be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_OUTPUT_FLAG         = "--gtest_output"
501be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFAULT_OUTPUT_FILE = "test_detail.xml"
5141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_PROGRAM_NAME = "gtest_xml_output_unittest_"
5241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
5341d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotSUPPORTS_STACK_TRACES = False
5441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
5541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotif SUPPORTS_STACK_TRACES:
56fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  STACK_TRACE_TEMPLATE = '\nStack trace:\n*'
5741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotelse:
58fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  STACK_TRACE_TEMPLATE = ''
591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
601be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaEXPECTED_NON_EMPTY_XML = """<?xml version="1.0" encoding="UTF-8"?>
61fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes<testsuites tests="23" failures="4" disabled="2" errors="0" time="*" timestamp="*" name="AllTests" ad_hoc_property="42">
621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  <testsuite name="SuccessfulTest" tests="1" failures="0" disabled="0" errors="0" time="*">
631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    <testcase name="Succeeds" status="run" time="*" classname="SuccessfulTest"/>
641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  </testsuite>
651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  <testsuite name="FailedTest" tests="1" failures="1" disabled="0" errors="0" time="*">
661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    <testcase name="Fails" status="run" time="*" classname="FailedTest">
67fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      <failure message="gtest_xml_output_unittest_.cc:*&#x0A;Value of: 2&#x0A;Expected: 1" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
681be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaValue of: 2
6941d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotExpected: 1%(stack)s]]></failure>
701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    </testcase>
711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  </testsuite>
721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  <testsuite name="MixedResultTest" tests="3" failures="1" disabled="1" errors="0" time="*">
731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    <testcase name="Succeeds" status="run" time="*" classname="MixedResultTest"/>
741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    <testcase name="Fails" status="run" time="*" classname="MixedResultTest">
75fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      <failure message="gtest_xml_output_unittest_.cc:*&#x0A;Value of: 2&#x0A;Expected: 1" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
761be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaValue of: 2
7741d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotExpected: 1%(stack)s]]></failure>
78fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      <failure message="gtest_xml_output_unittest_.cc:*&#x0A;Value of: 3&#x0A;Expected: 2" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
791be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaValue of: 3
8041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotExpected: 2%(stack)s]]></failure>
811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    </testcase>
821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    <testcase name="DISABLED_test" status="notrun" time="*" classname="MixedResultTest"/>
831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  </testsuite>
8441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  <testsuite name="XmlQuotingTest" tests="1" failures="1" disabled="0" errors="0" time="*">
8541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    <testcase name="OutputsCData" status="run" time="*" classname="XmlQuotingTest">
86fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      <failure message="gtest_xml_output_unittest_.cc:*&#x0A;Failed&#x0A;XML output: &lt;?xml encoding=&quot;utf-8&quot;&gt;&lt;top&gt;&lt;![CDATA[cdata text]]&gt;&lt;/top&gt;" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
8741d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotFailed
8841d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotXML output: <?xml encoding="utf-8"><top><![CDATA[cdata text]]>]]&gt;<![CDATA[</top>%(stack)s]]></failure>
8941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    </testcase>
9041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  </testsuite>
9141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  <testsuite name="InvalidCharactersTest" tests="1" failures="1" disabled="0" errors="0" time="*">
9241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    <testcase name="InvalidCharactersInMessage" status="run" time="*" classname="InvalidCharactersTest">
93fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      <failure message="gtest_xml_output_unittest_.cc:*&#x0A;Failed&#x0A;Invalid characters in brackets []" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
9441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotFailed
9541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotInvalid characters in brackets []%(stack)s]]></failure>
9641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    </testcase>
9741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  </testsuite>
981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  <testsuite name="DisabledTest" tests="1" failures="0" disabled="1" errors="0" time="*">
991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    <testcase name="DISABLED_test_not_run" status="notrun" time="*" classname="DisabledTest"/>
1001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  </testsuite>
101fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  <testsuite name="PropertyRecordingTest" tests="4" failures="0" disabled="0" errors="0" time="*" SetUpTestCase="yes" TearDownTestCase="aye">
1021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    <testcase name="OneProperty" status="run" time="*" classname="PropertyRecordingTest" key_1="1"/>
1031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    <testcase name="IntValuedProperty" status="run" time="*" classname="PropertyRecordingTest" key_int="1"/>
1041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    <testcase name="ThreeProperties" status="run" time="*" classname="PropertyRecordingTest" key_1="1" key_2="2" key_3="3"/>
1051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    <testcase name="TwoValuesForOneKeyUsesLastValue" status="run" time="*" classname="PropertyRecordingTest" key_1="2"/>
1061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  </testsuite>
1071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  <testsuite name="NoFixtureTest" tests="3" failures="0" disabled="0" errors="0" time="*">
1081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania     <testcase name="RecordProperty" status="run" time="*" classname="NoFixtureTest" key="1"/>
1091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania     <testcase name="ExternalUtilityThatCallsRecordIntValuedProperty" status="run" time="*" classname="NoFixtureTest" key_for_utility_int="1"/>
1101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania     <testcase name="ExternalUtilityThatCallsRecordStringValuedProperty" status="run" time="*" classname="NoFixtureTest" key_for_utility_string="1"/>
1111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  </testsuite>
11241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  <testsuite name="Single/ValueParamTest" tests="4" failures="0" disabled="0" errors="0" time="*">
11341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    <testcase name="HasValueParamAttribute/0" value_param="33" status="run" time="*" classname="Single/ValueParamTest" />
11441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    <testcase name="HasValueParamAttribute/1" value_param="42" status="run" time="*" classname="Single/ValueParamTest" />
11541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    <testcase name="AnotherTestThatHasValueParamAttribute/0" value_param="33" status="run" time="*" classname="Single/ValueParamTest" />
11641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    <testcase name="AnotherTestThatHasValueParamAttribute/1" value_param="42" status="run" time="*" classname="Single/ValueParamTest" />
11741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  </testsuite>
11841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  <testsuite name="TypedTest/0" tests="1" failures="0" disabled="0" errors="0" time="*">
11941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="TypedTest/0" />
12041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  </testsuite>
12141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  <testsuite name="TypedTest/1" tests="1" failures="0" disabled="0" errors="0" time="*">
12241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="TypedTest/1" />
12341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  </testsuite>
12441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  <testsuite name="Single/TypeParameterizedTestCase/0" tests="1" failures="0" disabled="0" errors="0" time="*">
12541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="Single/TypeParameterizedTestCase/0" />
12641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  </testsuite>
12741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  <testsuite name="Single/TypeParameterizedTestCase/1" tests="1" failures="0" disabled="0" errors="0" time="*">
12841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="Single/TypeParameterizedTestCase/1" />
12941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  </testsuite>
13041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot</testsuites>""" % {'stack': STACK_TRACE_TEMPLATE}
1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
132fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesEXPECTED_FILTERED_TEST_XML = """<?xml version="1.0" encoding="UTF-8"?>
133fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes<testsuites tests="1" failures="0" disabled="0" errors="0" time="*"
134fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes            timestamp="*" name="AllTests" ad_hoc_property="42">
135fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  <testsuite name="SuccessfulTest" tests="1" failures="0" disabled="0"
136fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes             errors="0" time="*">
137fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    <testcase name="Succeeds" status="run" time="*" classname="SuccessfulTest"/>
138fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  </testsuite>
139fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes</testsuites>"""
1401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1411be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaEXPECTED_EMPTY_XML = """<?xml version="1.0" encoding="UTF-8"?>
142fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes<testsuites tests="0" failures="0" disabled="0" errors="0" time="*"
143fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes            timestamp="*" name="AllTests">
14441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot</testsuites>"""
1451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
146fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesGTEST_PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath(GTEST_PROGRAM_NAME)
147fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
148fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesSUPPORTS_TYPED_TESTS = 'TypedTest' in gtest_test_utils.Subprocess(
149fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    [GTEST_PROGRAM_PATH, GTEST_LIST_TESTS_FLAG], capture_stderr=False).output
150fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
1511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
1531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  """
1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Unit test for Google Test's XML output functionality.
1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  """
1561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
157fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  # This test currently breaks on platforms that do not support typed and
158fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  # type-parameterized tests, so we don't run it under them.
159fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  if SUPPORTS_TYPED_TESTS:
160fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    def testNonEmptyXmlOutput(self):
161fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      """
162fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      Runs a test program that generates a non-empty XML output, and
163fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      tests that the XML output is expected.
164fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      """
165fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      self._TestXmlOutput(GTEST_PROGRAM_NAME, EXPECTED_NON_EMPTY_XML, 1)
1661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  def testEmptyXmlOutput(self):
168fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    """Verifies XML output for a Google Test binary without actual tests.
169fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
1701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    Runs a test program that generates an empty XML output, and
1711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    tests that the XML output is expected.
1721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    """
1731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
174fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    self._TestXmlOutput('gtest_no_test_unittest', EXPECTED_EMPTY_XML, 0)
175fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
176fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  def testTimestampValue(self):
177fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    """Checks whether the timestamp attribute in the XML output is valid.
178fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
179fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    Runs a test program that generates an empty XML output, and checks if
180fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    the timestamp attribute in the testsuites tag is valid.
181fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    """
182fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    actual = self._GetXmlOutput('gtest_no_test_unittest', [], 0)
183fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    date_time_str = actual.documentElement.getAttributeNode('timestamp').value
184fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    # datetime.strptime() is only available in Python 2.5+ so we have to
185fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    # parse the expected datetime manually.
186fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    match = re.match(r'(\d+)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)', date_time_str)
187fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    self.assertTrue(
188fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        re.match,
189fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        'XML datettime string %s has incorrect format' % date_time_str)
190fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    date_time_from_xml = datetime.datetime(
191fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        year=int(match.group(1)), month=int(match.group(2)),
192fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        day=int(match.group(3)), hour=int(match.group(4)),
193fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        minute=int(match.group(5)), second=int(match.group(6)))
194fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
195fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    time_delta = abs(datetime.datetime.now() - date_time_from_xml)
196fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    # timestamp value should be near the current local time
197fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    self.assertTrue(time_delta < datetime.timedelta(seconds=600),
198fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                    'time_delta is %s' % time_delta)
199fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    actual.unlink()
2001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  def testDefaultOutputFile(self):
2021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    """
2031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    Confirms that Google Test produces an XML output file with the expected
2041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    default name if no name is explicitly specified.
2051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    """
20641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    output_file = os.path.join(gtest_test_utils.GetTempDir(),
20741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                               GTEST_DEFAULT_OUTPUT_FILE)
20841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    gtest_prog_path = gtest_test_utils.GetTestExecutablePath(
209fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        'gtest_no_test_unittest')
2101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    try:
2111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      os.remove(output_file)
2121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    except OSError, e:
2131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      if e.errno != errno.ENOENT:
2141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        raise
2151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    p = gtest_test_utils.Subprocess(
217fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        [gtest_prog_path, '%s=xml' % GTEST_OUTPUT_FLAG],
21841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        working_dir=gtest_test_utils.GetTempDir())
2191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.assert_(p.exited)
2201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.assertEquals(0, p.exit_code)
2211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.assert_(os.path.isfile(output_file))
2221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
22341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  def testSuppressedXmlOutput(self):
22441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    """
22541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    Tests that no XML file is generated if the default XML listener is
22641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    shut down before RUN_ALL_TESTS is invoked.
22741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    """
22841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
22941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    xml_path = os.path.join(gtest_test_utils.GetTempDir(),
230fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                            GTEST_PROGRAM_NAME + 'out.xml')
23141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    if os.path.isfile(xml_path):
23241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      os.remove(xml_path)
23341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
234fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    command = [GTEST_PROGRAM_PATH,
235fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes               '%s=xml:%s' % (GTEST_OUTPUT_FLAG, xml_path),
236fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes               '--shut_down_xml']
23741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    p = gtest_test_utils.Subprocess(command)
23841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    if p.terminated_by_signal:
239fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      # p.signal is avalable only if p.terminated_by_signal is True.
240fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      self.assertFalse(
241fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes          p.terminated_by_signal,
242fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes          '%s was killed by signal %d' % (GTEST_PROGRAM_NAME, p.signal))
24341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    else:
24441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      self.assert_(p.exited)
24541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      self.assertEquals(1, p.exit_code,
24641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                        "'%s' exited with code %s, which doesn't match "
247fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                        'the expected exit code %s.'
24841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                        % (command, p.exit_code, 1))
24941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
25041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    self.assert_(not os.path.isfile(xml_path))
25141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
252fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  def testFilteredTestXmlOutput(self):
253fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    """Verifies XML output when a filter is applied.
2541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
255fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    Runs a test program that executes only some tests and verifies that
256fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    non-selected tests do not show up in the XML output.
2571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    """
258fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
259fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    self._TestXmlOutput(GTEST_PROGRAM_NAME, EXPECTED_FILTERED_TEST_XML, 0,
260fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                        extra_args=['%s=SuccessfulTest.*' % GTEST_FILTER_FLAG])
261fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
262fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  def _GetXmlOutput(self, gtest_prog_name, extra_args, expected_exit_code):
263fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    """
264fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    Returns the xml output generated by running the program gtest_prog_name.
265fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    Furthermore, the program's exit code must be expected_exit_code.
2661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    """
26741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    xml_path = os.path.join(gtest_test_utils.GetTempDir(),
268fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                            gtest_prog_name + 'out.xml')
26941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    gtest_prog_path = gtest_test_utils.GetTestExecutablePath(gtest_prog_name)
2701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
271fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    command = ([gtest_prog_path, '%s=xml:%s' % (GTEST_OUTPUT_FLAG, xml_path)] +
272fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes               extra_args)
2731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    p = gtest_test_utils.Subprocess(command)
2741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if p.terminated_by_signal:
2751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      self.assert_(False,
276fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                   '%s was killed by signal %d' % (gtest_prog_name, p.signal))
2771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    else:
2781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      self.assert_(p.exited)
2791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      self.assertEquals(expected_exit_code, p.exit_code,
2801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                        "'%s' exited with code %s, which doesn't match "
281fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                        'the expected exit code %s.'
2821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                        % (command, p.exit_code, expected_exit_code))
283fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    actual = minidom.parse(xml_path)
284fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    return actual
2851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
286fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  def _TestXmlOutput(self, gtest_prog_name, expected_xml,
287fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                     expected_exit_code, extra_args=None):
288fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    """
289fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    Asserts that the XML document generated by running the program
290fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    gtest_prog_name matches expected_xml, a string containing another
291fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    XML document.  Furthermore, the program's exit code must be
292fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    expected_exit_code.
293fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    """
294fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
295fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    actual = self._GetXmlOutput(gtest_prog_name, extra_args or [],
296fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                                expected_exit_code)
2971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    expected = minidom.parseString(expected_xml)
2981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.NormalizeXml(actual.documentElement)
2991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.AssertEquivalentNodes(expected.documentElement,
3001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                               actual.documentElement)
3011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    expected.unlink()
302fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    actual.unlink()
3031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaif __name__ == '__main__':
3061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  os.environ['GTEST_STACK_TRACE_DEPTH'] = '1'
3071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  gtest_test_utils.Main()
308