Imported Upstream version 1.12.0
[platform/upstream/gtest.git] / googletest / test / googletest-json-outfiles-test.py
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
33 import json
34 import os
35 from googletest.test import gtest_json_test_utils
36 from googletest.test import gtest_test_utils
37
38 GTEST_OUTPUT_SUBDIR = 'json_outfiles'
39 GTEST_OUTPUT_1_TEST = 'gtest_xml_outfile1_test_'
40 GTEST_OUTPUT_2_TEST = 'gtest_xml_outfile2_test_'
41
42 EXPECTED_1 = {
43     u'tests':
44         1,
45     u'failures':
46         0,
47     u'disabled':
48         0,
49     u'errors':
50         0,
51     u'time':
52         u'*',
53     u'timestamp':
54         u'*',
55     u'name':
56         u'AllTests',
57     u'testsuites': [{
58         u'name':
59             u'PropertyOne',
60         u'tests':
61             1,
62         u'failures':
63             0,
64         u'disabled':
65             0,
66         u'errors':
67             0,
68         u'time':
69             u'*',
70         u'timestamp':
71             u'*',
72         u'testsuite': [{
73             u'name': u'TestSomeProperties',
74             u'file': u'gtest_xml_outfile1_test_.cc',
75             u'line': 41,
76             u'status': u'RUN',
77             u'result': u'COMPLETED',
78             u'time': u'*',
79             u'timestamp': u'*',
80             u'classname': u'PropertyOne',
81             u'SetUpProp': u'1',
82             u'TestSomeProperty': u'1',
83             u'TearDownProp': u'1',
84         }],
85     }],
86 }
87
88 EXPECTED_2 = {
89     u'tests':
90         1,
91     u'failures':
92         0,
93     u'disabled':
94         0,
95     u'errors':
96         0,
97     u'time':
98         u'*',
99     u'timestamp':
100         u'*',
101     u'name':
102         u'AllTests',
103     u'testsuites': [{
104         u'name':
105             u'PropertyTwo',
106         u'tests':
107             1,
108         u'failures':
109             0,
110         u'disabled':
111             0,
112         u'errors':
113             0,
114         u'time':
115             u'*',
116         u'timestamp':
117             u'*',
118         u'testsuite': [{
119             u'name': u'TestSomeProperties',
120             u'file': u'gtest_xml_outfile2_test_.cc',
121             u'line': 41,
122             u'status': u'RUN',
123             u'result': u'COMPLETED',
124             u'timestamp': u'*',
125             u'time': u'*',
126             u'classname': u'PropertyTwo',
127             u'SetUpProp': u'2',
128             u'TestSomeProperty': u'2',
129             u'TearDownProp': u'2',
130         }],
131     }],
132 }
133
134
135 class GTestJsonOutFilesTest(gtest_test_utils.TestCase):
136   """Unit test for Google Test's JSON output functionality."""
137
138   def setUp(self):
139     # We want the trailing '/' that the last "" provides in os.path.join, for
140     # telling Google Test to create an output directory instead of a single file
141     # for xml output.
142     self.output_dir_ = os.path.join(gtest_test_utils.GetTempDir(),
143                                     GTEST_OUTPUT_SUBDIR, '')
144     self.DeleteFilesAndDir()
145
146   def tearDown(self):
147     self.DeleteFilesAndDir()
148
149   def DeleteFilesAndDir(self):
150     try:
151       os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_1_TEST + '.json'))
152     except os.error:
153       pass
154     try:
155       os.remove(os.path.join(self.output_dir_, GTEST_OUTPUT_2_TEST + '.json'))
156     except os.error:
157       pass
158     try:
159       os.rmdir(self.output_dir_)
160     except os.error:
161       pass
162
163   def testOutfile1(self):
164     self._TestOutFile(GTEST_OUTPUT_1_TEST, EXPECTED_1)
165
166   def testOutfile2(self):
167     self._TestOutFile(GTEST_OUTPUT_2_TEST, EXPECTED_2)
168
169   def _TestOutFile(self, test_name, expected):
170     gtest_prog_path = gtest_test_utils.GetTestExecutablePath(test_name)
171     command = [gtest_prog_path, '--gtest_output=json:%s' % self.output_dir_]
172     p = gtest_test_utils.Subprocess(command,
173                                     working_dir=gtest_test_utils.GetTempDir())
174     self.assert_(p.exited)
175     self.assertEquals(0, p.exit_code)
176
177     output_file_name1 = test_name + '.json'
178     output_file1 = os.path.join(self.output_dir_, output_file_name1)
179     output_file_name2 = 'lt-' + output_file_name1
180     output_file2 = os.path.join(self.output_dir_, output_file_name2)
181     self.assert_(os.path.isfile(output_file1) or os.path.isfile(output_file2),
182                  output_file1)
183
184     if os.path.isfile(output_file1):
185       with open(output_file1) as f:
186         actual = json.load(f)
187     else:
188       with open(output_file2) as f:
189         actual = json.load(f)
190     self.assertEqual(expected, gtest_json_test_utils.normalize(actual))
191
192
193 if __name__ == '__main__':
194   os.environ['GTEST_STACK_TRACE_DEPTH'] = '0'
195   gtest_test_utils.Main()