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 | 13 |
14 # Mock out android_commands.GetAttachedDevices(). | 14 # Mock out android_commands.GetAttachedDevices(). |
15 from pylib import android_commands | 15 from pylib import android_commands |
16 android_commands.GetAttachedDevices = lambda: ['0', '1'] | 16 android_commands.GetAttachedDevices = lambda: ['0', '1'] |
17 | 17 |
| 18 import base_test_result |
18 import shard | 19 import shard |
19 import test_result | |
20 | 20 |
21 | 21 |
22 class TestException(Exception): | 22 class TestException(Exception): |
23 pass | 23 pass |
24 | 24 |
25 | 25 |
26 class MockRunner(object): | 26 class MockRunner(object): |
27 """A mock TestRunner.""" | 27 """A mock TestRunner.""" |
28 def __init__(self, device='0', shard_index=0): | 28 def __init__(self, device='0', shard_index=0): |
29 self.device = device | 29 self.device = device |
30 self.shard_index = shard_index | 30 self.shard_index = shard_index |
31 self.setups = 0 | 31 self.setups = 0 |
32 self.teardowns = 0 | 32 self.teardowns = 0 |
33 | 33 |
34 def RunTest(self, test): | 34 def RunTest(self, test): |
35 return (test_result.TestResults.FromRun( | 35 results = base_test_result.TestRunResults() |
36 ok=[test_result.BaseTestResult(test, '')]), | 36 results.AddResult( |
37 None) | 37 base_test_result.BaseTestResult(test, base_test_result.ResultType.PASS)) |
| 38 return (results, None) |
38 | 39 |
39 def SetUp(self): | 40 def SetUp(self): |
40 self.setups += 1 | 41 self.setups += 1 |
41 | 42 |
42 def TearDown(self): | 43 def TearDown(self): |
43 self.teardowns += 1 | 44 self.teardowns += 1 |
44 | 45 |
45 | 46 |
46 class MockRunnerFail(MockRunner): | 47 class MockRunnerFail(MockRunner): |
47 def RunTest(self, test): | 48 def RunTest(self, test): |
48 return (test_result.TestResults.FromRun( | 49 results = base_test_result.TestRunResults() |
49 failed=[test_result.BaseTestResult(test, '')]), | 50 results.AddResult( |
50 test) | 51 base_test_result.BaseTestResult(test, base_test_result.ResultType.FAIL)) |
| 52 return (results, test) |
51 | 53 |
52 | 54 |
53 class MockRunnerFailTwice(MockRunner): | 55 class MockRunnerFailTwice(MockRunner): |
54 def __init__(self, device='0', shard_index=0): | 56 def __init__(self, device='0', shard_index=0): |
55 super(MockRunnerFailTwice, self).__init__(device, shard_index) | 57 super(MockRunnerFailTwice, self).__init__(device, shard_index) |
56 self._fails = 0 | 58 self._fails = 0 |
57 | 59 |
58 def RunTest(self, test): | 60 def RunTest(self, test): |
59 self._fails += 1 | 61 self._fails += 1 |
| 62 results = base_test_result.TestRunResults() |
60 if self._fails <= 2: | 63 if self._fails <= 2: |
61 return (test_result.TestResults.FromRun( | 64 results.AddResult(base_test_result.BaseTestResult( |
62 failed=[test_result.BaseTestResult(test, '')]), | 65 test, base_test_result.ResultType.FAIL)) |
63 test) | 66 return (results, test) |
64 else: | 67 else: |
65 return (test_result.TestResults.FromRun( | 68 results.AddResult(base_test_result.BaseTestResult( |
66 ok=[test_result.BaseTestResult(test, '')]), | 69 test, base_test_result.ResultType.PASS)) |
67 None) | 70 return (results, None) |
68 | 71 |
69 | 72 |
70 class MockRunnerException(MockRunner): | 73 class MockRunnerException(MockRunner): |
71 def RunTest(self, test): | 74 def RunTest(self, test): |
72 raise TestException | 75 raise TestException |
73 | 76 |
74 | 77 |
75 class TestFunctions(unittest.TestCase): | 78 class TestFunctions(unittest.TestCase): |
76 """Tests for shard._RunTestsFromQueue.""" | 79 """Tests for shard._RunTestsFromQueue.""" |
77 @staticmethod | 80 @staticmethod |
78 def _RunTests(mock_runner, tests): | 81 def _RunTests(mock_runner, tests): |
79 results = [] | 82 results = [] |
80 tests = shard._TestCollection([shard._Test(t) for t in tests]) | 83 tests = shard._TestCollection([shard._Test(t) for t in tests]) |
81 shard._RunTestsFromQueue(mock_runner, tests, results) | 84 shard._RunTestsFromQueue(mock_runner, tests, results) |
82 return test_result.TestResults.FromTestResults(results) | 85 run_results = base_test_result.TestRunResults() |
| 86 for r in results: |
| 87 run_results.AddTestRunResults(r) |
| 88 return run_results |
83 | 89 |
84 def testRunTestsFromQueue(self): | 90 def testRunTestsFromQueue(self): |
85 results = TestFunctions._RunTests(MockRunner(), ['a', 'b']) | 91 results = TestFunctions._RunTests(MockRunner(), ['a', 'b']) |
86 self.assertEqual(len(results.ok), 2) | 92 self.assertEqual(len(results.GetPass()), 2) |
87 self.assertEqual(len(results.GetAllBroken()), 0) | 93 self.assertEqual(len(results.GetNotPass()), 0) |
88 | 94 |
89 def testRunTestsFromQueueRetry(self): | 95 def testRunTestsFromQueueRetry(self): |
90 results = TestFunctions._RunTests(MockRunnerFail(), ['a', 'b']) | 96 results = TestFunctions._RunTests(MockRunnerFail(), ['a', 'b']) |
91 self.assertEqual(len(results.ok), 0) | 97 self.assertEqual(len(results.GetPass()), 0) |
92 self.assertEqual(len(results.failed), 2) | 98 self.assertEqual(len(results.GetFail()), 2) |
93 | 99 |
94 def testRunTestsFromQueueFailTwice(self): | 100 def testRunTestsFromQueueFailTwice(self): |
95 results = TestFunctions._RunTests(MockRunnerFailTwice(), ['a', 'b']) | 101 results = TestFunctions._RunTests(MockRunnerFailTwice(), ['a', 'b']) |
96 self.assertEqual(len(results.ok), 2) | 102 self.assertEqual(len(results.GetPass()), 2) |
97 self.assertEqual(len(results.GetAllBroken()), 0) | 103 self.assertEqual(len(results.GetNotPass()), 0) |
98 | 104 |
99 def testSetUp(self): | 105 def testSetUp(self): |
100 runners = [] | 106 runners = [] |
101 counter = shard._ThreadSafeCounter() | 107 counter = shard._ThreadSafeCounter() |
102 shard._SetUp(MockRunner, '0', runners, counter) | 108 shard._SetUp(MockRunner, '0', runners, counter) |
103 self.assertEqual(len(runners), 1) | 109 self.assertEqual(len(runners), 1) |
104 self.assertEqual(runners[0].setups, 1) | 110 self.assertEqual(runners[0].setups, 1) |
105 | 111 |
106 def testThreadSafeCounter(self): | 112 def testThreadSafeCounter(self): |
107 counter = shard._ThreadSafeCounter() | 113 counter = shard._ThreadSafeCounter() |
(...skipping 11 matching lines...) Expand all Loading... |
119 for runner in runners: | 125 for runner in runners: |
120 self.assertEqual(runner.setups, 1) | 126 self.assertEqual(runner.setups, 1) |
121 self.assertEqual(set([r.device for r in runners]), | 127 self.assertEqual(set([r.device for r in runners]), |
122 set(['0', '1'])) | 128 set(['0', '1'])) |
123 self.assertEqual(set([r.shard_index for r in runners]), | 129 self.assertEqual(set([r.shard_index for r in runners]), |
124 set([0, 1])) | 130 set([0, 1])) |
125 | 131 |
126 def testRun(self): | 132 def testRun(self): |
127 runners = [MockRunner('0'), MockRunner('1')] | 133 runners = [MockRunner('0'), MockRunner('1')] |
128 results = shard._RunAllTests(runners, self.tests) | 134 results = shard._RunAllTests(runners, self.tests) |
129 self.assertEqual(len(results.ok), len(self.tests)) | 135 self.assertEqual(len(results.GetPass()), len(self.tests)) |
130 | 136 |
131 def testTearDown(self): | 137 def testTearDown(self): |
132 runners = [MockRunner('0'), MockRunner('1')] | 138 runners = [MockRunner('0'), MockRunner('1')] |
133 shard._TearDownRunners(runners) | 139 shard._TearDownRunners(runners) |
134 for runner in runners: | 140 for runner in runners: |
135 self.assertEqual(runner.teardowns, 1) | 141 self.assertEqual(runner.teardowns, 1) |
136 | 142 |
137 def testRetry(self): | 143 def testRetry(self): |
138 runners = shard._CreateRunners(MockRunnerFail, ['0', '1']) | 144 runners = shard._CreateRunners(MockRunnerFail, ['0', '1']) |
139 results = shard._RunAllTests(runners, self.tests) | 145 results = shard._RunAllTests(runners, self.tests) |
140 self.assertEqual(len(results.failed), len(self.tests)) | 146 self.assertEqual(len(results.GetFail()), len(self.tests)) |
141 | 147 |
142 def testReraise(self): | 148 def testReraise(self): |
143 runners = shard._CreateRunners(MockRunnerException, ['0', '1']) | 149 runners = shard._CreateRunners(MockRunnerException, ['0', '1']) |
144 with self.assertRaises(TestException): | 150 with self.assertRaises(TestException): |
145 shard._RunAllTests(runners, self.tests) | 151 shard._RunAllTests(runners, self.tests) |
146 | 152 |
147 | 153 |
148 class TestShard(unittest.TestCase): | 154 class TestShard(unittest.TestCase): |
149 """Tests for shard.Shard.""" | 155 """Tests for shard.Shard.""" |
150 @staticmethod | 156 @staticmethod |
151 def _RunShard(runner_factory): | 157 def _RunShard(runner_factory): |
152 return shard.ShardAndRunTests(runner_factory, ['0', '1'], ['a', 'b', 'c']) | 158 return shard.ShardAndRunTests(runner_factory, ['0', '1'], ['a', 'b', 'c']) |
153 | 159 |
154 def testShard(self): | 160 def testShard(self): |
155 results = TestShard._RunShard(MockRunner) | 161 results = TestShard._RunShard(MockRunner) |
156 self.assertEqual(len(results.ok), 3) | 162 self.assertEqual(len(results.GetPass()), 3) |
157 | 163 |
158 def testFailing(self): | 164 def testFailing(self): |
159 results = TestShard._RunShard(MockRunnerFail) | 165 results = TestShard._RunShard(MockRunnerFail) |
160 self.assertEqual(len(results.ok), 0) | 166 self.assertEqual(len(results.GetPass()), 0) |
161 self.assertEqual(len(results.failed), 3) | 167 self.assertEqual(len(results.GetFail()), 3) |
162 | 168 |
163 | 169 |
164 if __name__ == '__main__': | 170 if __name__ == '__main__': |
165 unittest.main() | 171 unittest.main() |
OLD | NEW |