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