OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 """Unittests for shard.py.""" |
| 6 |
| 7 import os |
| 8 import sys |
| 9 import unittest |
| 10 |
| 11 sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), |
| 12 os.pardir, os.pardir)) |
| 13 from pylib import android_commands |
| 14 |
| 15 import shard |
| 16 import test_result |
| 17 |
| 18 |
| 19 class TestException(Exception): |
| 20 pass |
| 21 |
| 22 |
| 23 class MockRunner(object): |
| 24 """A mock TestRunner.""" |
| 25 def __init__(self, device='0'): |
| 26 self.device = device |
| 27 |
| 28 def Run(self, test): |
| 29 return (test_result.TestResults.FromRun( |
| 30 ok=[test_result.BaseTestResult(test, '')]), |
| 31 None) |
| 32 |
| 33 |
| 34 class MockRunnerRetry(MockRunner): |
| 35 def Run(self, test): |
| 36 return (test_result.TestResults.FromRun( |
| 37 failed=[test_result.BaseTestResult(test, '')]), |
| 38 test) |
| 39 |
| 40 |
| 41 class MockRunnerException(MockRunner): |
| 42 def Run(self, test): |
| 43 raise TestException |
| 44 |
| 45 |
| 46 class TestWorker(unittest.TestCase): |
| 47 """Tests for shard._Worker.""" |
| 48 @staticmethod |
| 49 def _RunRunner(mock_runner, tests): |
| 50 results = [] |
| 51 retry = [] |
| 52 worker = shard._Worker(mock_runner, tests, results, retry) |
| 53 worker.start() |
| 54 worker.join() |
| 55 worker.Reraise() |
| 56 return (results, retry) |
| 57 |
| 58 def testRun(self): |
| 59 results, retry = TestWorker._RunRunner(MockRunner(), ['a', 'b']) |
| 60 self.assertEqual(len(results), 2) |
| 61 self.assertEqual(len(retry), 0) |
| 62 |
| 63 def testRetry(self): |
| 64 results, retry = TestWorker._RunRunner(MockRunnerRetry(), ['a', 'b']) |
| 65 self.assertEqual(len(results), 2) |
| 66 self.assertEqual(len(retry), 2) |
| 67 |
| 68 def testReraise(self): |
| 69 with self.assertRaises(TestException): |
| 70 TestWorker._RunRunner(MockRunnerException(), ['a', 'b']) |
| 71 |
| 72 |
| 73 class TestRunAllTests(unittest.TestCase): |
| 74 """Tests for shard._RunAllTests and shard._CreateRunners.""" |
| 75 def setUp(self): |
| 76 self.tests = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] |
| 77 |
| 78 def testRun(self): |
| 79 runners = shard._CreateRunners(MockRunner, ['0', '1']) |
| 80 results, retry = shard._RunAllTests(runners, self.tests) |
| 81 self.assertEqual(len(results), len(self.tests)) |
| 82 self.assertEqual(len(retry), 0) |
| 83 |
| 84 def testRetry(self): |
| 85 runners = shard._CreateRunners(MockRunnerRetry, ['0', '1']) |
| 86 results, retry = shard._RunAllTests(runners, self.tests) |
| 87 self.assertEqual(len(results), len(self.tests)) |
| 88 self.assertEqual(len(retry), len(self.tests)) |
| 89 |
| 90 def testReraise(self): |
| 91 runners = shard._CreateRunners(MockRunnerException, ['0', '1']) |
| 92 with self.assertRaises(TestException): |
| 93 shard._RunAllTests(runners, self.tests) |
| 94 |
| 95 |
| 96 class TestShard(unittest.TestCase): |
| 97 """Tests for shard.Shard.""" |
| 98 @staticmethod |
| 99 def _RunShard(runner_factory): |
| 100 devices = ['0', '1'] |
| 101 # Mock out android_commands.GetAttachedDevices(). |
| 102 android_commands.GetAttachedDevices = lambda: devices |
| 103 return shard.ShardAndRunTests(runner_factory, devices, ['a', 'b', 'c']) |
| 104 |
| 105 def testShard(self): |
| 106 results = TestShard._RunShard(MockRunner) |
| 107 self.assertEqual(len(results.ok), 3) |
| 108 |
| 109 def testFailing(self): |
| 110 results = TestShard._RunShard(MockRunnerRetry) |
| 111 self.assertEqual(len(results.ok), 0) |
| 112 self.assertEqual(len(results.failed), 3) |
| 113 |
| 114 |
| 115 if __name__ == '__main__': |
| 116 unittest.main() |
OLD | NEW |