OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 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 """Unittests for test_dispatcher.py.""" | 6 """Unittests for test_dispatcher.py.""" |
7 # pylint: disable=R0201 | |
8 # pylint: disable=W0212 | |
9 | 7 |
10 import os | 8 # pylint: disable=no-self-use |
11 import sys | 9 # pylint: disable=protected-access |
| 10 |
12 import unittest | 11 import unittest |
13 | 12 |
14 | |
15 from devil.android import device_utils | 13 from devil.android import device_utils |
16 from devil.android.sdk import adb_wrapper | 14 from devil.android.sdk import adb_wrapper |
| 15 from devil.constants import exit_codes |
17 from devil.utils import watchdog_timer | 16 from devil.utils import watchdog_timer |
18 from pylib import constants | |
19 from pylib.base import base_test_result | 17 from pylib.base import base_test_result |
20 from pylib.base import test_collection | 18 from pylib.base import test_collection |
21 from pylib.base import test_dispatcher | 19 from pylib.base import test_dispatcher |
| 20 from pylib.constants import host_paths |
22 | 21 |
23 sys.path.append( | 22 with host_paths.SysPath(host_paths.PYMOCK_PATH): |
24 os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) | 23 import mock # pylint: disable=import-error |
25 import mock # pylint: disable=import-error | |
26 | 24 |
27 | 25 |
28 class TestException(Exception): | 26 class TestException(Exception): |
29 pass | 27 pass |
30 | 28 |
31 | 29 |
32 def _MockDevice(serial): | 30 def _MockDevice(serial): |
33 d = mock.MagicMock(spec=device_utils.DeviceUtils) | 31 d = mock.MagicMock(spec=device_utils.DeviceUtils) |
34 d.__str__.return_value = serial | 32 d.__str__.return_value = serial |
35 d.adb = mock.MagicMock(spec=adb_wrapper.AdbWrapper) | 33 d.adb = mock.MagicMock(spec=adb_wrapper.AdbWrapper) |
(...skipping 132 matching lines...) Loading... |
168 test_dispatcher._TearDownRunners(runners) | 166 test_dispatcher._TearDownRunners(runners) |
169 for runner in runners: | 167 for runner in runners: |
170 self.assertEqual(runner.teardowns, 1) | 168 self.assertEqual(runner.teardowns, 1) |
171 | 169 |
172 def testRetry(self): | 170 def testRetry(self): |
173 runners = test_dispatcher._CreateRunners( | 171 runners = test_dispatcher._CreateRunners( |
174 MockRunnerFail, [_MockDevice('0'), _MockDevice('1')]) | 172 MockRunnerFail, [_MockDevice('0'), _MockDevice('1')]) |
175 results, exit_code = test_dispatcher._RunAllTests( | 173 results, exit_code = test_dispatcher._RunAllTests( |
176 runners, self.test_collection_factory, 0) | 174 runners, self.test_collection_factory, 0) |
177 self.assertEqual(len(results.GetFail()), len(self.tests)) | 175 self.assertEqual(len(results.GetFail()), len(self.tests)) |
178 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) | 176 self.assertEqual(exit_code, exit_codes.ERROR) |
179 | 177 |
180 def testReraise(self): | 178 def testReraise(self): |
181 runners = test_dispatcher._CreateRunners( | 179 runners = test_dispatcher._CreateRunners( |
182 MockRunnerException, [_MockDevice('0'), _MockDevice('1')]) | 180 MockRunnerException, [_MockDevice('0'), _MockDevice('1')]) |
183 with self.assertRaises(TestException): | 181 with self.assertRaises(TestException): |
184 test_dispatcher._RunAllTests(runners, self.test_collection_factory, 0) | 182 test_dispatcher._RunAllTests(runners, self.test_collection_factory, 0) |
185 | 183 |
186 | 184 |
187 class TestShard(unittest.TestCase): | 185 class TestShard(unittest.TestCase): |
188 """Tests test_dispatcher.RunTests with sharding.""" | 186 """Tests test_dispatcher.RunTests with sharding.""" |
189 @staticmethod | 187 @staticmethod |
190 def _RunShard(runner_factory): | 188 def _RunShard(runner_factory): |
191 return test_dispatcher.RunTests( | 189 return test_dispatcher.RunTests( |
192 ['a', 'b', 'c'], runner_factory, [_MockDevice('0'), _MockDevice('1')], | 190 ['a', 'b', 'c'], runner_factory, [_MockDevice('0'), _MockDevice('1')], |
193 shard=True) | 191 shard=True) |
194 | 192 |
195 def testShard(self): | 193 def testShard(self): |
196 results, exit_code = TestShard._RunShard(MockRunner) | 194 results, exit_code = TestShard._RunShard(MockRunner) |
197 self.assertEqual(len(results.GetPass()), 3) | 195 self.assertEqual(len(results.GetPass()), 3) |
198 self.assertEqual(exit_code, 0) | 196 self.assertEqual(exit_code, 0) |
199 | 197 |
200 def testFailing(self): | 198 def testFailing(self): |
201 results, exit_code = TestShard._RunShard(MockRunnerFail) | 199 results, exit_code = TestShard._RunShard(MockRunnerFail) |
202 self.assertEqual(len(results.GetPass()), 0) | 200 self.assertEqual(len(results.GetPass()), 0) |
203 self.assertEqual(len(results.GetFail()), 3) | 201 self.assertEqual(len(results.GetFail()), 3) |
204 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) | 202 self.assertEqual(exit_code, exit_codes.ERROR) |
205 | 203 |
206 def testNoTests(self): | 204 def testNoTests(self): |
207 results, exit_code = test_dispatcher.RunTests( | 205 results, exit_code = test_dispatcher.RunTests( |
208 [], MockRunner, [_MockDevice('0'), _MockDevice('1')], shard=True) | 206 [], MockRunner, [_MockDevice('0'), _MockDevice('1')], shard=True) |
209 self.assertEqual(len(results.GetAll()), 0) | 207 self.assertEqual(len(results.GetAll()), 0) |
210 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) | 208 self.assertEqual(exit_code, exit_codes.ERROR) |
211 | 209 |
212 | 210 |
213 class TestReplicate(unittest.TestCase): | 211 class TestReplicate(unittest.TestCase): |
214 """Tests test_dispatcher.RunTests with replication.""" | 212 """Tests test_dispatcher.RunTests with replication.""" |
215 @staticmethod | 213 @staticmethod |
216 def _RunReplicate(runner_factory): | 214 def _RunReplicate(runner_factory): |
217 return test_dispatcher.RunTests( | 215 return test_dispatcher.RunTests( |
218 ['a', 'b', 'c'], runner_factory, [_MockDevice('0'), _MockDevice('1')], | 216 ['a', 'b', 'c'], runner_factory, [_MockDevice('0'), _MockDevice('1')], |
219 shard=False) | 217 shard=False) |
220 | 218 |
221 def testReplicate(self): | 219 def testReplicate(self): |
222 results, exit_code = TestReplicate._RunReplicate(MockRunner) | 220 results, exit_code = TestReplicate._RunReplicate(MockRunner) |
223 # We expect 6 results since each test should have been run on every device | 221 # We expect 6 results since each test should have been run on every device |
224 self.assertEqual(len(results.GetPass()), 6) | 222 self.assertEqual(len(results.GetPass()), 6) |
225 self.assertEqual(exit_code, 0) | 223 self.assertEqual(exit_code, 0) |
226 | 224 |
227 def testFailing(self): | 225 def testFailing(self): |
228 results, exit_code = TestReplicate._RunReplicate(MockRunnerFail) | 226 results, exit_code = TestReplicate._RunReplicate(MockRunnerFail) |
229 self.assertEqual(len(results.GetPass()), 0) | 227 self.assertEqual(len(results.GetPass()), 0) |
230 self.assertEqual(len(results.GetFail()), 6) | 228 self.assertEqual(len(results.GetFail()), 6) |
231 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) | 229 self.assertEqual(exit_code, exit_codes.ERROR) |
232 | 230 |
233 def testNoTests(self): | 231 def testNoTests(self): |
234 results, exit_code = test_dispatcher.RunTests( | 232 results, exit_code = test_dispatcher.RunTests( |
235 [], MockRunner, [_MockDevice('0'), _MockDevice('1')], shard=False) | 233 [], MockRunner, [_MockDevice('0'), _MockDevice('1')], shard=False) |
236 self.assertEqual(len(results.GetAll()), 0) | 234 self.assertEqual(len(results.GetAll()), 0) |
237 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) | 235 self.assertEqual(exit_code, exit_codes.ERROR) |
238 | 236 |
239 | 237 |
240 if __name__ == '__main__': | 238 if __name__ == '__main__': |
241 unittest.main() | 239 unittest.main() |
OLD | NEW |