OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 |
| 7 """Unit tests for instrumentation.TestRunner.""" |
| 8 |
| 9 # pylint: disable=W0212 |
| 10 |
| 11 import os |
| 12 import sys |
| 13 import unittest |
| 14 |
| 15 from pylib import constants |
| 16 from pylib.base import base_test_result |
| 17 from pylib.instrumentation import test_runner |
| 18 |
| 19 sys.path.append(os.path.join( |
| 20 constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) |
| 21 import mock # pylint: disable=F0401 |
| 22 |
| 23 |
| 24 class InstrumentationTestRunnerTest(unittest.TestCase): |
| 25 |
| 26 def setUp(self): |
| 27 options = mock.Mock() |
| 28 options.tool = '' |
| 29 package = mock.Mock() |
| 30 self.instance = test_runner.TestRunner(options, None, 0, package) |
| 31 |
| 32 def testParseAmInstrumentRawOutput_nothing(self): |
| 33 code, result, statuses = ( |
| 34 test_runner.TestRunner._ParseAmInstrumentRawOutput([''])) |
| 35 self.assertEqual(None, code) |
| 36 self.assertEqual([], result) |
| 37 self.assertEqual([], statuses) |
| 38 |
| 39 def testParseAmInstrumentRawOutput_noMatchingStarts(self): |
| 40 raw_output = [ |
| 41 '', |
| 42 'this.is.a.test.package.TestClass:.', |
| 43 'Test result for =.', |
| 44 'Time: 1.234', |
| 45 '', |
| 46 'OK (1 test)', |
| 47 ] |
| 48 |
| 49 code, result, statuses = ( |
| 50 test_runner.TestRunner._ParseAmInstrumentRawOutput(raw_output)) |
| 51 self.assertEqual(None, code) |
| 52 self.assertEqual([], result) |
| 53 self.assertEqual([], statuses) |
| 54 |
| 55 def testParseAmInstrumentRawOutput_resultAndCode(self): |
| 56 raw_output = [ |
| 57 'INSTRUMENTATION_RESULT: foo', |
| 58 'bar', |
| 59 'INSTRUMENTATION_CODE: -1', |
| 60 ] |
| 61 |
| 62 code, result, _ = ( |
| 63 test_runner.TestRunner._ParseAmInstrumentRawOutput(raw_output)) |
| 64 self.assertEqual(-1, code) |
| 65 self.assertEqual(['foo', 'bar'], result) |
| 66 |
| 67 def testParseAmInstrumentRawOutput_oneStatus(self): |
| 68 raw_output = [ |
| 69 'INSTRUMENTATION_STATUS: foo=1', |
| 70 'INSTRUMENTATION_STATUS: bar=hello', |
| 71 'INSTRUMENTATION_STATUS: world=false', |
| 72 'INSTRUMENTATION_STATUS: class=this.is.a.test.package.TestClass', |
| 73 'INSTRUMENTATION_STATUS: test=testMethod', |
| 74 'INSTRUMENTATION_STATUS_CODE: 0', |
| 75 ] |
| 76 |
| 77 _, _, statuses = ( |
| 78 test_runner.TestRunner._ParseAmInstrumentRawOutput(raw_output)) |
| 79 |
| 80 expected = [ |
| 81 (0, { |
| 82 'foo': '1', |
| 83 'bar': 'hello', |
| 84 'world': 'false', |
| 85 'class': 'this.is.a.test.package.TestClass', |
| 86 'test': 'testMethod', |
| 87 }) |
| 88 ] |
| 89 self.assertEqual(expected, statuses) |
| 90 |
| 91 def testParseAmInstrumentRawOutput_multiStatus(self): |
| 92 raw_output = [ |
| 93 'INSTRUMENTATION_STATUS: class=foo', |
| 94 'INSTRUMENTATION_STATUS: test=bar', |
| 95 'INSTRUMENTATION_STATUS_CODE: 1', |
| 96 'INSTRUMENTATION_STATUS: test_skipped=true', |
| 97 'INSTRUMENTATION_STATUS_CODE: 0', |
| 98 'INSTRUMENTATION_STATUS: class=hello', |
| 99 'INSTRUMENTATION_STATUS: test=world', |
| 100 'INSTRUMENTATION_STATUS: stack=', |
| 101 'foo/bar.py (27)', |
| 102 'hello/world.py (42)', |
| 103 'test/file.py (1)', |
| 104 'INSTRUMENTATION_STATUS_CODE: -1', |
| 105 ] |
| 106 |
| 107 _, _, statuses = ( |
| 108 test_runner.TestRunner._ParseAmInstrumentRawOutput(raw_output)) |
| 109 |
| 110 expected = [ |
| 111 (1, {'class': 'foo', 'test': 'bar',}), |
| 112 (0, {'test_skipped': 'true'}), |
| 113 (-1, { |
| 114 'class': 'hello', |
| 115 'test': 'world', |
| 116 'stack': '\nfoo/bar.py (27)\nhello/world.py (42)\ntest/file.py (1)' |
| 117 }), |
| 118 ] |
| 119 self.assertEqual(expected, statuses) |
| 120 |
| 121 def testParseAmInstrumentRawOutput_statusResultAndCode(self): |
| 122 raw_output = [ |
| 123 'INSTRUMENTATION_STATUS: class=foo', |
| 124 'INSTRUMENTATION_STATUS: test=bar', |
| 125 'INSTRUMENTATION_STATUS_CODE: 1', |
| 126 'INSTRUMENTATION_RESULT: hello', |
| 127 'world', |
| 128 '', |
| 129 '', |
| 130 'INSTRUMENTATION_CODE: 0', |
| 131 ] |
| 132 |
| 133 code, result, statuses = ( |
| 134 test_runner.TestRunner._ParseAmInstrumentRawOutput(raw_output)) |
| 135 |
| 136 self.assertEqual(0, code) |
| 137 self.assertEqual(['hello', 'world', '', ''], result) |
| 138 self.assertEqual([(1, {'class': 'foo', 'test': 'bar'})], statuses) |
| 139 |
| 140 def testGenerateTestResult_noStatus(self): |
| 141 result = self.instance._GenerateTestResult( |
| 142 'test.package.TestClass#testMethod', [], 0, 1000) |
| 143 self.assertEqual('test.package.TestClass#testMethod', result.GetName()) |
| 144 self.assertEqual(base_test_result.ResultType.UNKNOWN, result.GetType()) |
| 145 self.assertEqual('', result.GetLog()) |
| 146 self.assertEqual(1000, result.GetDur()) |
| 147 |
| 148 def testGenerateTestResult_testPassed(self): |
| 149 statuses = [ |
| 150 (1, { |
| 151 'class': 'test.package.TestClass', |
| 152 'test': 'testMethod', |
| 153 }), |
| 154 (0, { |
| 155 'class': 'test.package.TestClass', |
| 156 'test': 'testMethod', |
| 157 }), |
| 158 ] |
| 159 result = self.instance._GenerateTestResult( |
| 160 'test.package.TestClass#testMethod', statuses, 0, 1000) |
| 161 self.assertEqual(base_test_result.ResultType.PASS, result.GetType()) |
| 162 |
| 163 def testGenerateTestResult_testSkipped_first(self): |
| 164 statuses = [ |
| 165 (0, { |
| 166 'test_skipped': 'true', |
| 167 }), |
| 168 (1, { |
| 169 'class': 'test.package.TestClass', |
| 170 'test': 'testMethod', |
| 171 }), |
| 172 (0, { |
| 173 'class': 'test.package.TestClass', |
| 174 'test': 'testMethod', |
| 175 }), |
| 176 ] |
| 177 result = self.instance._GenerateTestResult( |
| 178 'test.package.TestClass#testMethod', statuses, 0, 1000) |
| 179 self.assertEqual(base_test_result.ResultType.SKIP, result.GetType()) |
| 180 |
| 181 def testGenerateTestResult_testSkipped_last(self): |
| 182 statuses = [ |
| 183 (1, { |
| 184 'class': 'test.package.TestClass', |
| 185 'test': 'testMethod', |
| 186 }), |
| 187 (0, { |
| 188 'class': 'test.package.TestClass', |
| 189 'test': 'testMethod', |
| 190 }), |
| 191 (0, { |
| 192 'test_skipped': 'true', |
| 193 }), |
| 194 ] |
| 195 result = self.instance._GenerateTestResult( |
| 196 'test.package.TestClass#testMethod', statuses, 0, 1000) |
| 197 self.assertEqual(base_test_result.ResultType.SKIP, result.GetType()) |
| 198 |
| 199 def testGenerateTestResult_testSkipped_false(self): |
| 200 statuses = [ |
| 201 (0, { |
| 202 'test_skipped': 'false', |
| 203 }), |
| 204 (1, { |
| 205 'class': 'test.package.TestClass', |
| 206 'test': 'testMethod', |
| 207 }), |
| 208 (0, { |
| 209 'class': 'test.package.TestClass', |
| 210 'test': 'testMethod', |
| 211 }), |
| 212 ] |
| 213 result = self.instance._GenerateTestResult( |
| 214 'test.package.TestClass#testMethod', statuses, 0, 1000) |
| 215 self.assertEqual(base_test_result.ResultType.PASS, result.GetType()) |
| 216 |
| 217 def testGenerateTestResult_testFailed(self): |
| 218 statuses = [ |
| 219 (1, { |
| 220 'class': 'test.package.TestClass', |
| 221 'test': 'testMethod', |
| 222 }), |
| 223 (-2, { |
| 224 'class': 'test.package.TestClass', |
| 225 'test': 'testMethod', |
| 226 }), |
| 227 ] |
| 228 result = self.instance._GenerateTestResult( |
| 229 'test.package.TestClass#testMethod', statuses, 0, 1000) |
| 230 self.assertEqual(base_test_result.ResultType.FAIL, result.GetType()) |
| 231 |
| 232 def testGenerateTestResult_testCrashed(self): |
| 233 self.instance.test_pkg.GetPackageName = mock.Mock( |
| 234 return_value='generate.test.result.test.package') |
| 235 self.instance.device.old_interface.DismissCrashDialogIfNeeded = mock.Mock( |
| 236 return_value='generate.test.result.test.package') |
| 237 statuses = [ |
| 238 (1, { |
| 239 'class': 'test.package.TestClass', |
| 240 'test': 'testMethod', |
| 241 }), |
| 242 (-1, { |
| 243 'class': 'test.package.TestClass', |
| 244 'test': 'testMethod', |
| 245 'stack': '\nfoo/bar.py (27)\nhello/world.py (42)', |
| 246 }), |
| 247 ] |
| 248 result = self.instance._GenerateTestResult( |
| 249 'test.package.TestClass#testMethod', statuses, 0, 1000) |
| 250 self.assertEqual(base_test_result.ResultType.CRASH, result.GetType()) |
| 251 self.assertEqual('\nfoo/bar.py (27)\nhello/world.py (42)', result.GetLog()) |
| 252 |
| 253 def testRunInstrumentationTest_verifyAdbShellCommand(self): |
| 254 self.instance.options.test_runner = 'MyTestRunner' |
| 255 self.instance.device.RunShellCommand = mock.Mock() |
| 256 self.instance._GenerateTestResult = mock.Mock() |
| 257 with mock.patch('pylib.instrumentation.test_runner.' |
| 258 'TestRunner._ParseAmInstrumentRawOutput', |
| 259 return_value=(mock.Mock(), mock.Mock(), mock.Mock())): |
| 260 self.instance.RunInstrumentationTest( |
| 261 'test.package.TestClass#testMethod', |
| 262 'test.package', |
| 263 {'test_arg_key': 'test_arg_value'}, |
| 264 100) |
| 265 self.instance.device.RunShellCommand.assert_called_with( |
| 266 ['am', 'instrument', '-r', |
| 267 '-e', 'test_arg_key', "'test_arg_value'", |
| 268 '-e', 'class', "'test.package.TestClass#testMethod'", |
| 269 '-w', 'test.package/MyTestRunner'], |
| 270 timeout=100, retries=0) |
| 271 |
| 272 if __name__ == '__main__': |
| 273 unittest.main(verbosity=2) |
| 274 |
OLD | NEW |