summaryrefslogtreecommitdiffstats
path: root/googletest/test/gtest_list_output_unittest.py
blob: b882126e11c463cca362a16f697d832a30e7f4e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env python
#
# Copyright 2006, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""Unit test for Google Test's --gtest_list_tests flag.

A user can ask Google Test to list all tests by specifying the
--gtest_list_tests flag. If output is requested, via --gtest_output=xml
or --gtest_output=json, the tests are listed, with extra information in the
output file.
This script tests such functionality by invoking gtest_list_output_unittest_
 (a program written with Google Test) the command line flags.
"""

import os
import re
import gtest_test_utils

GTEST_LIST_TESTS_FLAG = '--gtest_list_tests'
GTEST_OUTPUT_FLAG = '--gtest_output'

EXPECTED_XML = """<\?xml version="1.0" encoding="UTF-8"\?>
<testsuites tests="16" name="AllTests">
  <testsuite name="FooTest" tests="2">
    <testcase name="Test1" file=".*gtest_list_output_unittest_.cc" line="43" />
    <testcase name="Test2" file=".*gtest_list_output_unittest_.cc" line="45" />
  </testsuite>
  <testsuite name="FooTestFixture" tests="2">
    <testcase name="Test3" file=".*gtest_list_output_unittest_.cc" line="48" />
    <testcase name="Test4" file=".*gtest_list_output_unittest_.cc" line="49" />
  </testsuite>
  <testsuite name="TypedTest/0" tests="2">
    <testcase name="Test7" type_param="int" file=".*gtest_list_output_unittest_.cc" line="61" />
    <testcase name="Test8" type_param="int" file=".*gtest_list_output_unittest_.cc" line="62" />
  </testsuite>
  <testsuite name="TypedTest/1" tests="2">
    <testcase name="Test7" type_param="bool" file=".*gtest_list_output_unittest_.cc" line="61" />
    <testcase name="Test8" type_param="bool" file=".*gtest_list_output_unittest_.cc" line="62" />
  </testsuite>
  <testsuite name="Single/TypeParameterizedTestSuite/0" tests="2">
    <testcase name="Test9" type_param="int" file=".*gtest_list_output_unittest_.cc" line="69" />
    <testcase name="Test10" type_param="int" file=".*gtest_list_output_unittest_.cc" line="70" />
  </testsuite>
  <testsuite name="Single/TypeParameterizedTestSuite/1" tests="2">
    <testcase name="Test9" type_param="bool" file=".*gtest_list_output_unittest_.cc" line="69" />
    <testcase name="Test10" type_param="bool" file=".*gtest_list_output_unittest_.cc" line="70" />
  </testsuite>
  <testsuite name="ValueParam/ValueParamTest" tests="4">
    <testcase name="Test5/0" value_param="33" file=".*gtest_list_output_unittest_.cc" line="52" />
    <testcase name="Test5/1" value_param="42" file=".*gtest_list_output_unittest_.cc" line="52" />
    <testcase name="Test6/0" value_param="33" file=".*gtest_list_output_unittest_.cc" line="53" />
    <testcase name="Test6/1" value_param="42" file=".*gtest_list_output_unittest_.cc" line="53" />
  </testsuite>
</testsuites>
"""

EXPECTED_JSON = """{
  "tests": 16,
  "name": "AllTests",
  "testsuites": \[
    {
      "name": "FooTest",
      "tests": 2,
      "testsuite": \[
        {
          "name": "Test1",
          "file": ".*gtest_list_output_unittest_.cc",
          "line": 43
        },
        {
          "name": "Test2",
          "file": ".*gtest_list_output_unittest_.cc",
          "line": 45
        }
      \]
    },
    {
      "name": "FooTestFixture",
      "tests": 2,
      "testsuite": \[
        {
          "name": "Test3",
          "file": ".*gtest_list_output_unittest_.cc",
          "line": 48
        },
        {
          "name": "Test4",
          "file": ".*gtest_list_output_unittest_.cc",
          "line": 49
        }
      \]
    },
    {
      "name": "TypedTest\\\\/0",
      "tests": 2,
      "testsuite": \[
        {
          "name": "Test7",
          "type_param": "int",
          "file": ".*gtest_list_output_unittest_.cc",
          "line": 61
        },
        {
          "name": "Test8",
          "type_param": "int",
          "file": ".*gtest_list_output_unittest_.cc",
          "line": 62
        }
      \]
    },
    {
      "name": "TypedTest\\\\/1",
      "tests": 2,
      "testsuite": \[
        {
          "name": "Test7",
          "type_param": "bool",
          "file": ".*gtest_list_output_unittest_.cc",
          "line": 61
        },
        {
          "name": "Test8",
          "type_param": "bool",
          "file": ".*gtest_list_output_unittest_.cc",
          "line": 62
        }
      \]
    },
    {
      "name": "Single\\\\/TypeParameterizedTestSuite\\\\/0",
      "tests": 2,
      "testsuite": \[
        {
          "name": "Test9",
          "type_param": "int",
          "file": ".*gtest_list_output_unittest_.cc",
          "line": 69
        },
        {
          "name": "Test10",
          "type_param": "int",
          "file": ".*gtest_list_output_unittest_.cc",
          "line": 70
        }
      \]
    },
    {
      "name": "Single\\\\/TypeParameterizedTestSuite\\\\/1",
      "tests": 2,
      "testsuite": \[
        {
          "name": "Test9",
          "type_param": "bool",
          "file": ".*gtest_list_output_unittest_.cc",
          "line": 69
        },
        {
          "name": "Test10",
          "type_param": "bool",
          "file": ".*gtest_list_output_unittest_.cc",
          "line": 70
        }
      \]
    },
    {
      "name": "ValueParam\\\\/ValueParamTest",
      "tests": 4,
      "testsuite": \[
        {
          "name": "Test5\\\\/0",
          "value_param": "33",
          "file": ".*gtest_list_output_unittest_.cc",
          "line": 52
        },
        {
          "name": "Test5\\\\/1",
          "value_param": "42",
          "file": ".*gtest_list_output_unittest_.cc",
          "line": 52
        },
        {
          "name": "Test6\\\\/0",
          "value_param": "33",
          "file": ".*gtest_list_output_unittest_.cc",
          "line": 53
        },
        {
          "name": "Test6\\\\/1",
          "value_param": "42",
          "file": ".*gtest_list_output_unittest_.cc",
          "line": 53
        }
      \]
    }
  \]
}
"""


class GTestListTestsOutputUnitTest(gtest_test_utils.TestCase):
  """Unit test for Google Test's list tests with output to file functionality.
  """

  def testXml(self):
    """Verifies XML output for listing tests in a Google Test binary.

    Runs a test program that generates an empty XML output, and
    tests that the XML output is expected.
    """
    self._TestOutput('xml', EXPECTED_XML)

  def testJSON(self):
    """Verifies XML output for listing tests in a Google Test binary.

    Runs a test program that generates an empty XML output, and
    tests that the XML output is expected.
    """
    self._TestOutput('json', EXPECTED_JSON)

  def _GetOutput(self, out_format):
    file_path = os.path.join(gtest_test_utils.GetTempDir(),
                             'test_out.' + out_format)
    gtest_prog_path = gtest_test_utils.GetTestExecutablePath(
        'gtest_list_output_unittest_')

    command = ([
        gtest_prog_path,
        '%s=%s:%s' % (GTEST_OUTPUT_FLAG, out_format, file_path),
        '--gtest_list_tests'
    ])
    environ_copy = os.environ.copy()
    p = gtest_test_utils.Subprocess(
        command, env=environ_copy, working_dir=gtest_test_utils.GetTempDir())

    self.assertTrue(p.exited)
    self.assertEqual(0, p.exit_code)
    self.assertTrue(os.path.isfile(file_path))
    with open(file_path) as f:
      result = f.read()
    return result

  def _TestOutput(self, test_format, expected_output):
    actual = self._GetOutput(test_format)
    actual_lines = actual.splitlines()
    expected_lines = expected_output.splitlines()
    line_count = 0
    for actual_line in actual_lines:
      expected_line = expected_lines[line_count]
      expected_line_re = re.compile(expected_line.strip())
      self.assertTrue(
          expected_line_re.match(actual_line.strip()),
          ('actual output of "%s",\n'
           'which does not match expected regex of "%s"\n'
           'on line %d' % (actual, expected_output, line_count)))
      line_count = line_count + 1


if __name__ == '__main__':
  os.environ['GTEST_STACK_TRACE_DEPTH'] = '1'
  gtest_test_utils.Main()