1#!/usr/bin/env python 2# Copyright 2018, Google Inc. 3# All rights reserved. 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions are 7# met: 8# 9# * Redistributions of source code must retain the above copyright 10# notice, this list of conditions and the following disclaimer. 11# * Redistributions in binary form must reproduce the above 12# copyright notice, this list of conditions and the following disclaimer 13# in the documentation and/or other materials provided with the 14# distribution. 15# * Neither the name of Google Inc. nor the names of its 16# contributors may be used to endorse or promote products derived from 17# this software without specific prior written permission. 18# 19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31"""Unit test for the gtest_json_output module.""" 32 33import json 34import os 35from googletest.test import gtest_json_test_utils 36from googletest.test import gtest_test_utils 37 38GTEST_OUTPUT_SUBDIR = 'json_outfiles' 39GTEST_OUTPUT_1_TEST = 'gtest_xml_outfile1_test_' 40GTEST_OUTPUT_2_TEST = 'gtest_xml_outfile2_test_' 41 42EXPECTED_1 = { 43 'tests': 1, 44 'failures': 0, 45 'disabled': 0, 46 'errors': 0, 47 'time': '*', 48 'timestamp': '*', 49 'name': 'AllTests', 50 'testsuites': [{ 51 'name': 'PropertyOne', 52 'tests': 1, 53 'failures': 0, 54 'disabled': 0, 55 'errors': 0, 56 'time': '*', 57 'timestamp': '*', 58 'testsuite': [{ 59 'name': 'TestSomeProperties', 60 'file': 'gtest_xml_outfile1_test_.cc', 61 'line': 41, 62 'status': 'RUN', 63 'result': 'COMPLETED', 64 'time': '*', 65 'timestamp': '*', 66 'classname': 'PropertyOne', 67 'SetUpProp': '1', 68 'TestSomeProperty': '1', 69 'TearDownProp': '1', 70 }], 71 }], 72} 73 74EXPECTED_2 = { 75 'tests': 1, 76 'failures': 0, 77 'disabled': 0, 78 'errors': 0, 79 'time': '*', 80 'timestamp': '*', 81 'name': 'AllTests', 82 'testsuites': [{ 83 'name': 'PropertyTwo', 84 'tests': 1, 85 'failures': 0, 86 'disabled': 0, 87 'errors': 0, 88 'time': '*', 89 'timestamp': '*', 90 'testsuite': [{ 91 'name': 'TestInt64ConvertibleProperties', 92 'file': 'gtest_xml_outfile2_test_.cc', 93 'line': 43, 94 'status': 'RUN', 95 'result': 'COMPLETED', 96 'timestamp': '*', 97 'time': '*', 98 'classname': 'PropertyTwo', 99 'SetUpProp': '2', 100 'TestFloatProperty': '3.25', 101 'TestDoubleProperty': '4.75', 102 'TestSizetProperty': '5', 103 'TestBoolProperty': 'true', 104 'TestCharProperty': 'A', 105 'TestInt16Property': '6', 106 'TestInt32Property': '7', 107 'TestInt64Property': '8', 108 'TestEnumProperty': '9', 109 'TestAtomicIntProperty': '10', 110 'TearDownProp': '2', 111 }], 112 }], 113} 114 115 116class GTestJsonOutFilesTest(gtest_test_utils.TestCase): 117 """Unit test for Google Test's JSON output functionality.""" 118 119 def setUp(self): 120 # We want the trailing '/' that the last "" provides in os.path.join, for 121 # telling Google Test to create an output directory instead of a single file 122 # for xml output. 123 self.output_dir_ = os.path.join( 124 gtest_test_utils.GetTempDir(), GTEST_OUTPUT_SUBDIR, '' 125 ) 126 self.DeleteFilesAndDir() 127 128 def tearDown(self): 129 self.DeleteFilesAndDir() 130 131 def DeleteFilesAndDir(self): 132 try: 133 os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_1_TEST + '.json')) 134 except os.error: 135 pass 136 try: 137 os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_2_TEST + '.json')) 138 except os.error: 139 pass 140 try: 141 os.rmdir(self.output_dir_) 142 except os.error: 143 pass 144 145 def testOutfile1(self): 146 self._TestOutFile(GTEST_OUTPUT_1_TEST, EXPECTED_1) 147 148 def testOutfile2(self): 149 self._TestOutFile(GTEST_OUTPUT_2_TEST, EXPECTED_2) 150 151 def _TestOutFile(self, test_name, expected): 152 gtest_prog_path = gtest_test_utils.GetTestExecutablePath(test_name) 153 command = [gtest_prog_path, '--gtest_output=json:%s' % self.output_dir_] 154 p = gtest_test_utils.Subprocess( 155 command, working_dir=gtest_test_utils.GetTempDir() 156 ) 157 self.assertTrue(p.exited) 158 self.assertEqual(0, p.exit_code) 159 160 output_file_name1 = test_name + '.json' 161 output_file1 = os.path.join(self.output_dir_, output_file_name1) 162 output_file_name2 = 'lt-' + output_file_name1 163 output_file2 = os.path.join(self.output_dir_, output_file_name2) 164 self.assertTrue( 165 os.path.isfile(output_file1) or os.path.isfile(output_file2), 166 output_file1, 167 ) 168 169 if os.path.isfile(output_file1): 170 with open(output_file1) as f: 171 actual = json.load(f) 172 else: 173 with open(output_file2) as f: 174 actual = json.load(f) 175 self.assertEqual(expected, gtest_json_test_utils.normalize(actual)) 176 177 178if __name__ == '__main__': 179 os.environ['GTEST_STACK_TRACE_DEPTH'] = '0' 180 gtest_test_utils.Main() 181