| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Unittests for shard.py.""" | 5 """Unittests for shard.py.""" |
| 6 | 6 |
| 7 import os | 7 import os |
| 8 import sys | 8 import sys |
| 9 import unittest | 9 import unittest |
| 10 | 10 |
| 11 sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), | 11 sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), |
| 12 os.pardir, os.pardir)) | 12 os.pardir, os.pardir)) |
| 13 |
| 14 # Mock out android_commands.GetAttachedDevices(). |
| 13 from pylib import android_commands | 15 from pylib import android_commands |
| 16 android_commands.GetAttachedDevices = lambda: ['0', '1'] |
| 14 | 17 |
| 15 import shard | 18 import shard |
| 16 import test_result | 19 import test_result |
| 17 | 20 |
| 18 | 21 |
| 19 class TestException(Exception): | 22 class TestException(Exception): |
| 20 pass | 23 pass |
| 21 | 24 |
| 22 | 25 |
| 23 class MockRunner(object): | 26 class MockRunner(object): |
| 24 """A mock TestRunner.""" | 27 """A mock TestRunner.""" |
| 25 def __init__(self, device='0'): | 28 def __init__(self, device='0'): |
| 26 self.device = device | 29 self.device = device |
| 30 self.setups = 0 |
| 31 self.teardowns = 0 |
| 27 | 32 |
| 28 def Run(self, test): | 33 def RunTest(self, test): |
| 29 return (test_result.TestResults.FromRun( | 34 return (test_result.TestResults.FromRun( |
| 30 ok=[test_result.BaseTestResult(test, '')]), | 35 ok=[test_result.BaseTestResult(test, '')]), |
| 31 None) | 36 None) |
| 32 | 37 |
| 38 def SetUp(self): |
| 39 self.setups += 1 |
| 33 | 40 |
| 34 class MockRunnerRetry(MockRunner): | 41 def TearDown(self): |
| 35 def Run(self, test): | 42 self.teardowns += 1 |
| 43 |
| 44 |
| 45 class MockRunnerFail(MockRunner): |
| 46 def RunTest(self, test): |
| 36 return (test_result.TestResults.FromRun( | 47 return (test_result.TestResults.FromRun( |
| 37 failed=[test_result.BaseTestResult(test, '')]), | 48 failed=[test_result.BaseTestResult(test, '')]), |
| 38 test) | 49 test) |
| 39 | 50 |
| 40 | 51 |
| 52 class MockRunnerFailTwice(MockRunner): |
| 53 def __init__(self, device='0'): |
| 54 super(MockRunnerFailTwice, self).__init__(device) |
| 55 self._fails = 0 |
| 56 |
| 57 def RunTest(self, test): |
| 58 self._fails += 1 |
| 59 if self._fails <= 2: |
| 60 return (test_result.TestResults.FromRun( |
| 61 failed=[test_result.BaseTestResult(test, '')]), |
| 62 test) |
| 63 else: |
| 64 return (test_result.TestResults.FromRun( |
| 65 ok=[test_result.BaseTestResult(test, '')]), |
| 66 None) |
| 67 |
| 68 |
| 41 class MockRunnerException(MockRunner): | 69 class MockRunnerException(MockRunner): |
| 42 def Run(self, test): | 70 def RunTest(self, test): |
| 43 raise TestException | 71 raise TestException |
| 44 | 72 |
| 45 | 73 |
| 46 class TestWorker(unittest.TestCase): | 74 class TestFunctions(unittest.TestCase): |
| 47 """Tests for shard._Worker.""" | 75 """Tests for shard._RunTestsFromQueue.""" |
| 48 @staticmethod | 76 @staticmethod |
| 49 def _RunRunner(mock_runner, tests): | 77 def _RunTests(mock_runner, tests): |
| 50 results = [] | 78 results = [] |
| 51 retry = [] | 79 tests = shard._TestCollection([shard._Test(t) for t in tests]) |
| 52 worker = shard._Worker(mock_runner, tests, results, retry) | 80 shard._RunTestsFromQueue(mock_runner, tests, results) |
| 53 worker.start() | 81 return test_result.TestResults.FromTestResults(results) |
| 54 worker.join() | |
| 55 worker.ReraiseIfException() | |
| 56 return (results, retry) | |
| 57 | 82 |
| 58 def testRun(self): | 83 def testRunTestsFromQueue(self): |
| 59 results, retry = TestWorker._RunRunner(MockRunner(), ['a', 'b']) | 84 results = TestFunctions._RunTests(MockRunner(), ['a', 'b']) |
| 60 self.assertEqual(len(results), 2) | 85 self.assertEqual(len(results.ok), 2) |
| 61 self.assertEqual(len(retry), 0) | 86 self.assertEqual(len(results.GetAllBroken()), 0) |
| 62 | 87 |
| 63 def testRetry(self): | 88 def testRunTestsFromQueueRetry(self): |
| 64 results, retry = TestWorker._RunRunner(MockRunnerRetry(), ['a', 'b']) | 89 results = TestFunctions._RunTests(MockRunnerFail(), ['a', 'b']) |
| 65 self.assertEqual(len(results), 2) | 90 self.assertEqual(len(results.ok), 0) |
| 66 self.assertEqual(len(retry), 2) | 91 self.assertEqual(len(results.failed), 2) |
| 67 | 92 |
| 68 def testReraise(self): | 93 def testRunTestsFromQueueFailTwice(self): |
| 69 with self.assertRaises(TestException): | 94 results = TestFunctions._RunTests(MockRunnerFailTwice(), ['a', 'b']) |
| 70 TestWorker._RunRunner(MockRunnerException(), ['a', 'b']) | 95 self.assertEqual(len(results.ok), 2) |
| 96 self.assertEqual(len(results.GetAllBroken()), 0) |
| 97 |
| 98 def testSetUp(self): |
| 99 runners = [] |
| 100 shard._SetUp(MockRunner, '0', runners) |
| 101 self.assertEqual(len(runners), 1) |
| 102 self.assertEqual(runners[0].setups, 1) |
| 71 | 103 |
| 72 | 104 |
| 73 class TestRunAllTests(unittest.TestCase): | 105 class TestThreadGroupFunctions(unittest.TestCase): |
| 74 """Tests for shard._RunAllTests and shard._CreateRunners.""" | 106 """Tests for shard._RunAllTests and shard._CreateRunners.""" |
| 75 def setUp(self): | 107 def setUp(self): |
| 76 self.tests = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] | 108 self.tests = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] |
| 77 | 109 |
| 110 def testCreate(self): |
| 111 runners = shard._CreateRunners(MockRunner, ['0', '1']) |
| 112 for runner in runners: |
| 113 self.assertEqual(runner.setups, 1) |
| 114 |
| 78 def testRun(self): | 115 def testRun(self): |
| 79 runners = shard._CreateRunners(MockRunner, ['0', '1']) | 116 runners = [MockRunner('0'), MockRunner('1')] |
| 80 results, retry = shard._RunAllTests(runners, self.tests) | 117 results = shard._RunAllTests(runners, self.tests) |
| 81 self.assertEqual(len(results), len(self.tests)) | 118 self.assertEqual(len(results.ok), len(self.tests)) |
| 82 self.assertEqual(len(retry), 0) | 119 |
| 120 def testTearDown(self): |
| 121 runners = [MockRunner('0'), MockRunner('1')] |
| 122 shard._TearDownRunners(runners) |
| 123 for runner in runners: |
| 124 self.assertEqual(runner.teardowns, 1) |
| 83 | 125 |
| 84 def testRetry(self): | 126 def testRetry(self): |
| 85 runners = shard._CreateRunners(MockRunnerRetry, ['0', '1']) | 127 runners = shard._CreateRunners(MockRunnerFail, ['0', '1']) |
| 86 results, retry = shard._RunAllTests(runners, self.tests) | 128 results = shard._RunAllTests(runners, self.tests) |
| 87 self.assertEqual(len(results), len(self.tests)) | 129 self.assertEqual(len(results.failed), len(self.tests)) |
| 88 self.assertEqual(len(retry), len(self.tests)) | |
| 89 | 130 |
| 90 def testReraise(self): | 131 def testReraise(self): |
| 91 runners = shard._CreateRunners(MockRunnerException, ['0', '1']) | 132 runners = shard._CreateRunners(MockRunnerException, ['0', '1']) |
| 92 with self.assertRaises(TestException): | 133 with self.assertRaises(TestException): |
| 93 shard._RunAllTests(runners, self.tests) | 134 shard._RunAllTests(runners, self.tests) |
| 94 | 135 |
| 95 | 136 |
| 96 class TestShard(unittest.TestCase): | 137 class TestShard(unittest.TestCase): |
| 97 """Tests for shard.Shard.""" | 138 """Tests for shard.Shard.""" |
| 98 @staticmethod | 139 @staticmethod |
| 99 def _RunShard(runner_factory): | 140 def _RunShard(runner_factory): |
| 100 devices = ['0', '1'] | 141 return shard.ShardAndRunTests(runner_factory, ['0', '1'], ['a', 'b', 'c']) |
| 101 # Mock out android_commands.GetAttachedDevices(). | |
| 102 android_commands.GetAttachedDevices = lambda: devices | |
| 103 return shard.ShardAndRunTests(runner_factory, devices, ['a', 'b', 'c']) | |
| 104 | 142 |
| 105 def testShard(self): | 143 def testShard(self): |
| 106 results = TestShard._RunShard(MockRunner) | 144 results = TestShard._RunShard(MockRunner) |
| 107 self.assertEqual(len(results.ok), 3) | 145 self.assertEqual(len(results.ok), 3) |
| 108 | 146 |
| 109 def testFailing(self): | 147 def testFailing(self): |
| 110 results = TestShard._RunShard(MockRunnerRetry) | 148 results = TestShard._RunShard(MockRunnerFail) |
| 111 self.assertEqual(len(results.ok), 0) | 149 self.assertEqual(len(results.ok), 0) |
| 112 self.assertEqual(len(results.failed), 3) | 150 self.assertEqual(len(results.failed), 3) |
| 113 | 151 |
| 114 | 152 |
| 115 if __name__ == '__main__': | 153 if __name__ == '__main__': |
| 116 unittest.main() | 154 unittest.main() |
| OLD | NEW |