1#!/usr/bin/env python 2# 3# Copyright 2006, 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"""Unit test for Google Test's --gtest_list_tests flag. 32 33A user can ask Google Test to list all tests by specifying the 34--gtest_list_tests flag. If output is requested, via --gtest_output=xml 35or --gtest_output=json, the tests are listed, with extra information in the 36output file. 37This script tests such functionality by invoking gtest_list_output_unittest_ 38 (a program written with Google Test) the command line flags. 39""" 40 41import os 42import re 43import gtest_test_utils 44 45GTEST_LIST_TESTS_FLAG = '--gtest_list_tests' 46GTEST_OUTPUT_FLAG = '--gtest_output' 47 48EXPECTED_XML = """<\?xml version="1.0" encoding="UTF-8"\?> 49<testsuites tests="2" name="AllTests"> 50 <testsuite name="FooTest" tests="2"> 51 <testcase name="Test1" file=".*gtest_list_output_unittest_.cc" line="43" /> 52 <testcase name="Test2" file=".*gtest_list_output_unittest_.cc" line="45" /> 53 </testsuite> 54</testsuites> 55""" 56 57EXPECTED_JSON = """{ 58 "tests": 2, 59 "name": "AllTests", 60 "testsuites": \[ 61 { 62 "name": "FooTest", 63 "tests": 2, 64 "testsuite": \[ 65 { 66 "name": "Test1", 67 "file": ".*gtest_list_output_unittest_.cc", 68 "line": 43 69 }, 70 { 71 "name": "Test2", 72 "file": ".*gtest_list_output_unittest_.cc", 73 "line": 45 74 } 75 \] 76 } 77 \] 78} 79""" 80 81 82class GTestListTestsOutputUnitTest(gtest_test_utils.TestCase): 83 """Unit test for Google Test's list tests with output to file functionality. 84 """ 85 86 def testXml(self): 87 """Verifies XML output for listing tests in a Google Test binary. 88 89 Runs a test program that generates an empty XML output, and 90 tests that the XML output is expected. 91 """ 92 self._TestOutput('xml', EXPECTED_XML) 93 94 def testJSON(self): 95 """Verifies XML output for listing tests in a Google Test binary. 96 97 Runs a test program that generates an empty XML output, and 98 tests that the XML output is expected. 99 """ 100 self._TestOutput('json', EXPECTED_JSON) 101 102 def _GetOutput(self, out_format): 103 file_path = os.path.join(gtest_test_utils.GetTempDir(), 104 'test_out.' + out_format) 105 gtest_prog_path = gtest_test_utils.GetTestExecutablePath( 106 'gtest_list_output_unittest_') 107 108 command = ([ 109 gtest_prog_path, 110 '%s=%s:%s' % (GTEST_OUTPUT_FLAG, out_format, file_path), 111 '--gtest_list_tests' 112 ]) 113 environ_copy = os.environ.copy() 114 p = gtest_test_utils.Subprocess( 115 command, env=environ_copy, working_dir=gtest_test_utils.GetTempDir()) 116 117 self.assert_(p.exited) 118 self.assertEquals(0, p.exit_code) 119 with open(file_path) as f: 120 result = f.read() 121 return result 122 123 def _TestOutput(self, test_format, expected_output): 124 actual = self._GetOutput(test_format) 125 actual_lines = actual.splitlines() 126 expected_lines = expected_output.splitlines() 127 line_count = 0 128 for actual_line in actual_lines: 129 expected_line = expected_lines[line_count] 130 expected_line_re = re.compile(expected_line.strip()) 131 self.assert_( 132 expected_line_re.match(actual_line.strip()), 133 ('actual output of "%s",\n' 134 'which does not match expected regex of "%s"\n' 135 'on line %d' % (actual, expected_output, line_count))) 136 line_count = line_count + 1 137 138 139if __name__ == '__main__': 140 os.environ['GTEST_STACK_TRACE_DEPTH'] = '1' 141 gtest_test_utils.Main() 142