| OLD | NEW |
| 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2013 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 """Dispatches the instrumentation tests.""" | 5 """Dispatches the instrumentation tests.""" |
| 6 | 6 |
| 7 import logging | 7 import logging |
| 8 import os | 8 import os |
| 9 | 9 |
| 10 from pylib import android_commands | 10 from pylib import android_commands |
| 11 from pylib.base import base_test_result | 11 from pylib.base import base_test_result |
| 12 from pylib.base import shard | 12 from pylib.base import shard |
| 13 from pylib.uiautomator import test_package as uiautomator_package | |
| 14 | 13 |
| 15 import test_package | 14 import test_package |
| 16 import test_runner | 15 import test_runner |
| 17 | 16 |
| 18 | 17 |
| 19 def Dispatch(options): | 18 def Dispatch(options): |
| 20 """Dispatches instrumentation tests onto connected device(s). | 19 """Dispatches instrumentation tests onto connected device(s). |
| 21 | 20 |
| 22 If possible, this method will attempt to shard the tests to | 21 If possible, this method will attempt to shard the tests to |
| 23 all connected devices. Otherwise, dispatch and run tests on one device. | 22 all connected devices. Otherwise, dispatch and run tests on one device. |
| 24 | 23 |
| 25 Args: | 24 Args: |
| 26 options: Command line options. | 25 options: Command line options. |
| 27 | 26 |
| 28 Returns: | 27 Returns: |
| 29 A TestRunResults object holding the results of the Java tests. | 28 A TestRunResults object holding the results of the Java tests. |
| 30 | 29 |
| 31 Raises: | 30 Raises: |
| 32 Exception: when there are no attached devices. | 31 Exception: when there are no attached devices. |
| 33 """ | 32 """ |
| 34 is_uiautomator_test = False | 33 test_pkg = test_package.TestPackage(options.test_apk_path, |
| 35 if hasattr(options, 'uiautomator_jar'): | 34 options.test_apk_jar_path) |
| 36 test_pkg = uiautomator_package.TestPackage( | 35 tests = test_pkg._GetAllMatchingTests( |
| 37 options.uiautomator_jar, options.uiautomator_info_jar) | 36 options.annotations, options.test_filter) |
| 38 is_uiautomator_test = True | |
| 39 else: | |
| 40 test_pkg = test_package.TestPackage(options.test_apk_path, | |
| 41 options.test_apk_jar_path) | |
| 42 # The default annotation for tests which do not have any sizes annotation. | |
| 43 default_size_annotation = 'SmallTest' | |
| 44 | |
| 45 def _GetTestsMissingAnnotation(test_pkg): | |
| 46 test_size_annotations = frozenset(['Smoke', 'SmallTest', 'MediumTest', | |
| 47 'LargeTest', 'EnormousTest', 'FlakyTest', | |
| 48 'DisabledTest', 'Manual', 'PerfTest']) | |
| 49 tests_missing_annotations = [] | |
| 50 for test_method in test_pkg.GetTestMethods(): | |
| 51 annotations = frozenset(test_pkg.GetTestAnnotations(test_method)) | |
| 52 if (annotations.isdisjoint(test_size_annotations) and | |
| 53 not test_pkg.IsPythonDrivenTest(test_method)): | |
| 54 tests_missing_annotations.append(test_method) | |
| 55 return sorted(tests_missing_annotations) | |
| 56 | |
| 57 if options.annotation: | |
| 58 available_tests = test_pkg.GetAnnotatedTests(options.annotation) | |
| 59 if options.annotation.count(default_size_annotation) > 0: | |
| 60 tests_missing_annotations = _GetTestsMissingAnnotation(test_pkg) | |
| 61 if tests_missing_annotations: | |
| 62 logging.warning('The following tests do not contain any annotation. ' | |
| 63 'Assuming "%s":\n%s', | |
| 64 default_size_annotation, | |
| 65 '\n'.join(tests_missing_annotations)) | |
| 66 available_tests += tests_missing_annotations | |
| 67 else: | |
| 68 available_tests = [m for m in test_pkg.GetTestMethods() | |
| 69 if not test_pkg.IsPythonDrivenTest(m)] | |
| 70 | |
| 71 tests = [] | |
| 72 if options.test_filter: | |
| 73 # |available_tests| are in adb instrument format: package.path.class#test. | |
| 74 filter_without_hash = options.test_filter.replace('#', '.') | |
| 75 tests = [t for t in available_tests | |
| 76 if filter_without_hash in t.replace('#', '.')] | |
| 77 else: | |
| 78 tests = available_tests | |
| 79 | |
| 80 if not tests: | 37 if not tests: |
| 81 logging.warning('No instrumentation tests to run with current args.') | 38 logging.warning('No instrumentation tests to run with current args.') |
| 82 return base_test_result.TestRunResults() | 39 return base_test_result.TestRunResults() |
| 83 | 40 |
| 84 tests *= options.number_of_runs | |
| 85 | |
| 86 attached_devices = android_commands.GetAttachedDevices() | 41 attached_devices = android_commands.GetAttachedDevices() |
| 87 | |
| 88 if not attached_devices: | 42 if not attached_devices: |
| 89 raise Exception('There are no devices online.') | 43 raise Exception('There are no devices online.') |
| 44 |
| 90 if options.device: | 45 if options.device: |
| 46 assert options.device in attached_devices |
| 91 attached_devices = [options.device] | 47 attached_devices = [options.device] |
| 92 | 48 |
| 93 logging.info('Will run: %s', str(tests)) | |
| 94 | |
| 95 if len(attached_devices) > 1 and options.wait_for_debugger: | 49 if len(attached_devices) > 1 and options.wait_for_debugger: |
| 96 logging.warning('Debugger can not be sharded, using first available device') | 50 logging.warning('Debugger can not be sharded, using first available device') |
| 97 attached_devices = attached_devices[:1] | 51 attached_devices = attached_devices[:1] |
| 98 | 52 |
| 99 def TestRunnerFactory(device, shard_index): | 53 def TestRunnerFactory(device, shard_index): |
| 100 return test_runner.TestRunner( | 54 return test_runner.TestRunner( |
| 101 options, device, shard_index, test_pkg, [], is_uiautomator_test) | 55 options, device, shard_index, test_pkg, []) |
| 102 | 56 |
| 103 return shard.ShardAndRunTests(TestRunnerFactory, attached_devices, tests, | 57 return shard.ShardAndRunTests(TestRunnerFactory, attached_devices, tests, |
| 104 options.build_type) | 58 options.build_type) |
| OLD | NEW |