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_unknown(self): 153 raw_output = [ 154 '[ RUN ] FooTest.Bar', 155 ] 156 actual = gtest_test_instance.ParseGTestOutput(raw_output, None, None) 157 self.assertEqual(1, len(actual)) 158 self.assertEqual('FooTest.Bar', actual[0].GetName()) 159 self.assertEqual(0, actual[0].GetDuration()) 160 self.assertEqual(base_test_result.ResultType.CRASH, actual[0].GetType()) 161 162 def testParseGTestOutput_nonterminalUnknown(self): 163 raw_output = [ 164 '[ RUN ] FooTest.Bar', 165 '[ RUN ] FooTest.Baz', 166 '[ OK ] FooTest.Baz (1 ms)', 167 ] 168 actual = gtest_test_instance.ParseGTestOutput(raw_output, None, None) 169 self.assertEqual(2, len(actual)) 170 171 self.assertEqual('FooTest.Bar', actual[0].GetName()) 172 self.assertEqual(0, actual[0].GetDuration()) 173 self.assertEqual(base_test_result.ResultType.CRASH, actual[0].GetType()) 174 175 self.assertEqual('FooTest.Baz', actual[1].GetName()) 176 self.assertEqual(1, actual[1].GetDuration()) 177 self.assertEqual(base_test_result.ResultType.PASS, actual[1].GetType()) 178 179 def testParseGTestOutput_deathTestCrashOk(self): 180 raw_output = [ 181 '[ RUN ] FooTest.Bar', 182 '[ CRASHED ]', 183 '[ OK ] FooTest.Bar (1 ms)', 184 ] 185 actual = gtest_test_instance.ParseGTestOutput(raw_output, None, None) 186 self.assertEqual(1, len(actual)) 187 188 self.assertEqual('FooTest.Bar', actual[0].GetName()) 189 self.assertEqual(1, actual[0].GetDuration()) 190 self.assertEqual(base_test_result.ResultType.PASS, actual[0].GetType()) 191 192 def testParseGTestOutput_typeParameterized(self): 193 raw_output = [ 194 '[ RUN ] Baz/FooTest.Bar/0', 195 '[ FAILED ] Baz/FooTest.Bar/0, where TypeParam = (1 ms)', 196 ] 197 actual = gtest_test_instance.ParseGTestOutput(raw_output, None, None) 198 self.assertEqual(1, len(actual)) 199 self.assertEqual('Baz/FooTest.Bar/0', actual[0].GetName()) 200 self.assertEqual(1, actual[0].GetDuration()) 201 self.assertEqual(base_test_result.ResultType.FAIL, actual[0].GetType()) 202 203 def testParseGTestOutput_valueParameterized(self): 204 raw_output = [ 205 '[ RUN ] Baz/FooTest.Bar/0', 206 '[ FAILED ] Baz/FooTest.Bar/0,' + 207 ' where GetParam() = 4-byte object <00-00 00-00> (1 ms)', 208 ] 209 actual = gtest_test_instance.ParseGTestOutput(raw_output, None, None) 210 self.assertEqual(1, len(actual)) 211 self.assertEqual('Baz/FooTest.Bar/0', actual[0].GetName()) 212 self.assertEqual(1, actual[0].GetDuration()) 213 self.assertEqual(base_test_result.ResultType.FAIL, actual[0].GetType()) 214 215 def testParseGTestOutput_typeAndValueParameterized(self): 216 raw_output = [ 217 '[ RUN ] Baz/FooTest.Bar/0', 218 '[ FAILED ] Baz/FooTest.Bar/0,' + 219 ' where TypeParam = and GetParam() = (1 ms)', 220 ] 221 actual = gtest_test_instance.ParseGTestOutput(raw_output, None, None) 222 self.assertEqual(1, len(actual)) 223 self.assertEqual('Baz/FooTest.Bar/0', actual[0].GetName()) 224 self.assertEqual(1, actual[0].GetDuration()) 225 self.assertEqual(base_test_result.ResultType.FAIL, actual[0].GetType()) 226 227 def testParseGTestOutput_skippedTest(self): 228 raw_output = [ 229 '[ RUN ] FooTest.Bar', 230 '[ SKIPPED ] FooTest.Bar (1 ms)', 231 ] 232 actual = gtest_test_instance.ParseGTestOutput(raw_output, None, None) 233 self.assertEqual(1, len(actual)) 234 self.assertEqual('FooTest.Bar', actual[0].GetName()) 235 self.assertEqual(1, actual[0].GetDuration()) 236 self.assertEqual(base_test_result.ResultType.SKIP, actual[0].GetType()) 237 238 def testParseGTestXML_none(self): 239 actual = gtest_test_instance.ParseGTestXML(None) 240 self.assertEqual([], actual) 241 242 def testParseGTestJSON_none(self): 243 actual = gtest_test_instance.ParseGTestJSON(None) 244 self.assertEqual([], actual) 245 246 def testParseGTestJSON_example(self): 247 raw_json = """ 248 { 249 "tests": { 250 "mojom_tests": { 251 "parse": { 252 "ast_unittest": { 253 "ASTTest": { 254 "testNodeBase": { 255 "expected": "PASS", 256 "actual": "PASS", 257 "artifacts": { 258 "screenshot": ["screenshots/page.png"] 259 } 260 } 261 } 262 } 263 } 264 } 265 }, 266 "interrupted": false, 267 "path_delimiter": ".", 268 "version": 3, 269 "seconds_since_epoch": 1406662283.764424, 270 "num_failures_by_type": { 271 "FAIL": 0, 272 "PASS": 1 273 }, 274 "artifact_types": { 275 "screenshot": "image/png" 276 } 277 }""" 278 actual = gtest_test_instance.ParseGTestJSON(raw_json) 279 self.assertEqual(1, len(actual)) 280 self.assertEqual('mojom_tests.parse.ast_unittest.ASTTest.testNodeBase', 281 actual[0].GetName()) 282 self.assertEqual(base_test_result.ResultType.PASS, actual[0].GetType()) 283 284 def testParseGTestJSON_skippedTest_example(self): 285 raw_json = """ 286 { 287 "tests": { 288 "mojom_tests": { 289 "parse": { 290 "ast_unittest": { 291 "ASTTest": { 292 "testNodeBase": { 293 "expected": "SKIP", 294 "actual": "SKIP" 295 } 296 } 297 } 298 } 299 } 300 }, 301 "interrupted": false, 302 "path_delimiter": ".", 303 "version": 3, 304 "seconds_since_epoch": 1406662283.764424, 305 "num_failures_by_type": { 306 "SKIP": 1 307 } 308 }""" 309 actual = gtest_test_instance.ParseGTestJSON(raw_json) 310 self.assertEqual(1, len(actual)) 311 self.assertEqual('mojom_tests.parse.ast_unittest.ASTTest.testNodeBase', 312 actual[0].GetName()) 313 self.assertEqual(base_test_result.ResultType.SKIP, actual[0].GetType()) 314 315 def testTestNameWithoutDisabledPrefix_disabled(self): 316 test_name_list = [ 317 'A.DISABLED_B', 318 'DISABLED_A.B', 319 'DISABLED_A.DISABLED_B', 320 ] 321 for test_name in test_name_list: 322 actual = gtest_test_instance \ 323 .TestNameWithoutDisabledPrefix(test_name) 324 expected = 'A.B' 325 self.assertEqual(expected, actual) 326 327 def testTestNameWithoutDisabledPrefix_flaky(self): 328 test_name_list = [ 329 'A.FLAKY_B', 330 'FLAKY_A.B', 331 'FLAKY_A.FLAKY_B', 332 ] 333 for test_name in test_name_list: 334 actual = gtest_test_instance \ 335 .TestNameWithoutDisabledPrefix(test_name) 336 expected = 'A.B' 337 self.assertEqual(expected, actual) 338 339 def testTestNameWithoutDisabledPrefix_notDisabledOrFlaky(self): 340 test_name = 'A.B' 341 actual = gtest_test_instance \ 342 .TestNameWithoutDisabledPrefix(test_name) 343 expected = 'A.B' 344 self.assertEqual(expected, actual) 345 346 347if __name__ == '__main__': 348 unittest.main(verbosity=2) 349