• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env vpython3
2# Copyright 2014 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6
7import unittest
8
9from pylib.base import base_test_result
10from pylib.gtest import gtest_test_instance
11
12
13class GtestTestInstanceTests(unittest.TestCase):
14
15  def testParseGTestListTests_simple(self):
16    raw_output = [
17      'TestCaseOne.',
18      '  testOne',
19      '  testTwo',
20      'TestCaseTwo.',
21      '  testThree',
22      '  testFour',
23    ]
24    actual = gtest_test_instance.ParseGTestListTests(raw_output)
25    expected = [
26      'TestCaseOne.testOne',
27      'TestCaseOne.testTwo',
28      'TestCaseTwo.testThree',
29      'TestCaseTwo.testFour',
30    ]
31    self.assertEqual(expected, actual)
32
33  def testParseGTestListTests_typeParameterized_old(self):
34    raw_output = [
35      'TPTestCase/WithTypeParam/0.',
36      '  testOne',
37      '  testTwo',
38    ]
39    actual = gtest_test_instance.ParseGTestListTests(raw_output)
40    expected = [
41      'TPTestCase/WithTypeParam/0.testOne',
42      'TPTestCase/WithTypeParam/0.testTwo',
43    ]
44    self.assertEqual(expected, actual)
45
46  def testParseGTestListTests_typeParameterized_new(self):
47    raw_output = [
48      'TPTestCase/WithTypeParam/0.  # TypeParam = TypeParam0',
49      '  testOne',
50      '  testTwo',
51    ]
52    actual = gtest_test_instance.ParseGTestListTests(raw_output)
53    expected = [
54      'TPTestCase/WithTypeParam/0.testOne',
55      'TPTestCase/WithTypeParam/0.testTwo',
56    ]
57    self.assertEqual(expected, actual)
58
59  def testParseGTestListTests_valueParameterized_old(self):
60    raw_output = [
61      'VPTestCase.',
62      '  testWithValueParam/0',
63      '  testWithValueParam/1',
64    ]
65    actual = gtest_test_instance.ParseGTestListTests(raw_output)
66    expected = [
67      'VPTestCase.testWithValueParam/0',
68      'VPTestCase.testWithValueParam/1',
69    ]
70    self.assertEqual(expected, actual)
71
72  def testParseGTestListTests_valueParameterized_new(self):
73    raw_output = [
74      'VPTestCase.',
75      '  testWithValueParam/0  # GetParam() = 0',
76      '  testWithValueParam/1  # GetParam() = 1',
77    ]
78    actual = gtest_test_instance.ParseGTestListTests(raw_output)
79    expected = [
80      'VPTestCase.testWithValueParam/0',
81      'VPTestCase.testWithValueParam/1',
82    ]
83    self.assertEqual(expected, actual)
84
85  def testParseGTestListTests_emptyTestName(self):
86    raw_output = [
87      'TestCase.',
88      '  ',
89      '  nonEmptyTestName',
90    ]
91    actual = gtest_test_instance.ParseGTestListTests(raw_output)
92    expected = [
93      'TestCase.nonEmptyTestName',
94    ]
95    self.assertEqual(expected, actual)
96
97  def testParseGTestOutput_pass(self):
98    raw_output = [
99      '[ RUN      ] FooTest.Bar',
100      '[       OK ] FooTest.Bar (1 ms)',
101    ]
102    actual = gtest_test_instance.ParseGTestOutput(raw_output, None, None)
103    self.assertEqual(1, len(actual))
104    self.assertEqual('FooTest.Bar', actual[0].GetName())
105    self.assertEqual(1, actual[0].GetDuration())
106    self.assertEqual(base_test_result.ResultType.PASS, actual[0].GetType())
107
108  def testParseGTestOutput_fail(self):
109    raw_output = [
110      '[ RUN      ] FooTest.Bar',
111      '[   FAILED ] FooTest.Bar (1 ms)',
112    ]
113    actual = gtest_test_instance.ParseGTestOutput(raw_output, None, None)
114    self.assertEqual(1, len(actual))
115    self.assertEqual('FooTest.Bar', actual[0].GetName())
116    self.assertEqual(1, actual[0].GetDuration())
117    self.assertEqual(base_test_result.ResultType.FAIL, actual[0].GetType())
118
119  def testParseGTestOutput_crash(self):
120    raw_output = [
121      '[ RUN      ] FooTest.Bar',
122      '[  CRASHED ] FooTest.Bar (1 ms)',
123    ]
124    actual = gtest_test_instance.ParseGTestOutput(raw_output, None, None)
125    self.assertEqual(1, len(actual))
126    self.assertEqual('FooTest.Bar', actual[0].GetName())
127    self.assertEqual(1, actual[0].GetDuration())
128    self.assertEqual(base_test_result.ResultType.CRASH, actual[0].GetType())
129
130  def testParseGTestOutput_errorCrash(self):
131    raw_output = [
132      '[ RUN      ] FooTest.Bar',
133      '[ERROR:blah] Currently running: FooTest.Bar',
134    ]
135    actual = gtest_test_instance.ParseGTestOutput(raw_output, None, None)
136    self.assertEqual(1, len(actual))
137    self.assertEqual('FooTest.Bar', actual[0].GetName())
138    self.assertIsNone(actual[0].GetDuration())
139    self.assertEqual(base_test_result.ResultType.CRASH, actual[0].GetType())
140
141  def testParseGTestOutput_fatalDcheck(self):
142    raw_output = [
143        '[ RUN      ] FooTest.Bar',
144        '[0324/183029.116334:FATAL:test_timeouts.cc(103)] Check failed: !init',
145    ]
146    actual = gtest_test_instance.ParseGTestOutput(raw_output, None, None)
147    self.assertEqual(1, len(actual))
148    self.assertEqual('FooTest.Bar', actual[0].GetName())
149    self.assertIsNone(actual[0].GetDuration())
150    self.assertEqual(base_test_result.ResultType.CRASH, actual[0].GetType())
151
152  def testParseGTestOutput_crashWithoutCheckOrErrorMessage(self):
153    """Regression test for https://crbug.com/355630342."""
154    raw_output = [
155        '>>ScopedMainEntryLogger',
156        '[ RUN      ] FooTest.WillCrashSuddenly',
157        'BrowserTestBase received signal: Segmentation fault. Backtrace:',
158        '<fake backtrace line>',
159        '>>ScopedMainEntryLogger',
160        'Note: Google Test filter = FooTest.DoNotConsume',
161        '[ RUN      ] FooTest.DoNotConsume',
162        '[       OK ] FooTest.DoNotConsume (1 ms)',
163        '<<ScopedMainEntryLogger',
164    ]
165    actual = gtest_test_instance.ParseGTestOutput(raw_output, None, None)
166    self.assertEqual(2, len(actual))
167
168    self.assertEqual('FooTest.WillCrashSuddenly', actual[0].GetName())
169    self.assertIsNone(actual[0].GetDuration())
170    self.assertEqual(base_test_result.ResultType.CRASH, actual[0].GetType())
171    self.assertEqual([
172        '[ RUN      ] FooTest.WillCrashSuddenly',
173        'BrowserTestBase received signal: Segmentation fault. Backtrace:',
174        '<fake backtrace line>',
175    ], actual[0].GetLog().splitlines())
176
177    self.assertEqual('FooTest.DoNotConsume', actual[1].GetName())
178    self.assertEqual(1, actual[1].GetDuration())
179    self.assertEqual(base_test_result.ResultType.PASS, actual[1].GetType())
180    self.assertEqual([
181        '[ RUN      ] FooTest.DoNotConsume',
182        '[       OK ] FooTest.DoNotConsume (1 ms)',
183    ], actual[1].GetLog().splitlines())
184
185  def testParseGTestOutput_unknown(self):
186    raw_output = [
187      '[ RUN      ] FooTest.Bar',
188    ]
189    actual = gtest_test_instance.ParseGTestOutput(raw_output, None, None)
190    self.assertEqual(1, len(actual))
191    self.assertEqual('FooTest.Bar', actual[0].GetName())
192    self.assertEqual(0, actual[0].GetDuration())
193    self.assertEqual(base_test_result.ResultType.CRASH, actual[0].GetType())
194
195  def testParseGTestOutput_nonterminalUnknown(self):
196    raw_output = [
197      '[ RUN      ] FooTest.Bar',
198      '[ RUN      ] FooTest.Baz',
199      '[       OK ] FooTest.Baz (1 ms)',
200    ]
201    actual = gtest_test_instance.ParseGTestOutput(raw_output, None, None)
202    self.assertEqual(2, len(actual))
203
204    self.assertEqual('FooTest.Bar', actual[0].GetName())
205    self.assertEqual(0, actual[0].GetDuration())
206    self.assertEqual(base_test_result.ResultType.CRASH, actual[0].GetType())
207
208    self.assertEqual('FooTest.Baz', actual[1].GetName())
209    self.assertEqual(1, actual[1].GetDuration())
210    self.assertEqual(base_test_result.ResultType.PASS, actual[1].GetType())
211
212  def testParseGTestOutput_deathTestCrashOk(self):
213    raw_output = [
214      '[ RUN      ] FooTest.Bar',
215      '[ CRASHED      ]',
216      '[       OK ] FooTest.Bar (1 ms)',
217    ]
218    actual = gtest_test_instance.ParseGTestOutput(raw_output, None, None)
219    self.assertEqual(1, len(actual))
220
221    self.assertEqual('FooTest.Bar', actual[0].GetName())
222    self.assertEqual(1, actual[0].GetDuration())
223    self.assertEqual(base_test_result.ResultType.PASS, actual[0].GetType())
224
225  def testParseGTestOutput_typeParameterized(self):
226    raw_output = [
227        '[ RUN      ] Baz/FooTest.Bar/0',
228        '[   FAILED ] Baz/FooTest.Bar/0, where TypeParam =  (1 ms)',
229    ]
230    actual = gtest_test_instance.ParseGTestOutput(raw_output, None, None)
231    self.assertEqual(1, len(actual))
232    self.assertEqual('Baz/FooTest.Bar/0', actual[0].GetName())
233    self.assertEqual(1, actual[0].GetDuration())
234    self.assertEqual(base_test_result.ResultType.FAIL, actual[0].GetType())
235
236  def testParseGTestOutput_valueParameterized(self):
237    raw_output = [
238        '[ RUN      ] Baz/FooTest.Bar/0',
239        '[   FAILED ] Baz/FooTest.Bar/0,' +
240        ' where GetParam() = 4-byte object <00-00 00-00> (1 ms)',
241    ]
242    actual = gtest_test_instance.ParseGTestOutput(raw_output, None, None)
243    self.assertEqual(1, len(actual))
244    self.assertEqual('Baz/FooTest.Bar/0', actual[0].GetName())
245    self.assertEqual(1, actual[0].GetDuration())
246    self.assertEqual(base_test_result.ResultType.FAIL, actual[0].GetType())
247
248  def testParseGTestOutput_typeAndValueParameterized(self):
249    raw_output = [
250        '[ RUN      ] Baz/FooTest.Bar/0',
251        '[   FAILED ] Baz/FooTest.Bar/0,' +
252        ' where TypeParam =  and GetParam() =  (1 ms)',
253    ]
254    actual = gtest_test_instance.ParseGTestOutput(raw_output, None, None)
255    self.assertEqual(1, len(actual))
256    self.assertEqual('Baz/FooTest.Bar/0', actual[0].GetName())
257    self.assertEqual(1, actual[0].GetDuration())
258    self.assertEqual(base_test_result.ResultType.FAIL, actual[0].GetType())
259
260  def testParseGTestOutput_skippedTest(self):
261    raw_output = [
262        '[ RUN      ] FooTest.Bar',
263        '[  SKIPPED ] FooTest.Bar (1 ms)',
264    ]
265    actual = gtest_test_instance.ParseGTestOutput(raw_output, None, None)
266    self.assertEqual(1, len(actual))
267    self.assertEqual('FooTest.Bar', actual[0].GetName())
268    self.assertEqual(1, actual[0].GetDuration())
269    self.assertEqual(base_test_result.ResultType.SKIP, actual[0].GetType())
270
271  def testParseGTestXML_none(self):
272    actual = gtest_test_instance.ParseGTestXML(None)
273    self.assertEqual([], actual)
274
275  def testParseGTestJSON_none(self):
276    actual = gtest_test_instance.ParseGTestJSON(None)
277    self.assertEqual([], actual)
278
279  def testParseGTestJSON_example(self):
280    raw_json = """
281      {
282        "tests": {
283          "mojom_tests": {
284            "parse": {
285              "ast_unittest": {
286                "ASTTest": {
287                  "testNodeBase": {
288                    "expected": "PASS",
289                    "actual": "PASS",
290                    "artifacts": {
291                      "screenshot": ["screenshots/page.png"]
292                    }
293                  }
294                }
295              }
296            }
297          }
298        },
299        "interrupted": false,
300        "path_delimiter": ".",
301        "version": 3,
302        "seconds_since_epoch": 1406662283.764424,
303        "num_failures_by_type": {
304          "FAIL": 0,
305          "PASS": 1
306        },
307        "artifact_types": {
308          "screenshot": "image/png"
309        }
310      }"""
311    actual = gtest_test_instance.ParseGTestJSON(raw_json)
312    self.assertEqual(1, len(actual))
313    self.assertEqual('mojom_tests.parse.ast_unittest.ASTTest.testNodeBase',
314                     actual[0].GetName())
315    self.assertEqual(base_test_result.ResultType.PASS, actual[0].GetType())
316
317  def testParseGTestJSON_skippedTest_example(self):
318    raw_json = """
319      {
320        "tests": {
321          "mojom_tests": {
322            "parse": {
323              "ast_unittest": {
324                "ASTTest": {
325                  "testNodeBase": {
326                    "expected": "SKIP",
327                    "actual": "SKIP"
328                  }
329                }
330              }
331            }
332          }
333        },
334        "interrupted": false,
335        "path_delimiter": ".",
336        "version": 3,
337        "seconds_since_epoch": 1406662283.764424,
338        "num_failures_by_type": {
339          "SKIP": 1
340        }
341      }"""
342    actual = gtest_test_instance.ParseGTestJSON(raw_json)
343    self.assertEqual(1, len(actual))
344    self.assertEqual('mojom_tests.parse.ast_unittest.ASTTest.testNodeBase',
345                     actual[0].GetName())
346    self.assertEqual(base_test_result.ResultType.SKIP, actual[0].GetType())
347
348  def testTestNameWithoutDisabledPrefix_disabled(self):
349    test_name_list = [
350      'A.DISABLED_B',
351      'DISABLED_A.B',
352      'DISABLED_A.DISABLED_B',
353    ]
354    for test_name in test_name_list:
355      actual = gtest_test_instance \
356          .TestNameWithoutDisabledPrefix(test_name)
357      expected = 'A.B'
358      self.assertEqual(expected, actual)
359
360  def testTestNameWithoutDisabledPrefix_flaky(self):
361    test_name_list = [
362      'A.FLAKY_B',
363      'FLAKY_A.B',
364      'FLAKY_A.FLAKY_B',
365    ]
366    for test_name in test_name_list:
367      actual = gtest_test_instance \
368          .TestNameWithoutDisabledPrefix(test_name)
369      expected = 'A.B'
370      self.assertEqual(expected, actual)
371
372  def testTestNameWithoutDisabledPrefix_notDisabledOrFlaky(self):
373    test_name = 'A.B'
374    actual = gtest_test_instance \
375        .TestNameWithoutDisabledPrefix(test_name)
376    expected = 'A.B'
377    self.assertEqual(expected, actual)
378
379
380if __name__ == '__main__':
381  unittest.main(verbosity=2)
382