Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(244)

Side by Side Diff: build/android/pylib/base/test_dispatcher_unittest.py

Issue 1105323002: [Android] Remove more uses of android_commands from build/android/pylib. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python
1 # Copyright 2013 The Chromium Authors. All rights reserved. 2 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # 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
3 # found in the LICENSE file. 4 # found in the LICENSE file.
4 5
5 """Unittests for test_dispatcher.py.""" 6 """Unittests for test_dispatcher.py."""
6 # pylint: disable=R0201 7 # pylint: disable=R0201
7 # pylint: disable=W0212 8 # pylint: disable=W0212
8 9
9 import os 10 import os
10 import sys 11 import sys
11 import unittest 12 import unittest
12 13
13 sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)),
14 os.pardir, os.pardir))
15 14
16 # Mock out android_commands.GetAttachedDevices().
17 from pylib import android_commands
18 android_commands.GetAttachedDevices = lambda: ['0', '1']
19 from pylib import constants 15 from pylib import constants
20 from pylib.base import base_test_result 16 from pylib.base import base_test_result
21 from pylib.base import test_collection 17 from pylib.base import test_collection
22 from pylib.base import test_dispatcher 18 from pylib.base import test_dispatcher
19 from pylib.device import device_utils
23 from pylib.utils import watchdog_timer 20 from pylib.utils import watchdog_timer
24 21
22 sys.path.append(
23 os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', 'pymock'))
24 import mock
25
25 26
26 class TestException(Exception): 27 class TestException(Exception):
27 pass 28 pass
28 29
29 30
31 def _CreateMockDevice(serial):
perezju 2015/04/28 10:33:50 wait, this is creating a _real_ DeviceUtils. Shoul
jbudorick 2015/04/28 13:44:59 Done.
32 d = device_utils.DeviceUtils(serial)
33 d.IsOnline = mock.MagicMock(return_value=True)
34 return d
35
36
30 class MockRunner(object): 37 class MockRunner(object):
31 """A mock TestRunner.""" 38 """A mock TestRunner."""
32 def __init__(self, device='0', shard_index=0): 39 def __init__(self, device=_CreateMockDevice('0'), shard_index=0):
perezju 2015/04/28 10:33:50 I think this should be device=None, and then self.
jbudorick 2015/04/28 13:44:59 Done.
33 self.device_serial = device 40 self.device = device
41 self.device_serial = device.adb.GetDeviceSerial()
34 self.shard_index = shard_index 42 self.shard_index = shard_index
35 self.setups = 0 43 self.setups = 0
36 self.teardowns = 0 44 self.teardowns = 0
37 45
38 def RunTest(self, test): 46 def RunTest(self, test):
39 results = base_test_result.TestRunResults() 47 results = base_test_result.TestRunResults()
40 results.AddResult( 48 results.AddResult(
41 base_test_result.BaseTestResult(test, base_test_result.ResultType.PASS)) 49 base_test_result.BaseTestResult(test, base_test_result.ResultType.PASS))
42 return (results, None) 50 return (results, None)
43 51
44 def SetUp(self): 52 def SetUp(self):
45 self.setups += 1 53 self.setups += 1
46 54
47 def TearDown(self): 55 def TearDown(self):
48 self.teardowns += 1 56 self.teardowns += 1
49 57
50 58
51 class MockRunnerFail(MockRunner): 59 class MockRunnerFail(MockRunner):
52 def RunTest(self, test): 60 def RunTest(self, test):
53 results = base_test_result.TestRunResults() 61 results = base_test_result.TestRunResults()
54 results.AddResult( 62 results.AddResult(
55 base_test_result.BaseTestResult(test, base_test_result.ResultType.FAIL)) 63 base_test_result.BaseTestResult(test, base_test_result.ResultType.FAIL))
56 return (results, test) 64 return (results, test)
57 65
58 66
59 class MockRunnerFailTwice(MockRunner): 67 class MockRunnerFailTwice(MockRunner):
60 def __init__(self, device='0', shard_index=0): 68 def __init__(self, device=_CreateMockDevice('0'), shard_index=0):
perezju 2015/04/28 10:33:50 same here
jbudorick 2015/04/28 13:44:59 Done.
61 super(MockRunnerFailTwice, self).__init__(device, shard_index) 69 super(MockRunnerFailTwice, self).__init__(device, shard_index)
62 self._fails = 0 70 self._fails = 0
63 71
64 def RunTest(self, test): 72 def RunTest(self, test):
65 self._fails += 1 73 self._fails += 1
66 results = base_test_result.TestRunResults() 74 results = base_test_result.TestRunResults()
67 if self._fails <= 2: 75 if self._fails <= 2:
68 results.AddResult(base_test_result.BaseTestResult( 76 results.AddResult(base_test_result.BaseTestResult(
69 test, base_test_result.ResultType.FAIL)) 77 test, base_test_result.ResultType.FAIL))
70 return (results, test) 78 return (results, test)
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 self.assertEqual(len(results.GetFail()), 2) 112 self.assertEqual(len(results.GetFail()), 2)
105 113
106 def testRunTestsFromQueueFailTwice(self): 114 def testRunTestsFromQueueFailTwice(self):
107 results = TestFunctions._RunTests(MockRunnerFailTwice(), ['a', 'b']) 115 results = TestFunctions._RunTests(MockRunnerFailTwice(), ['a', 'b'])
108 self.assertEqual(len(results.GetPass()), 2) 116 self.assertEqual(len(results.GetPass()), 2)
109 self.assertEqual(len(results.GetNotPass()), 0) 117 self.assertEqual(len(results.GetNotPass()), 0)
110 118
111 def testSetUp(self): 119 def testSetUp(self):
112 runners = [] 120 runners = []
113 counter = test_dispatcher._ThreadSafeCounter() 121 counter = test_dispatcher._ThreadSafeCounter()
114 test_dispatcher._SetUp(MockRunner, '0', runners, counter) 122 test_dispatcher._SetUp(MockRunner, _CreateMockDevice('0'), runners, counter)
115 self.assertEqual(len(runners), 1) 123 self.assertEqual(len(runners), 1)
116 self.assertEqual(runners[0].setups, 1) 124 self.assertEqual(runners[0].setups, 1)
117 125
118 def testThreadSafeCounter(self): 126 def testThreadSafeCounter(self):
119 counter = test_dispatcher._ThreadSafeCounter() 127 counter = test_dispatcher._ThreadSafeCounter()
120 for i in xrange(5): 128 for i in xrange(5):
121 self.assertEqual(counter.GetAndIncrement(), i) 129 self.assertEqual(counter.GetAndIncrement(), i)
122 130
123 def testApplyMaxPerRun(self): 131 def testApplyMaxPerRun(self):
124 self.assertEqual( 132 self.assertEqual(
125 ['A:B', 'C:D', 'E', 'F:G', 'H:I'], 133 ['A:B', 'C:D', 'E', 'F:G', 'H:I'],
126 test_dispatcher.ApplyMaxPerRun(['A:B', 'C:D:E', 'F:G:H:I'], 2)) 134 test_dispatcher.ApplyMaxPerRun(['A:B', 'C:D:E', 'F:G:H:I'], 2))
127 135
128 136
129 class TestThreadGroupFunctions(unittest.TestCase): 137 class TestThreadGroupFunctions(unittest.TestCase):
130 """Tests test_dispatcher._RunAllTests and test_dispatcher._CreateRunners.""" 138 """Tests test_dispatcher._RunAllTests and test_dispatcher._CreateRunners."""
131 def setUp(self): 139 def setUp(self):
132 self.tests = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] 140 self.tests = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
133 shared_test_collection = test_collection.TestCollection( 141 shared_test_collection = test_collection.TestCollection(
134 [test_dispatcher._Test(t) for t in self.tests]) 142 [test_dispatcher._Test(t) for t in self.tests])
135 self.test_collection_factory = lambda: shared_test_collection 143 self.test_collection_factory = lambda: shared_test_collection
136 144
137 def testCreate(self): 145 def testCreate(self):
138 runners = test_dispatcher._CreateRunners(MockRunner, ['0', '1']) 146 runners = test_dispatcher._CreateRunners(
147 MockRunner,
148 [_CreateMockDevice('0'), _CreateMockDevice('1')])
139 for runner in runners: 149 for runner in runners:
140 self.assertEqual(runner.setups, 1) 150 self.assertEqual(runner.setups, 1)
141 self.assertEqual(set([r.device_serial for r in runners]), 151 self.assertEqual(set([r.device_serial for r in runners]),
142 set(['0', '1'])) 152 set(['0', '1']))
143 self.assertEqual(set([r.shard_index for r in runners]), 153 self.assertEqual(set([r.shard_index for r in runners]),
144 set([0, 1])) 154 set([0, 1]))
145 155
146 def testRun(self): 156 def testRun(self):
147 runners = [MockRunner('0'), MockRunner('1')] 157 runners = [MockRunner(_CreateMockDevice('0')),
158 MockRunner(_CreateMockDevice('1'))]
148 results, exit_code = test_dispatcher._RunAllTests( 159 results, exit_code = test_dispatcher._RunAllTests(
149 runners, self.test_collection_factory, 0) 160 runners, self.test_collection_factory, 0)
150 self.assertEqual(len(results.GetPass()), len(self.tests)) 161 self.assertEqual(len(results.GetPass()), len(self.tests))
151 self.assertEqual(exit_code, 0) 162 self.assertEqual(exit_code, 0)
152 163
153 def testTearDown(self): 164 def testTearDown(self):
154 runners = [MockRunner('0'), MockRunner('1')] 165 runners = [MockRunner(_CreateMockDevice('0')),
166 MockRunner(_CreateMockDevice('1'))]
155 test_dispatcher._TearDownRunners(runners) 167 test_dispatcher._TearDownRunners(runners)
156 for runner in runners: 168 for runner in runners:
157 self.assertEqual(runner.teardowns, 1) 169 self.assertEqual(runner.teardowns, 1)
158 170
159 def testRetry(self): 171 def testRetry(self):
160 runners = test_dispatcher._CreateRunners(MockRunnerFail, ['0', '1']) 172 runners = test_dispatcher._CreateRunners(
173 MockRunnerFail,
174 [_CreateMockDevice('0'), _CreateMockDevice('1')])
161 results, exit_code = test_dispatcher._RunAllTests( 175 results, exit_code = test_dispatcher._RunAllTests(
162 runners, self.test_collection_factory, 0) 176 runners, self.test_collection_factory, 0)
163 self.assertEqual(len(results.GetFail()), len(self.tests)) 177 self.assertEqual(len(results.GetFail()), len(self.tests))
164 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) 178 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE)
165 179
166 def testReraise(self): 180 def testReraise(self):
167 runners = test_dispatcher._CreateRunners(MockRunnerException, ['0', '1']) 181 runners = test_dispatcher._CreateRunners(
182 MockRunnerException,
183 [_CreateMockDevice('0'), _CreateMockDevice('1')])
168 with self.assertRaises(TestException): 184 with self.assertRaises(TestException):
169 test_dispatcher._RunAllTests(runners, self.test_collection_factory, 0) 185 test_dispatcher._RunAllTests(runners, self.test_collection_factory, 0)
170 186
171 187
172 class TestShard(unittest.TestCase): 188 class TestShard(unittest.TestCase):
173 """Tests test_dispatcher.RunTests with sharding.""" 189 """Tests test_dispatcher.RunTests with sharding."""
174 @staticmethod 190 @staticmethod
175 def _RunShard(runner_factory): 191 def _RunShard(runner_factory):
176 return test_dispatcher.RunTests( 192 return test_dispatcher.RunTests(
177 ['a', 'b', 'c'], runner_factory, ['0', '1'], shard=True) 193 ['a', 'b', 'c'], runner_factory,
194 [_CreateMockDevice('0'), _CreateMockDevice('1')],
195 shard=True)
178 196
179 def testShard(self): 197 def testShard(self):
180 results, exit_code = TestShard._RunShard(MockRunner) 198 results, exit_code = TestShard._RunShard(MockRunner)
181 self.assertEqual(len(results.GetPass()), 3) 199 self.assertEqual(len(results.GetPass()), 3)
182 self.assertEqual(exit_code, 0) 200 self.assertEqual(exit_code, 0)
183 201
184 def testFailing(self): 202 def testFailing(self):
185 results, exit_code = TestShard._RunShard(MockRunnerFail) 203 results, exit_code = TestShard._RunShard(MockRunnerFail)
186 self.assertEqual(len(results.GetPass()), 0) 204 self.assertEqual(len(results.GetPass()), 0)
187 self.assertEqual(len(results.GetFail()), 3) 205 self.assertEqual(len(results.GetFail()), 3)
188 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) 206 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE)
189 207
190 def testNoTests(self): 208 def testNoTests(self):
191 results, exit_code = test_dispatcher.RunTests( 209 results, exit_code = test_dispatcher.RunTests(
192 [], MockRunner, ['0', '1'], shard=True) 210 [], MockRunner, ['0', '1'], shard=True)
193 self.assertEqual(len(results.GetAll()), 0) 211 self.assertEqual(len(results.GetAll()), 0)
194 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) 212 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE)
195 213
196 214
197 class TestReplicate(unittest.TestCase): 215 class TestReplicate(unittest.TestCase):
198 """Tests test_dispatcher.RunTests with replication.""" 216 """Tests test_dispatcher.RunTests with replication."""
199 @staticmethod 217 @staticmethod
200 def _RunReplicate(runner_factory): 218 def _RunReplicate(runner_factory):
201 return test_dispatcher.RunTests( 219 return test_dispatcher.RunTests(
202 ['a', 'b', 'c'], runner_factory, ['0', '1'], shard=False) 220 ['a', 'b', 'c'], runner_factory,
221 [_CreateMockDevice('0'), _CreateMockDevice('1')],
222 shard=False)
203 223
204 def testReplicate(self): 224 def testReplicate(self):
205 results, exit_code = TestReplicate._RunReplicate(MockRunner) 225 results, exit_code = TestReplicate._RunReplicate(MockRunner)
206 # We expect 6 results since each test should have been run on every device 226 # We expect 6 results since each test should have been run on every device
207 self.assertEqual(len(results.GetPass()), 6) 227 self.assertEqual(len(results.GetPass()), 6)
208 self.assertEqual(exit_code, 0) 228 self.assertEqual(exit_code, 0)
209 229
210 def testFailing(self): 230 def testFailing(self):
211 results, exit_code = TestReplicate._RunReplicate(MockRunnerFail) 231 results, exit_code = TestReplicate._RunReplicate(MockRunnerFail)
212 self.assertEqual(len(results.GetPass()), 0) 232 self.assertEqual(len(results.GetPass()), 0)
213 self.assertEqual(len(results.GetFail()), 6) 233 self.assertEqual(len(results.GetFail()), 6)
214 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) 234 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE)
215 235
216 def testNoTests(self): 236 def testNoTests(self):
217 results, exit_code = test_dispatcher.RunTests( 237 results, exit_code = test_dispatcher.RunTests(
218 [], MockRunner, ['0', '1'], shard=False) 238 [], MockRunner,
239 [_CreateMockDevice('0'), _CreateMockDevice('1')],
240 shard=False)
219 self.assertEqual(len(results.GetAll()), 0) 241 self.assertEqual(len(results.GetAll()), 0)
220 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) 242 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE)
221 243
222 244
223 if __name__ == '__main__': 245 if __name__ == '__main__':
224 unittest.main() 246 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698