smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 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 |
Eric Aleshire | 97b368c9 | 2018-10-12 18:09:09 | [diff] [blame] | 9 | import glob |
Eric Aleshire | ce66b9f | 2019-02-08 08:00:31 | [diff] [blame^] | 10 | import logging |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 11 | import os |
Eric Aleshire | 97b368c9 | 2018-10-12 18:09:09 | [diff] [blame] | 12 | import subprocess |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 13 | import unittest |
| 14 | |
| 15 | import test_runner |
| 16 | |
| 17 | |
| 18 | class TestCase(unittest.TestCase): |
| 19 | """Test case which supports installing mocks. Uninstalls on tear down.""" |
| 20 | |
| 21 | def __init__(self, *args, **kwargs): |
| 22 | """Initializes a new instance of this class.""" |
| 23 | super(TestCase, self).__init__(*args, **kwargs) |
| 24 | |
| 25 | # Maps object to a dict which maps names of mocked members to their |
| 26 | # original values. |
| 27 | self._mocks = collections.OrderedDict() |
| 28 | |
| 29 | def mock(self, obj, member, mock): |
| 30 | """Installs mock in place of the named member of the given obj. |
| 31 | |
| 32 | Args: |
| 33 | obj: Any object. |
| 34 | member: String naming the attribute of the object to mock. |
| 35 | mock: The mock to install. |
| 36 | """ |
| 37 | self._mocks.setdefault(obj, collections.OrderedDict()).setdefault( |
| 38 | member, getattr(obj, member)) |
| 39 | setattr(obj, member, mock) |
| 40 | |
| 41 | def tearDown(self, *args, **kwargs): |
| 42 | """Uninstalls mocks.""" |
| 43 | super(TestCase, self).tearDown(*args, **kwargs) |
| 44 | |
| 45 | for obj in self._mocks: |
| 46 | for member, original_value in self._mocks[obj].iteritems(): |
| 47 | setattr(obj, member, original_value) |
| 48 | |
| 49 | |
| 50 | class GetKIFTestFilterTest(TestCase): |
| 51 | """Tests for test_runner.get_kif_test_filter.""" |
| 52 | |
| 53 | def test_correct(self): |
| 54 | """Ensures correctness of filter.""" |
| 55 | tests = [ |
| 56 | 'KIF.test1', |
| 57 | 'KIF.test2', |
| 58 | ] |
| 59 | expected = 'NAME:test1|test2' |
| 60 | |
| 61 | self.assertEqual(test_runner.get_kif_test_filter(tests), expected) |
| 62 | |
| 63 | def test_correct_inverted(self): |
| 64 | """Ensures correctness of inverted filter.""" |
| 65 | tests = [ |
| 66 | 'KIF.test1', |
| 67 | 'KIF.test2', |
| 68 | ] |
| 69 | expected = '-NAME:test1|test2' |
| 70 | |
| 71 | self.assertEqual( |
| 72 | test_runner.get_kif_test_filter(tests, invert=True), expected) |
| 73 | |
| 74 | |
| 75 | class GetGTestFilterTest(TestCase): |
| 76 | """Tests for test_runner.get_gtest_filter.""" |
| 77 | |
| 78 | def test_correct(self): |
| 79 | """Ensures correctness of filter.""" |
| 80 | tests = [ |
| 81 | 'test.1', |
| 82 | 'test.2', |
| 83 | ] |
| 84 | expected = 'test.1:test.2' |
| 85 | |
| 86 | self.assertEqual(test_runner.get_gtest_filter(tests), expected) |
| 87 | |
| 88 | def test_correct_inverted(self): |
| 89 | """Ensures correctness of inverted filter.""" |
| 90 | tests = [ |
| 91 | 'test.1', |
| 92 | 'test.2', |
| 93 | ] |
| 94 | expected = '-test.1:test.2' |
| 95 | |
| 96 | self.assertEqual( |
| 97 | test_runner.get_gtest_filter(tests, invert=True), expected) |
| 98 | |
| 99 | |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 100 | class InstallXcodeTest(TestCase): |
| 101 | """Tests install_xcode.""" |
| 102 | |
| 103 | def setUp(self): |
| 104 | super(InstallXcodeTest, self).setUp() |
| 105 | self.mock(test_runner, 'xcode_select', lambda _: None) |
| 106 | self.mock(os.path, 'exists', lambda _: True) |
| 107 | |
| 108 | def test_success(self): |
| 109 | self.assertTrue(test_runner.install_xcode('test_build', 'true', 'path')) |
| 110 | |
| 111 | def test_failure(self): |
| 112 | self.assertFalse(test_runner.install_xcode('test_build', 'false', 'path')) |
| 113 | |
| 114 | |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 115 | class SimulatorTestRunnerTest(TestCase): |
| 116 | """Tests for test_runner.SimulatorTestRunner.""" |
| 117 | |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 118 | def setUp(self): |
| 119 | super(SimulatorTestRunnerTest, self).setUp() |
| 120 | |
| 121 | def install_xcode(build, mac_toolchain_cmd, xcode_app_path): |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 122 | return True |
| 123 | |
Sergey Berezin | 7f9b4bf | 2018-05-21 19:49:07 | [diff] [blame] | 124 | self.mock(test_runner, 'get_current_xcode_info', lambda: { |
Sergey Berezin | 8263e5f | 2017-11-29 22:51:36 | [diff] [blame] | 125 | 'version': 'test version', 'build': 'test build', 'path': 'test/path'}) |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 126 | self.mock(test_runner, 'install_xcode', install_xcode) |
| 127 | self.mock(test_runner.subprocess, 'check_output', |
| 128 | lambda _: 'fake-bundle-id') |
| 129 | self.mock(os.path, 'abspath', lambda path: '/abs/path/to/%s' % path) |
| 130 | self.mock(os.path, 'exists', lambda _: True) |
Eric Aleshire | f22c7efb | 2019-02-04 23:41:26 | [diff] [blame] | 131 | self.mock(test_runner.TestRunner, 'set_sigterm_handler', |
| 132 | lambda self, handler: 0) |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 133 | |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 134 | def test_app_not_found(self): |
| 135 | """Ensures AppNotFoundError is raised.""" |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 136 | |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 137 | self.mock(os.path, 'exists', lambda p: not p.endswith('fake-app')) |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 138 | |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 139 | with self.assertRaises(test_runner.AppNotFoundError): |
| 140 | test_runner.SimulatorTestRunner( |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 141 | 'fake-app', |
| 142 | 'fake-iossim', |
| 143 | 'platform', |
| 144 | 'os', |
| 145 | 'xcode-version', |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 146 | '', # Empty xcode-build |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 147 | 'out-dir', |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 148 | ) |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 149 | |
| 150 | def test_iossim_not_found(self): |
| 151 | """Ensures SimulatorNotFoundError is raised.""" |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 152 | self.mock(os.path, 'exists', lambda p: not p.endswith('fake-iossim')) |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 153 | |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 154 | with self.assertRaises(test_runner.SimulatorNotFoundError): |
| 155 | test_runner.SimulatorTestRunner( |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 156 | 'fake-app', |
| 157 | 'fake-iossim', |
| 158 | 'platform', |
| 159 | 'os', |
| 160 | 'xcode-version', |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 161 | 'xcode-build', |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 162 | 'out-dir', |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 163 | ) |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 164 | |
| 165 | def test_init(self): |
| 166 | """Ensures instance is created.""" |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 167 | tr = test_runner.SimulatorTestRunner( |
| 168 | 'fake-app', |
| 169 | 'fake-iossim', |
| 170 | 'platform', |
| 171 | 'os', |
| 172 | 'xcode-version', |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 173 | 'xcode-build', |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 174 | 'out-dir', |
| 175 | ) |
| 176 | |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 177 | self.assertTrue(tr) |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 178 | |
| 179 | def test_startup_crash(self): |
| 180 | """Ensures test is relaunched once on startup crash.""" |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 181 | def set_up(self): |
| 182 | return |
| 183 | |
| 184 | @staticmethod |
Menglu Huang | 89909a8e | 2018-02-21 00:06:42 | [diff] [blame] | 185 | def _run(cmd, shards=None): |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 186 | return collections.namedtuple('result', ['crashed', 'crashed_test'])( |
| 187 | crashed=True, crashed_test=None) |
| 188 | |
| 189 | def tear_down(self): |
| 190 | return |
| 191 | |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 192 | self.mock(test_runner.SimulatorTestRunner, 'set_up', set_up) |
| 193 | self.mock(test_runner.TestRunner, '_run', _run) |
| 194 | self.mock(test_runner.SimulatorTestRunner, 'tear_down', tear_down) |
| 195 | |
| 196 | tr = test_runner.SimulatorTestRunner( |
| 197 | 'fake-app', |
| 198 | 'fake-iossim', |
| 199 | 'platform', |
| 200 | 'os', |
| 201 | 'xcode-version', |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 202 | 'xcode-build', |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 203 | 'out-dir', |
| 204 | ) |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 205 | with self.assertRaises(test_runner.AppLaunchError): |
| 206 | tr.launch() |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 207 | |
Menglu Huang | 89909a8e | 2018-02-21 00:06:42 | [diff] [blame] | 208 | def test_run(self): |
| 209 | """Ensures the _run method is correct with test sharding.""" |
| 210 | def shard_xctest(object_path, shards, test_cases=None): |
| 211 | return [['a/1', 'b/2'], ['c/3', 'd/4'], ['e/5']] |
| 212 | |
| 213 | def run_tests(self, test_shard=None): |
| 214 | out = [] |
| 215 | for test in test_shard: |
| 216 | testname = test.split('/') |
| 217 | out.append('Test Case \'-[%s %s]\' started.' % |
| 218 | (testname[0], testname[1])) |
| 219 | out.append('Test Case \'-[%s %s]\' passed (0.1 seconds)' % |
| 220 | (testname[0], testname[1])) |
| 221 | return (out, 0, 0) |
| 222 | |
| 223 | tr = test_runner.SimulatorTestRunner( |
| 224 | 'fake-app', |
| 225 | 'fake-iossim', |
| 226 | 'platform', |
| 227 | 'os', |
| 228 | 'xcode-version', |
| 229 | 'xcode-build', |
| 230 | 'out-dir', |
| 231 | ) |
| 232 | self.mock(test_runner, 'shard_xctest', shard_xctest) |
| 233 | self.mock(test_runner.SimulatorTestRunner, 'run_tests', run_tests) |
| 234 | |
| 235 | tr.xctest_path = 'fake.xctest' |
| 236 | cmd = tr.get_launch_command() |
| 237 | result = tr._run(cmd=cmd, shards=3) |
| 238 | self.assertIn('a/1', result.passed_tests) |
| 239 | self.assertIn('b/2', result.passed_tests) |
| 240 | self.assertIn('c/3', result.passed_tests) |
| 241 | self.assertIn('d/4', result.passed_tests) |
| 242 | self.assertIn('e/5', result.passed_tests) |
| 243 | |
Menglu Huang | eb4d757 | 2018-05-21 18:37:58 | [diff] [blame] | 244 | def test_run_with_system_alert(self): |
| 245 | """Ensures SystemAlertPresentError is raised when warning 'System alert |
| 246 | view is present, so skipping all tests' is in the output.""" |
| 247 | with self.assertRaises(test_runner.SystemAlertPresentError): |
| 248 | tr = test_runner.SimulatorTestRunner( |
| 249 | 'fake-app', |
| 250 | 'fake-iossim', |
| 251 | 'platform', |
| 252 | 'os', |
| 253 | 'xcode-version', |
| 254 | 'xcode-build', |
| 255 | 'out-dir', |
| 256 | ) |
| 257 | tr.xctest_path = 'fake.xctest' |
| 258 | cmd = ['echo', 'System alert view is present, so skipping all tests!'] |
| 259 | result = tr._run(cmd=cmd) |
| 260 | |
Menglu Huang | 0f56961 | 2017-12-08 06:35:07 | [diff] [blame] | 261 | def test_get_launch_command(self): |
Menglu Huang | 89909a8e | 2018-02-21 00:06:42 | [diff] [blame] | 262 | """Ensures launch command is correct with test_filters, test sharding and |
| 263 | test_cases.""" |
Menglu Huang | 0f56961 | 2017-12-08 06:35:07 | [diff] [blame] | 264 | tr = test_runner.SimulatorTestRunner( |
| 265 | 'fake-app', |
| 266 | 'fake-iossim', |
| 267 | 'platform', |
| 268 | 'os', |
| 269 | 'xcode-version', |
| 270 | 'xcode-build', |
| 271 | 'out-dir', |
| 272 | ) |
| 273 | tr.xctest_path = 'fake.xctest' |
| 274 | # Cases test_filter is not empty, with empty/non-empty self.test_cases. |
| 275 | tr.test_cases = [] |
| 276 | cmd = tr.get_launch_command(['a'], invert=False) |
| 277 | self.assertIn('-t', cmd) |
| 278 | self.assertIn('a', cmd) |
| 279 | |
| 280 | tr.test_cases = ['a', 'b'] |
| 281 | cmd = tr.get_launch_command(['a'], invert=False) |
| 282 | self.assertIn('-t', cmd) |
| 283 | self.assertIn('a', cmd) |
| 284 | self.assertNotIn('b', cmd) |
| 285 | |
| 286 | # Cases test_filter is empty, with empty/non-empty self.test_cases. |
| 287 | tr.test_cases = [] |
| 288 | cmd = tr.get_launch_command(test_filter=None, invert=False) |
| 289 | self.assertNotIn('-t', cmd) |
| 290 | |
| 291 | tr.test_cases = ['a', 'b'] |
| 292 | cmd = tr.get_launch_command(test_filter=None, invert=False) |
| 293 | self.assertIn('-t', cmd) |
| 294 | self.assertIn('a', cmd) |
| 295 | self.assertIn('b', cmd) |
| 296 | |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 297 | def test_relaunch(self): |
| 298 | """Ensures test is relaunched on test crash until tests complete.""" |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 299 | def set_up(self): |
| 300 | return |
| 301 | |
| 302 | @staticmethod |
Menglu Huang | 89909a8e | 2018-02-21 00:06:42 | [diff] [blame] | 303 | def _run(cmd, shards=None): |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 304 | result = collections.namedtuple( |
| 305 | 'result', [ |
| 306 | 'crashed', |
| 307 | 'crashed_test', |
| 308 | 'failed_tests', |
| 309 | 'flaked_tests', |
| 310 | 'passed_tests', |
| 311 | ], |
| 312 | ) |
Menglu Huang | 89909a8e | 2018-02-21 00:06:42 | [diff] [blame] | 313 | if '-e' not in cmd: |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 314 | # First run, has no test filter supplied. Mock a crash. |
| 315 | return result( |
| 316 | crashed=True, |
| 317 | crashed_test='c', |
| 318 | failed_tests={'b': ['b-out'], 'c': ['Did not complete.']}, |
| 319 | flaked_tests={'d': ['d-out']}, |
| 320 | passed_tests=['a'], |
| 321 | ) |
| 322 | else: |
| 323 | return result( |
| 324 | crashed=False, |
| 325 | crashed_test=None, |
| 326 | failed_tests={}, |
| 327 | flaked_tests={}, |
| 328 | passed_tests=[], |
| 329 | ) |
| 330 | |
| 331 | def tear_down(self): |
| 332 | return |
| 333 | |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 334 | self.mock(test_runner.SimulatorTestRunner, 'set_up', set_up) |
| 335 | self.mock(test_runner.TestRunner, '_run', _run) |
| 336 | self.mock(test_runner.SimulatorTestRunner, 'tear_down', tear_down) |
| 337 | |
| 338 | tr = test_runner.SimulatorTestRunner( |
| 339 | 'fake-app', |
| 340 | 'fake-iossim', |
| 341 | 'platform', |
| 342 | 'os', |
| 343 | 'xcode-version', |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 344 | 'xcode-build', |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 345 | 'out-dir', |
| 346 | ) |
| 347 | tr.launch() |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 348 | self.assertTrue(tr.logs) |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 349 | |
| 350 | |
Eric Aleshire | 97b368c9 | 2018-10-12 18:09:09 | [diff] [blame] | 351 | class WprProxySimulatorTestRunnerTest(TestCase): |
| 352 | """Tests for test_runner.WprProxySimulatorTestRunner.""" |
| 353 | |
| 354 | def setUp(self): |
| 355 | super(WprProxySimulatorTestRunnerTest, self).setUp() |
| 356 | |
| 357 | def install_xcode(build, mac_toolchain_cmd, xcode_app_path): |
| 358 | return True |
| 359 | |
| 360 | self.mock(test_runner, 'get_current_xcode_info', lambda: { |
| 361 | 'version': 'test version', 'build': 'test build', 'path': 'test/path'}) |
| 362 | self.mock(test_runner, 'install_xcode', install_xcode) |
| 363 | self.mock(test_runner.subprocess, 'check_output', |
| 364 | lambda _: 'fake-bundle-id') |
| 365 | self.mock(os.path, 'abspath', lambda path: '/abs/path/to/%s' % path) |
| 366 | self.mock(os.path, 'exists', lambda _: True) |
Eric Aleshire | f22c7efb | 2019-02-04 23:41:26 | [diff] [blame] | 367 | self.mock(test_runner.TestRunner, 'set_sigterm_handler', |
| 368 | lambda self, handler: 0) |
| 369 | self.mock(test_runner.SimulatorTestRunner, 'getSimulator', |
| 370 | lambda _: 'fake-id') |
| 371 | self.mock(test_runner.SimulatorTestRunner, 'deleteSimulator', |
| 372 | lambda a, b: True) |
| 373 | self.mock(test_runner.WprProxySimulatorTestRunner, |
| 374 | 'copy_trusted_certificate', lambda a, b: True) |
Eric Aleshire | 97b368c9 | 2018-10-12 18:09:09 | [diff] [blame] | 375 | |
| 376 | def test_replay_path_not_found(self): |
| 377 | """Ensures ReplayPathNotFoundError is raised.""" |
| 378 | |
| 379 | self.mock(os.path, 'exists', lambda p: not p.endswith('bad-replay-path')) |
| 380 | |
| 381 | with self.assertRaises(test_runner.ReplayPathNotFoundError): |
| 382 | test_runner.WprProxySimulatorTestRunner( |
| 383 | 'fake-app', |
| 384 | 'fake-iossim', |
| 385 | 'bad-replay-path', |
| 386 | 'platform', |
| 387 | 'os', |
| 388 | 'wpr-tools-path', |
| 389 | 'xcode-version', |
| 390 | 'xcode-build', |
| 391 | 'out-dir', |
| 392 | ) |
| 393 | |
| 394 | def test_wpr_tools_not_found(self): |
| 395 | """Ensures WprToolsNotFoundError is raised.""" |
| 396 | |
| 397 | self.mock(os.path, 'exists', lambda p: not p.endswith('bad-tools-path')) |
| 398 | |
| 399 | with self.assertRaises(test_runner.WprToolsNotFoundError): |
| 400 | test_runner.WprProxySimulatorTestRunner( |
| 401 | 'fake-app', |
| 402 | 'fake-iossim', |
| 403 | 'replay-path', |
| 404 | 'platform', |
| 405 | 'os', |
| 406 | 'bad-tools-path', |
| 407 | 'xcode-version', |
| 408 | 'xcode-build', |
| 409 | 'out-dir', |
| 410 | ) |
| 411 | |
| 412 | def test_init(self): |
| 413 | """Ensures instance is created.""" |
| 414 | tr = test_runner.WprProxySimulatorTestRunner( |
| 415 | 'fake-app', |
| 416 | 'fake-iossim', |
| 417 | 'replay-path', |
| 418 | 'platform', |
| 419 | 'os', |
| 420 | 'wpr-tools-path', |
| 421 | 'xcode-version', |
| 422 | 'xcode-build', |
| 423 | 'out-dir', |
| 424 | ) |
| 425 | |
| 426 | self.assertTrue(tr) |
| 427 | |
Eric Aleshire | 3f8c995 | 2018-12-12 21:57:43 | [diff] [blame] | 428 | def run_wpr_test(self, test_filter=[], invert=False): |
| 429 | """Wrapper that mocks the _run method and returns its result.""" |
Eric Aleshire | 97b368c9 | 2018-10-12 18:09:09 | [diff] [blame] | 430 | class FakeStdout: |
| 431 | def __init__(self): |
| 432 | self.line_index = 0 |
| 433 | self.lines = [ |
| 434 | 'Test Case \'-[a 1]\' started.', |
| 435 | 'Test Case \'-[a 1]\' has uninteresting logs.', |
| 436 | 'Test Case \'-[a 1]\' passed (0.1 seconds)', |
| 437 | 'Test Case \'-[b 2]\' started.', |
| 438 | 'Test Case \'-[b 2]\' passed (0.1 seconds)', |
| 439 | 'Test Case \'-[c 3]\' started.', |
| 440 | 'Test Case \'-[c 3]\' has interesting failure info.', |
| 441 | 'Test Case \'-[c 3]\' failed (0.1 seconds)', |
| 442 | ] |
| 443 | |
| 444 | def readline(self): |
| 445 | if self.line_index < len(self.lines): |
| 446 | return_line = self.lines[self.line_index] |
| 447 | self.line_index += 1 |
| 448 | return return_line |
| 449 | else: |
| 450 | return None |
| 451 | |
| 452 | class FakeProcess: |
| 453 | def __init__(self): |
| 454 | self.stdout = FakeStdout() |
| 455 | self.returncode = 0 |
| 456 | |
| 457 | def stdout(self): |
| 458 | return self.stdout |
| 459 | |
| 460 | def wait(self): |
| 461 | return |
| 462 | |
| 463 | def popen(recipe_cmd, env, stdout, stderr): |
| 464 | return FakeProcess() |
| 465 | |
| 466 | tr = test_runner.WprProxySimulatorTestRunner( |
| 467 | 'fake-app', |
| 468 | 'fake-iossim', |
| 469 | 'replay-path', |
| 470 | 'platform', |
| 471 | 'os', |
| 472 | 'wpr-tools-path', |
| 473 | 'xcode-version', |
| 474 | 'xcode-build', |
| 475 | 'out-dir', |
| 476 | ) |
Eric Aleshire | f22c7efb | 2019-02-04 23:41:26 | [diff] [blame] | 477 | self.mock(test_runner.WprProxySimulatorTestRunner, 'wprgo_start', |
| 478 | lambda a,b: None) |
| 479 | self.mock(test_runner.WprProxySimulatorTestRunner, 'wprgo_stop', |
| 480 | lambda _: None) |
Eric Aleshire | 97b368c9 | 2018-10-12 18:09:09 | [diff] [blame] | 481 | |
| 482 | self.mock(os.path, 'isfile', lambda _: True) |
| 483 | self.mock(glob, 'glob', lambda _: ["file1", "file2"]) |
| 484 | self.mock(subprocess, 'Popen', popen) |
| 485 | |
| 486 | tr.xctest_path = 'fake.xctest' |
Eric Aleshire | 3f8c995 | 2018-12-12 21:57:43 | [diff] [blame] | 487 | cmd = tr.get_launch_command(test_filter=test_filter, invert=invert) |
| 488 | return tr._run(cmd=cmd, shards=1) |
| 489 | |
| 490 | def test_run_no_filter(self): |
| 491 | """Ensures the _run method can handle passed and failed tests.""" |
| 492 | result = self.run_wpr_test() |
Eric Aleshire | 97b368c9 | 2018-10-12 18:09:09 | [diff] [blame] | 493 | self.assertIn('file1.a/1', result.passed_tests) |
| 494 | self.assertIn('file1.b/2', result.passed_tests) |
| 495 | self.assertIn('file1.c/3', result.failed_tests) |
| 496 | self.assertIn('file2.a/1', result.passed_tests) |
| 497 | self.assertIn('file2.b/2', result.passed_tests) |
| 498 | self.assertIn('file2.c/3', result.failed_tests) |
| 499 | |
Eric Aleshire | 3f8c995 | 2018-12-12 21:57:43 | [diff] [blame] | 500 | def test_run_with_filter(self): |
| 501 | """Ensures the _run method works with a filter.""" |
| 502 | result = self.run_wpr_test(test_filter=["file1"], invert=False) |
| 503 | self.assertIn('file1.a/1', result.passed_tests) |
| 504 | self.assertIn('file1.b/2', result.passed_tests) |
| 505 | self.assertIn('file1.c/3', result.failed_tests) |
| 506 | self.assertNotIn('file2.a/1', result.passed_tests) |
| 507 | self.assertNotIn('file2.b/2', result.passed_tests) |
| 508 | self.assertNotIn('file2.c/3', result.failed_tests) |
| 509 | |
| 510 | def test_run_with_inverted_filter(self): |
| 511 | """Ensures the _run method works with an inverted filter.""" |
| 512 | result = self.run_wpr_test(test_filter=["file1"], invert=True) |
| 513 | self.assertNotIn('file1.a/1', result.passed_tests) |
| 514 | self.assertNotIn('file1.b/2', result.passed_tests) |
| 515 | self.assertNotIn('file1.c/3', result.failed_tests) |
| 516 | self.assertIn('file2.a/1', result.passed_tests) |
| 517 | self.assertIn('file2.b/2', result.passed_tests) |
| 518 | self.assertIn('file2.c/3', result.failed_tests) |
Eric Aleshire | 97b368c9 | 2018-10-12 18:09:09 | [diff] [blame] | 519 | |
Menglu Huang | 0f56961 | 2017-12-08 06:35:07 | [diff] [blame] | 520 | class DeviceTestRunnerTest(TestCase): |
| 521 | def setUp(self): |
| 522 | super(DeviceTestRunnerTest, self).setUp() |
| 523 | |
| 524 | def install_xcode(build, mac_toolchain_cmd, xcode_app_path): |
| 525 | return True |
| 526 | |
Sergey Berezin | 7f9b4bf | 2018-05-21 19:49:07 | [diff] [blame] | 527 | self.mock(test_runner, 'get_current_xcode_info', lambda: { |
Menglu Huang | 0f56961 | 2017-12-08 06:35:07 | [diff] [blame] | 528 | 'version': 'test version', 'build': 'test build', 'path': 'test/path'}) |
| 529 | self.mock(test_runner, 'install_xcode', install_xcode) |
| 530 | self.mock(test_runner.subprocess, 'check_output', |
| 531 | lambda _: 'fake-bundle-id') |
| 532 | self.mock(os.path, 'abspath', lambda path: '/abs/path/to/%s' % path) |
| 533 | self.mock(os.path, 'exists', lambda _: True) |
| 534 | |
| 535 | self.tr = test_runner.DeviceTestRunner( |
| 536 | 'fake-app', |
| 537 | 'xcode-version', |
| 538 | 'xcode-build', |
| 539 | 'out-dir', |
| 540 | ) |
| 541 | self.tr.xctestrun_data = {'TestTargetName':{}} |
| 542 | |
| 543 | def test_with_test_filter_without_test_cases(self): |
| 544 | """Ensures tests in the run with test_filter and no test_cases.""" |
| 545 | self.tr.set_xctest_filters(['a', 'b'], invert=False) |
| 546 | self.assertEqual( |
| 547 | self.tr.xctestrun_data['TestTargetName']['OnlyTestIdentifiers'], |
| 548 | ['a', 'b'] |
| 549 | ) |
| 550 | |
| 551 | def test_invert_with_test_filter_without_test_cases(self): |
| 552 | """Ensures tests in the run invert with test_filter and no test_cases.""" |
| 553 | self.tr.set_xctest_filters(['a', 'b'], invert=True) |
| 554 | self.assertEqual( |
| 555 | self.tr.xctestrun_data['TestTargetName']['SkipTestIdentifiers'], |
| 556 | ['a', 'b'] |
| 557 | ) |
| 558 | |
| 559 | def test_with_test_filter_with_test_cases(self): |
| 560 | """Ensures tests in the run with test_filter and test_cases.""" |
| 561 | self.tr.test_cases = ['a', 'b', 'c', 'd'] |
| 562 | self.tr.set_xctest_filters(['a', 'b', 'irrelevant test'], invert=False) |
| 563 | self.assertEqual( |
| 564 | self.tr.xctestrun_data['TestTargetName']['OnlyTestIdentifiers'], |
| 565 | ['a', 'b'] |
| 566 | ) |
| 567 | |
| 568 | def test_invert_with_test_filter_with_test_cases(self): |
| 569 | """Ensures tests in the run invert with test_filter and test_cases.""" |
| 570 | self.tr.test_cases = ['a', 'b', 'c', 'd'] |
| 571 | self.tr.set_xctest_filters(['a', 'b', 'irrelevant test'], invert=True) |
| 572 | self.assertEqual( |
| 573 | self.tr.xctestrun_data['TestTargetName']['OnlyTestIdentifiers'], |
| 574 | ['c', 'd'] |
| 575 | ) |
| 576 | |
| 577 | def test_without_test_filter_without_test_cases(self): |
| 578 | """Ensures tests in the run with no test_filter and no test_cases.""" |
| 579 | self.tr.set_xctest_filters(test_filter=None, invert=False) |
| 580 | self.assertIsNone( |
| 581 | self.tr.xctestrun_data['TestTargetName'].get('OnlyTestIdentifiers')) |
| 582 | |
| 583 | def test_invert_without_test_filter_without_test_cases(self): |
| 584 | """Ensures tests in the run invert with no test_filter and no test_cases.""" |
| 585 | self.tr.set_xctest_filters(test_filter=None, invert=True) |
| 586 | self.assertIsNone( |
| 587 | self.tr.xctestrun_data['TestTargetName'].get('OnlyTestIdentifiers')) |
| 588 | |
| 589 | def test_without_test_filter_with_test_cases(self): |
| 590 | """Ensures tests in the run with no test_filter but test_cases.""" |
| 591 | self.tr.test_cases = ['a', 'b', 'c', 'd'] |
| 592 | self.tr.set_xctest_filters(test_filter=None, invert=False) |
| 593 | self.assertEqual( |
| 594 | self.tr.xctestrun_data['TestTargetName']['OnlyTestIdentifiers'], |
| 595 | ['a', 'b', 'c', 'd'] |
| 596 | ) |
| 597 | |
| 598 | def test_invert_without_test_filter_with_test_cases(self): |
| 599 | """Ensures tests in the run invert with no test_filter but test_cases.""" |
| 600 | self.tr.test_cases = ['a', 'b', 'c', 'd'] |
| 601 | self.tr.set_xctest_filters(test_filter=None, invert=True) |
| 602 | self.assertEqual( |
| 603 | self.tr.xctestrun_data['TestTargetName']['OnlyTestIdentifiers'], |
| 604 | ['a', 'b', 'c', 'd'] |
| 605 | ) |
| 606 | |
| 607 | |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 608 | if __name__ == '__main__': |
Eric Aleshire | ce66b9f | 2019-02-08 08:00:31 | [diff] [blame^] | 609 | logging.basicConfig(format='[%(asctime)s:%(levelname)s] %(message)s', |
| 610 | level=logging.DEBUG, datefmt='%I:%M:%S') |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 611 | unittest.main() |