2 # Copyright 2018, Google Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
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
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.
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.
31 """Unit test for the gtest_json_output module."""
35 import gtest_json_test_utils
36 import gtest_test_utils
38 GTEST_OUTPUT_SUBDIR = 'json_outfiles'
39 GTEST_OUTPUT_1_TEST = 'gtest_xml_outfile1_test_'
40 GTEST_OUTPUT_2_TEST = 'gtest_xml_outfile2_test_'
73 u'name': u'TestSomeProperties',
75 u'result': u'COMPLETED',
78 u'classname': u'PropertyOne',
80 u'TestSomeProperty': u'1',
81 u'TearDownProp': u'1',
117 u'name': u'TestSomeProperties',
119 u'result': u'COMPLETED',
122 u'classname': u'PropertyTwo',
124 u'TestSomeProperty': u'2',
125 u'TearDownProp': u'2',
131 class GTestJsonOutFilesTest(gtest_test_utils.TestCase):
132 """Unit test for Google Test's JSON output functionality."""
135 # We want the trailing '/' that the last "" provides in os.path.join, for
136 # telling Google Test to create an output directory instead of a single file
138 self.output_dir_ = os.path.join(gtest_test_utils.GetTempDir(),
139 GTEST_OUTPUT_SUBDIR, '')
140 self.DeleteFilesAndDir()
143 self.DeleteFilesAndDir()
145 def DeleteFilesAndDir(self):
147 os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_1_TEST + '.json'))
151 os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_2_TEST + '.json'))
155 os.rmdir(self.output_dir_)
159 def testOutfile1(self):
160 self._TestOutFile(GTEST_OUTPUT_1_TEST, EXPECTED_1)
162 def testOutfile2(self):
163 self._TestOutFile(GTEST_OUTPUT_2_TEST, EXPECTED_2)
165 def _TestOutFile(self, test_name, expected):
166 gtest_prog_path = gtest_test_utils.GetTestExecutablePath(test_name)
167 command = [gtest_prog_path, '--gtest_output=json:%s' % self.output_dir_]
168 p = gtest_test_utils.Subprocess(command,
169 working_dir=gtest_test_utils.GetTempDir())
170 self.assert_(p.exited)
171 self.assertEquals(0, p.exit_code)
173 output_file_name1 = test_name + '.json'
174 output_file1 = os.path.join(self.output_dir_, output_file_name1)
175 output_file_name2 = 'lt-' + output_file_name1
176 output_file2 = os.path.join(self.output_dir_, output_file_name2)
177 self.assert_(os.path.isfile(output_file1) or os.path.isfile(output_file2),
180 if os.path.isfile(output_file1):
181 with open(output_file1) as f:
182 actual = json.load(f)
184 with open(output_file2) as f:
185 actual = json.load(f)
186 self.assertEqual(expected, gtest_json_test_utils.normalize(actual))
189 if __name__ == '__main__':
190 os.environ['GTEST_STACK_TRACE_DEPTH'] = '0'
191 gtest_test_utils.Main()