OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Unittests for test_runner.py.""" |
| 7 |
| 8 import collections |
| 9 import json |
| 10 import os |
| 11 import sys |
| 12 import unittest |
| 13 |
| 14 import test_runner |
| 15 |
| 16 |
| 17 class TestCase(unittest.TestCase): |
| 18 """Test case which supports installing mocks. Uninstalls on tear down.""" |
| 19 |
| 20 def __init__(self, *args, **kwargs): |
| 21 """Initializes a new instance of this class.""" |
| 22 super(TestCase, self).__init__(*args, **kwargs) |
| 23 |
| 24 # Maps object to a dict which maps names of mocked members to their |
| 25 # original values. |
| 26 self._mocks = collections.OrderedDict() |
| 27 |
| 28 def mock(self, obj, member, mock): |
| 29 """Installs mock in place of the named member of the given obj. |
| 30 |
| 31 Args: |
| 32 obj: Any object. |
| 33 member: String naming the attribute of the object to mock. |
| 34 mock: The mock to install. |
| 35 """ |
| 36 self._mocks.setdefault(obj, collections.OrderedDict()).setdefault( |
| 37 member, getattr(obj, member)) |
| 38 setattr(obj, member, mock) |
| 39 |
| 40 def tearDown(self, *args, **kwargs): |
| 41 """Uninstalls mocks.""" |
| 42 super(TestCase, self).tearDown(*args, **kwargs) |
| 43 |
| 44 for obj in self._mocks: |
| 45 for member, original_value in self._mocks[obj].iteritems(): |
| 46 setattr(obj, member, original_value) |
| 47 |
| 48 |
| 49 class GetKIFTestFilterTest(TestCase): |
| 50 """Tests for test_runner.get_kif_test_filter.""" |
| 51 |
| 52 def test_correct(self): |
| 53 """Ensures correctness of filter.""" |
| 54 tests = [ |
| 55 'KIF.test1', |
| 56 'KIF.test2', |
| 57 ] |
| 58 expected = 'NAME:test1|test2' |
| 59 |
| 60 self.assertEqual(test_runner.get_kif_test_filter(tests), expected) |
| 61 |
| 62 def test_correct_inverted(self): |
| 63 """Ensures correctness of inverted filter.""" |
| 64 tests = [ |
| 65 'KIF.test1', |
| 66 'KIF.test2', |
| 67 ] |
| 68 expected = '-NAME:test1|test2' |
| 69 |
| 70 self.assertEqual( |
| 71 test_runner.get_kif_test_filter(tests, invert=True), expected) |
| 72 |
| 73 |
| 74 class GetGTestFilterTest(TestCase): |
| 75 """Tests for test_runner.get_gtest_filter.""" |
| 76 |
| 77 def test_correct(self): |
| 78 """Ensures correctness of filter.""" |
| 79 tests = [ |
| 80 'test.1', |
| 81 'test.2', |
| 82 ] |
| 83 expected = 'test.1:test.2' |
| 84 |
| 85 self.assertEqual(test_runner.get_gtest_filter(tests), expected) |
| 86 |
| 87 def test_correct_inverted(self): |
| 88 """Ensures correctness of inverted filter.""" |
| 89 tests = [ |
| 90 'test.1', |
| 91 'test.2', |
| 92 ] |
| 93 expected = '-test.1:test.2' |
| 94 |
| 95 self.assertEqual( |
| 96 test_runner.get_gtest_filter(tests, invert=True), expected) |
| 97 |
| 98 |
| 99 class SimulatorTestRunnerTest(TestCase): |
| 100 """Tests for test_runner.SimulatorTestRunner.""" |
| 101 |
| 102 def test_app_not_found(self): |
| 103 """Ensures AppNotFoundError is raised.""" |
| 104 def exists(path): |
| 105 if path == 'fake-app': |
| 106 return False |
| 107 return True |
| 108 |
| 109 def find_xcode(version): |
| 110 return {'found': True} |
| 111 |
| 112 def check_output(command): |
| 113 return 'fake-bundle-id' |
| 114 |
| 115 self.mock(test_runner.os.path, 'exists', exists) |
| 116 self.mock(test_runner.find_xcode, 'find_xcode', find_xcode) |
| 117 self.mock(test_runner.subprocess, 'check_output', check_output) |
| 118 |
| 119 self.assertRaises( |
| 120 test_runner.AppNotFoundError, |
| 121 test_runner.SimulatorTestRunner, |
| 122 'fake-app', |
| 123 'fake-iossim', |
| 124 'platform', |
| 125 'os', |
| 126 'xcode-version', |
| 127 'out-dir', |
| 128 ) |
| 129 |
| 130 def test_iossim_not_found(self): |
| 131 """Ensures SimulatorNotFoundError is raised.""" |
| 132 def exists(path): |
| 133 if path == 'fake-iossim': |
| 134 return False |
| 135 return True |
| 136 |
| 137 def find_xcode(version): |
| 138 return {'found': True} |
| 139 |
| 140 def check_output(command): |
| 141 return 'fake-bundle-id' |
| 142 |
| 143 self.mock(test_runner.os.path, 'exists', exists) |
| 144 self.mock(test_runner.find_xcode, 'find_xcode', find_xcode) |
| 145 self.mock(test_runner.subprocess, 'check_output', check_output) |
| 146 |
| 147 self.assertRaises( |
| 148 test_runner.SimulatorNotFoundError, |
| 149 test_runner.SimulatorTestRunner, |
| 150 'fake-app', |
| 151 'fake-iossim', |
| 152 'platform', |
| 153 'os', |
| 154 'xcode-version', |
| 155 'out-dir', |
| 156 ) |
| 157 |
| 158 def test_init(self): |
| 159 """Ensures instance is created.""" |
| 160 def exists(path): |
| 161 return True |
| 162 |
| 163 def find_xcode(version): |
| 164 return {'found': True} |
| 165 |
| 166 def check_output(command): |
| 167 return 'fake-bundle-id' |
| 168 |
| 169 self.mock(test_runner.os.path, 'exists', exists) |
| 170 self.mock(test_runner.find_xcode, 'find_xcode', find_xcode) |
| 171 self.mock(test_runner.subprocess, 'check_output', check_output) |
| 172 |
| 173 tr = test_runner.SimulatorTestRunner( |
| 174 'fake-app', |
| 175 'fake-iossim', |
| 176 'platform', |
| 177 'os', |
| 178 'xcode-version', |
| 179 'out-dir', |
| 180 ) |
| 181 |
| 182 self.failUnless(tr) |
| 183 |
| 184 def test_startup_crash(self): |
| 185 """Ensures test is relaunched once on startup crash.""" |
| 186 def exists(path): |
| 187 return True |
| 188 |
| 189 def find_xcode(version): |
| 190 return {'found': True} |
| 191 |
| 192 def check_output(command): |
| 193 return 'fake-bundle-id' |
| 194 |
| 195 def set_up(self): |
| 196 return |
| 197 |
| 198 @staticmethod |
| 199 def _run(command): |
| 200 return collections.namedtuple('result', ['crashed', 'crashed_test'])( |
| 201 crashed=True, crashed_test=None) |
| 202 |
| 203 def tear_down(self): |
| 204 return |
| 205 |
| 206 self.mock(test_runner.os.path, 'exists', exists) |
| 207 self.mock(test_runner.find_xcode, 'find_xcode', find_xcode) |
| 208 self.mock(test_runner.subprocess, 'check_output', check_output) |
| 209 self.mock(test_runner.SimulatorTestRunner, 'set_up', set_up) |
| 210 self.mock(test_runner.TestRunner, '_run', _run) |
| 211 self.mock(test_runner.SimulatorTestRunner, 'tear_down', tear_down) |
| 212 |
| 213 tr = test_runner.SimulatorTestRunner( |
| 214 'fake-app', |
| 215 'fake-iossim', |
| 216 'platform', |
| 217 'os', |
| 218 'xcode-version', |
| 219 'out-dir', |
| 220 ) |
| 221 self.assertRaises(test_runner.AppLaunchError, tr.launch) |
| 222 |
| 223 def test_relaunch(self): |
| 224 """Ensures test is relaunched on test crash until tests complete.""" |
| 225 def exists(path): |
| 226 return True |
| 227 |
| 228 def find_xcode(version): |
| 229 return {'found': True} |
| 230 |
| 231 def check_output(command): |
| 232 return 'fake-bundle-id' |
| 233 |
| 234 def set_up(self): |
| 235 return |
| 236 |
| 237 @staticmethod |
| 238 def _run(command): |
| 239 result = collections.namedtuple( |
| 240 'result', [ |
| 241 'crashed', |
| 242 'crashed_test', |
| 243 'failed_tests', |
| 244 'flaked_tests', |
| 245 'passed_tests', |
| 246 ], |
| 247 ) |
| 248 if '-e' not in command: |
| 249 # First run, has no test filter supplied. Mock a crash. |
| 250 return result( |
| 251 crashed=True, |
| 252 crashed_test='c', |
| 253 failed_tests={'b': ['b-out'], 'c': ['Did not complete.']}, |
| 254 flaked_tests={'d': ['d-out']}, |
| 255 passed_tests=['a'], |
| 256 ) |
| 257 else: |
| 258 return result( |
| 259 crashed=False, |
| 260 crashed_test=None, |
| 261 failed_tests={}, |
| 262 flaked_tests={}, |
| 263 passed_tests=[], |
| 264 ) |
| 265 |
| 266 def tear_down(self): |
| 267 return |
| 268 |
| 269 self.mock(test_runner.os.path, 'exists', exists) |
| 270 self.mock(test_runner.find_xcode, 'find_xcode', find_xcode) |
| 271 self.mock(test_runner.subprocess, 'check_output', check_output) |
| 272 self.mock(test_runner.SimulatorTestRunner, 'set_up', set_up) |
| 273 self.mock(test_runner.TestRunner, '_run', _run) |
| 274 self.mock(test_runner.SimulatorTestRunner, 'tear_down', tear_down) |
| 275 |
| 276 tr = test_runner.SimulatorTestRunner( |
| 277 'fake-app', |
| 278 'fake-iossim', |
| 279 'platform', |
| 280 'os', |
| 281 'xcode-version', |
| 282 'out-dir', |
| 283 ) |
| 284 tr.launch() |
| 285 self.failUnless(tr.logs) |
| 286 |
| 287 |
| 288 if __name__ == '__main__': |
| 289 unittest.main() |
OLD | NEW |