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 |
| 14 import shard |
| 15 import test_result |
| 16 |
| 17 |
| 18 class TestException(Exception): |
| 19 pass |
| 20 |
| 21 |
| 22 class MockRunner(object): |
| 23 """A mock TestRunner.""" |
| 24 def __init__(self, device='0'): |
| 25 self.device = device |
| 26 |
| 27 def Run(self, test): |
| 28 return (test_result.TestResults.FromRun( |
| 29 ok=[test_result.BaseTestResult(test, '')]), |
| 30 None) |
| 31 |
| 32 |
| 33 class MockRunnerRetry(MockRunner): |
| 34 def Run(self, test): |
| 35 return (test_result.TestResults.FromRun( |
| 36 failed=[test_result.BaseTestResult(test, '')]), |
| 37 test) |
| 38 |
| 39 |
| 40 class MockRunnerException(MockRunner): |
| 41 def Run(self, test): |
| 42 raise TestException |
| 43 |
| 44 |
| 45 class TestWorker(unittest.TestCase): |
| 46 """Tests for shard._Worker.""" |
| 47 @staticmethod |
| 48 def _RunRunner(mock_runner, tests): |
| 49 results = [] |
| 50 retry = [] |
| 51 worker = shard._Worker(mock_runner, tests, results, retry) |
| 52 worker.start() |
| 53 worker.join() |
| 54 worker.Reraise() |
| 55 return (results, retry) |
| 56 |
| 57 def testRun(self): |
| 58 results, retry = TestWorker._RunRunner(MockRunner(), ['a', 'b']) |
| 59 self.assertEqual(len(results), 2) |
| 60 self.assertEqual(len(retry), 0) |
| 61 |
| 62 def testRetry(self): |
| 63 results, retry = TestWorker._RunRunner(MockRunnerRetry(), ['a', 'b']) |
| 64 self.assertEqual(len(results), 2) |
| 65 self.assertEqual(len(retry), 2) |
| 66 |
| 67 def testReraise(self): |
| 68 with self.assertRaises(TestException): |
| 69 TestWorker._RunRunner(MockRunnerException(), ['a', 'b']) |
| 70 |
| 71 |
| 72 class TestRunAllTests(unittest.TestCase): |
| 73 """Tests for shard._RunAllTests and shard._CreateRunners.""" |
| 74 def setUp(self): |
| 75 self.tests = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] |
| 76 |
| 77 def testRun(self): |
| 78 runners = shard._CreateRunners(MockRunner, ['0', '1']) |
| 79 results, retry = shard._RunAllTests(runners, self.tests) |
| 80 self.assertEqual(len(results), len(self.tests)) |
| 81 self.assertEqual(len(retry), 0) |
| 82 |
| 83 def testRetry(self): |
| 84 runners = shard._CreateRunners(MockRunnerRetry, ['0', '1']) |
| 85 results, retry = shard._RunAllTests(runners, self.tests) |
| 86 self.assertEqual(len(results), len(self.tests)) |
| 87 self.assertEqual(len(retry), len(self.tests)) |
| 88 |
| 89 def testReraise(self): |
| 90 runners = shard._CreateRunners(MockRunnerException, ['0', '1']) |
| 91 with self.assertRaises(TestException): |
| 92 shard._RunAllTests(runners, self.tests) |
| 93 |
| 94 |
| 95 class TestShard(unittest.TestCase): |
| 96 """Tests for shard.Shard.""" |
| 97 @staticmethod |
| 98 def _RunShard(runner_factory): |
| 99 devices = ['0', '1'] |
| 100 return shard.Shard(runner_factory, devices, ['a', 'b', 'c'], |
| 101 get_attached_devices_callable=lambda: devices) |
| 102 |
| 103 def testShard(self): |
| 104 results = TestShard._RunShard(MockRunner) |
| 105 self.assertEqual(len(results.ok), 3) |
| 106 |
| 107 def testFailing(self): |
| 108 results = TestShard._RunShard(MockRunnerRetry) |
| 109 self.assertEqual(len(results.ok), 0) |
| 110 self.assertEqual(len(results.failed), 3) |
| 111 |
| 112 |
| 113 if __name__ == '__main__': |
| 114 unittest.main() |
OLD | NEW |