blob: e92f8514209f2b78e3bfe055b44d32d6ed8161c1 [file] [log] [blame]
[email protected]fbe29322013-07-09 09:03:261#!/usr/bin/env python
2#
3# Copyright 2013 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
[email protected]181a5c92013-09-06 17:11:467"""Runs all types of tests from one unified interface."""
[email protected]fbe29322013-07-09 09:03:268
jama47ca85c2014-12-03 18:38:079import argparse
[email protected]fbe29322013-07-09 09:03:2610import collections
[email protected]f7148dd42013-08-20 14:24:5711import logging
[email protected]fbe29322013-07-09 09:03:2612import os
[email protected]6bc1bda22013-07-19 22:08:3713import shutil
[email protected]83bb8152013-11-19 15:02:2114import signal
[email protected]fbe29322013-07-09 09:03:2615import sys
[email protected]83bb8152013-11-19 15:02:2116import threading
jbudorick256fd532014-10-24 01:50:1317import unittest
[email protected]fbe29322013-07-09 09:03:2618
[email protected]f7148dd42013-08-20 14:24:5719from pylib import android_commands
[email protected]fbe29322013-07-09 09:03:2620from pylib import constants
[email protected]c0662e092013-11-12 11:51:2521from pylib import forwarder
[email protected]fbe29322013-07-09 09:03:2622from pylib import ports
23from pylib.base import base_test_result
jbudorick66dc3722014-11-06 21:33:5124from pylib.base import environment_factory
[email protected]6bc1bda22013-07-19 22:08:3725from pylib.base import test_dispatcher
jbudorick66dc3722014-11-06 21:33:5126from pylib.base import test_instance_factory
27from pylib.base import test_run_factory
[email protected]6bc1bda22013-07-19 22:08:3728from pylib.gtest import gtest_config
[email protected]2a684222013-08-01 16:59:2229from pylib.gtest import setup as gtest_setup
30from pylib.gtest import test_options as gtest_test_options
[email protected]6b6abac6d2013-10-03 11:56:3831from pylib.linker import setup as linker_setup
[email protected]37ee0c792013-08-06 19:10:1332from pylib.host_driven import setup as host_driven_setup
[email protected]6bc1bda22013-07-19 22:08:3733from pylib.instrumentation import setup as instrumentation_setup
[email protected]2a684222013-08-01 16:59:2234from pylib.instrumentation import test_options as instrumentation_test_options
jbudorick9a6b7b332014-09-20 00:01:0735from pylib.junit import setup as junit_setup
36from pylib.junit import test_dispatcher as junit_dispatcher
[email protected]3dbdfa42013-08-08 01:08:1437from pylib.monkey import setup as monkey_setup
38from pylib.monkey import test_options as monkey_test_options
[email protected]ec3170b2013-08-14 14:39:4739from pylib.perf import setup as perf_setup
40from pylib.perf import test_options as perf_test_options
41from pylib.perf import test_runner as perf_test_runner
jbudorickb8c42072014-12-01 18:07:5442from pylib.results import json_results
43from pylib.results import report_results
[email protected]6bc1bda22013-07-19 22:08:3744from pylib.uiautomator import setup as uiautomator_setup
[email protected]2a684222013-08-01 16:59:2245from pylib.uiautomator import test_options as uiautomator_test_options
[email protected]2eea4872014-07-28 23:06:1746from pylib.utils import apk_helper
[email protected]71aec4b2013-11-20 00:35:2447from pylib.utils import reraiser_thread
[email protected]6bc1bda22013-07-19 22:08:3748from pylib.utils import run_tests_helper
[email protected]fbe29322013-07-09 09:03:2649
50
jama47ca85c2014-12-03 18:38:0751def AddCommonOptions(parser):
52 """Adds all common options to |parser|."""
[email protected]fbe29322013-07-09 09:03:2653
jama47ca85c2014-12-03 18:38:0754 group = parser.add_argument_group('Common Options')
55
[email protected]dfffbcbc2013-09-17 22:06:0156 default_build_type = os.environ.get('BUILDTYPE', 'Debug')
jama47ca85c2014-12-03 18:38:0757
58 debug_or_release_group = group.add_mutually_exclusive_group()
59 debug_or_release_group.add_argument(
60 '--debug', action='store_const', const='Debug', dest='build_type',
61 default=default_build_type,
62 help=('If set, run test suites under out/Debug. '
63 'Default is env var BUILDTYPE or Debug.'))
64 debug_or_release_group.add_argument(
65 '--release', action='store_const', const='Release', dest='build_type',
66 help=('If set, run test suites under out/Release. '
67 'Default is env var BUILDTYPE or Debug.'))
68
69 group.add_argument('--build-directory', dest='build_directory',
70 help=('Path to the directory in which build files are'
71 ' located (should not include build type)'))
72 group.add_argument('--output-directory', dest='output_directory',
73 help=('Path to the directory in which build files are'
74 ' located (must include build type). This will take'
75 ' precedence over --debug, --release and'
76 ' --build-directory'))
77 group.add_argument('--num_retries', dest='num_retries', type=int, default=2,
78 help=('Number of retries for a test before '
79 'giving up (default: %(default)s).'))
80 group.add_argument('-v',
81 '--verbose',
82 dest='verbose_count',
83 default=0,
84 action='count',
85 help='Verbose level (multiple times for more)')
86 group.add_argument('--flakiness-dashboard-server',
87 dest='flakiness_dashboard_server',
88 help=('Address of the server that is hosting the '
89 'Chrome for Android flakiness dashboard.'))
90 group.add_argument('--enable-platform-mode', action='store_true',
91 help=('Run the test scripts in platform mode, which '
92 'conceptually separates the test runner from the '
93 '"device" (local or remote, real or emulated) on '
94 'which the tests are running. [experimental]'))
95 group.add_argument('-e', '--environment', default='local',
96 choices=constants.VALID_ENVIRONMENTS,
97 help='Test environment to run in (default: %(default)s).')
98 group.add_argument('--adb-path',
99 help=('Specify the absolute path of the adb binary that '
100 'should be used.'))
101 group.add_argument('--json-results-file', dest='json_results_file',
102 help='If set, will dump results in JSON form '
103 'to specified file.')
[email protected]fbe29322013-07-09 09:03:26104
105
jama47ca85c2014-12-03 18:38:07106def ProcessCommonOptions(args):
[email protected]fbe29322013-07-09 09:03:26107 """Processes and handles all common options."""
jama47ca85c2014-12-03 18:38:07108 run_tests_helper.SetLogLevel(args.verbose_count)
109 constants.SetBuildType(args.build_type)
110 if args.build_directory:
111 constants.SetBuildDirectory(args.build_directory)
112 if args.output_directory:
113 constants.SetOutputDirectort(args.output_directory)
114 if args.adb_path:
115 constants.SetAdbPath(args.adb_path)
mikecase48e16bf2014-11-19 22:46:45116 # Some things such as Forwarder require ADB to be in the environment path.
117 adb_dir = os.path.dirname(constants.GetAdbPath())
118 if adb_dir and adb_dir not in os.environ['PATH'].split(os.pathsep):
119 os.environ['PATH'] = adb_dir + os.pathsep + os.environ['PATH']
[email protected]fbe29322013-07-09 09:03:26120
121
rnephew5c499782014-12-12 19:08:55122def AddRemoteDeviceOptions(parser):
123 group = parser.add_argument_group('Remote Device Options')
124
125 group.add_argument('--trigger', default='',
jbudoricke6c560152015-01-13 23:49:28126 help=('Only triggers the test if set. Stores test_run_id '
127 'in given file path. '))
rnephew5c499782014-12-12 19:08:55128 group.add_argument('--collect', default='',
jbudoricke6c560152015-01-13 23:49:28129 help=('Only collects the test results if set. '
130 'Gets test_run_id from given file path.'))
rnephewb74dd0632015-01-07 19:34:53131 group.add_argument('--remote-device', default='',
jbudoricke6c560152015-01-13 23:49:28132 help='Device type to run test on.')
rnephewb74dd0632015-01-07 19:34:53133 group.add_argument('--remote-device-os', default='',
jbudoricke6c560152015-01-13 23:49:28134 help='OS to have on the device.')
rnephew5c499782014-12-12 19:08:55135 group.add_argument('--results-path', default='',
jbudoricke6c560152015-01-13 23:49:28136 help='File path to download results to.')
rnephew7f1e2052014-12-12 23:00:11137 group.add_argument('--api-protocol',
jbudoricke6c560152015-01-13 23:49:28138 help='HTTP protocol to use. (http or https)')
139 group.add_argument('--api-address', help='Address to send HTTP requests.')
140 group.add_argument('--api-port', help='Port to send HTTP requests to.')
rnephew5c499782014-12-12 19:08:55141 group.add_argument('--runner-type', default='',
jbudoricke6c560152015-01-13 23:49:28142 help='Type of test to run as.')
rnephew5c499782014-12-12 19:08:55143 group.add_argument('--runner-package', default='',
jbudoricke6c560152015-01-13 23:49:28144 help='Package name of test.')
rnephew5c499782014-12-12 19:08:55145 group.add_argument('--apk-under-test', default='apks/Chrome.apk',
jbudoricke6c560152015-01-13 23:49:28146 help='APK to run tests on.')
rnephew5c499782014-12-12 19:08:55147
148 api_secret_group = group.add_mutually_exclusive_group()
149 api_secret_group.add_argument('--api-secret', default='',
jbudoricke6c560152015-01-13 23:49:28150 help='API secret for remote devices.')
rnephew5c499782014-12-12 19:08:55151 api_secret_group.add_argument('--api-secret-file', default='',
jbudoricke6c560152015-01-13 23:49:28152 help='Path to file that contains API secret.')
rnephew5c499782014-12-12 19:08:55153
154 api_key_group = group.add_mutually_exclusive_group()
155 api_key_group.add_argument('--api-key', default='',
jbudoricke6c560152015-01-13 23:49:28156 help='API key for remote devices.')
rnephew5c499782014-12-12 19:08:55157 api_key_group.add_argument('--api-key-file', default='',
jbudoricke6c560152015-01-13 23:49:28158 help='Path to file that contains API key.')
rnephew5c499782014-12-12 19:08:55159
160
jama47ca85c2014-12-03 18:38:07161def AddDeviceOptions(parser):
162 """Adds device options to |parser|."""
163 group = parser.add_argument_group(title='Device Options')
164 group.add_argument('-c', dest='cleanup_test_files',
165 help='Cleanup test files on the device after run',
166 action='store_true')
167 group.add_argument('--tool',
168 dest='tool',
169 help=('Run the test under a tool '
170 '(use --tool help to list them)'))
171 group.add_argument('-d', '--device', dest='test_device',
172 help=('Target device for the test suite '
173 'to run on.'))
jbudorick256fd532014-10-24 01:50:13174
175
jama47ca85c2014-12-03 18:38:07176def AddGTestOptions(parser):
177 """Adds gtest options to |parser|."""
[email protected]fbe29322013-07-09 09:03:26178
jama47ca85c2014-12-03 18:38:07179 gtest_suites = list(gtest_config.STABLE_TEST_SUITES
180 + gtest_config.EXPERIMENTAL_TEST_SUITES)
[email protected]fbe29322013-07-09 09:03:26181
jama47ca85c2014-12-03 18:38:07182 group = parser.add_argument_group('GTest Options')
jbudorick15cdcd52014-12-03 19:58:49183 group.add_argument('-s', '--suite', dest='suite_name',
jama47ca85c2014-12-03 18:38:07184 nargs='+', metavar='SUITE_NAME', required=True,
jbudorick15cdcd52014-12-03 19:58:49185 help=('Executable name of the test suite to run. '
186 'Available suites include (but are not limited to): '
187 '%s' % ', '.join('"%s"' % s for s in gtest_suites)))
jama47ca85c2014-12-03 18:38:07188 group.add_argument('-f', '--gtest_filter', '--gtest-filter',
189 dest='test_filter',
190 help='googletest-style filter string.')
191 group.add_argument('--gtest_also_run_disabled_tests',
192 '--gtest-also-run-disabled-tests',
193 dest='run_disabled', action='store_true',
194 help='Also run disabled tests if applicable.')
195 group.add_argument('-a', '--test-arguments', dest='test_arguments',
196 default='',
197 help='Additional arguments to pass to the test.')
198 group.add_argument('-t', dest='timeout', type=int, default=60,
199 help='Timeout to wait for each test '
200 '(default: %(default)s).')
201 group.add_argument('--isolate_file_path',
202 '--isolate-file-path',
203 dest='isolate_file_path',
204 help='.isolate file path to override the default '
205 'path')
206 AddDeviceOptions(parser)
207 AddCommonOptions(parser)
rnephew5c499782014-12-12 19:08:55208 AddRemoteDeviceOptions(parser)
[email protected]fbe29322013-07-09 09:03:26209
210
jama47ca85c2014-12-03 18:38:07211def AddLinkerTestOptions(parser):
212 group = parser.add_argument_group('Linker Test Options')
213 group.add_argument('-f', '--gtest-filter', dest='test_filter',
214 help='googletest-style filter string.')
215 AddCommonOptions(parser)
216 AddDeviceOptions(parser)
[email protected]6b6abac6d2013-10-03 11:56:38217
218
jama47ca85c2014-12-03 18:38:07219def AddJavaTestOptions(argument_group):
[email protected]fbe29322013-07-09 09:03:26220 """Adds the Java test options to |option_parser|."""
221
jama47ca85c2014-12-03 18:38:07222 argument_group.add_argument(
223 '-f', '--test-filter', dest='test_filter',
224 help=('Test filter (if not fully qualified, will run all matches).'))
225 argument_group.add_argument(
[email protected]fbe29322013-07-09 09:03:26226 '-A', '--annotation', dest='annotation_str',
227 help=('Comma-separated list of annotations. Run only tests with any of '
228 'the given annotations. An annotation can be either a key or a '
229 'key-values pair. A test that has no annotation is considered '
230 '"SmallTest".'))
jama47ca85c2014-12-03 18:38:07231 argument_group.add_argument(
[email protected]fbe29322013-07-09 09:03:26232 '-E', '--exclude-annotation', dest='exclude_annotation_str',
233 help=('Comma-separated list of annotations. Exclude tests with these '
234 'annotations.'))
jama47ca85c2014-12-03 18:38:07235 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59236 '--screenshot', dest='screenshot_failures', action='store_true',
237 help='Capture screenshots of test failures')
jama47ca85c2014-12-03 18:38:07238 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59239 '--save-perf-json', action='store_true',
240 help='Saves the JSON file for each UI Perf test.')
jama47ca85c2014-12-03 18:38:07241 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59242 '--official-build', action='store_true', help='Run official build tests.')
jama47ca85c2014-12-03 18:38:07243 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59244 '--test_data', '--test-data', action='append', default=[],
245 help=('Each instance defines a directory of test data that should be '
246 'copied to the target(s) before running the tests. The argument '
247 'should be of the form <target>:<source>, <target> is relative to '
248 'the device data directory, and <source> is relative to the '
249 'chromium build directory.'))
davileen98efad12015-01-05 19:48:21250 argument_group.add_argument(
251 '--disable-dalvik-asserts', dest='set_asserts', action='store_false',
252 default=True, help='Removes the dalvik.vm.enableassertions property')
253
[email protected]fbe29322013-07-09 09:03:26254
255
jama47ca85c2014-12-03 18:38:07256def ProcessJavaTestOptions(args):
[email protected]fbe29322013-07-09 09:03:26257 """Processes options/arguments and populates |options| with defaults."""
258
jama47ca85c2014-12-03 18:38:07259 # TODO(jbudorick): Handle most of this function in argparse.
260 if args.annotation_str:
261 args.annotations = args.annotation_str.split(',')
262 elif args.test_filter:
263 args.annotations = []
[email protected]fbe29322013-07-09 09:03:26264 else:
jama47ca85c2014-12-03 18:38:07265 args.annotations = ['Smoke', 'SmallTest', 'MediumTest', 'LargeTest',
266 'EnormousTest', 'IntegrationTest']
[email protected]fbe29322013-07-09 09:03:26267
jama47ca85c2014-12-03 18:38:07268 if args.exclude_annotation_str:
269 args.exclude_annotations = args.exclude_annotation_str.split(',')
[email protected]fbe29322013-07-09 09:03:26270 else:
jama47ca85c2014-12-03 18:38:07271 args.exclude_annotations = []
[email protected]fbe29322013-07-09 09:03:26272
[email protected]fbe29322013-07-09 09:03:26273
jama47ca85c2014-12-03 18:38:07274def AddInstrumentationTestOptions(parser):
275 """Adds Instrumentation test options to |parser|."""
[email protected]fbe29322013-07-09 09:03:26276
jama47ca85c2014-12-03 18:38:07277 parser.usage = '%(prog)s [options]'
[email protected]fbe29322013-07-09 09:03:26278
jama47ca85c2014-12-03 18:38:07279 group = parser.add_argument_group('Instrumentation Test Options')
280 AddJavaTestOptions(group)
[email protected]fbe29322013-07-09 09:03:26281
jama47ca85c2014-12-03 18:38:07282 java_or_python_group = group.add_mutually_exclusive_group()
283 java_or_python_group.add_argument(
284 '-j', '--java-only', action='store_false',
285 dest='run_python_tests', default=True, help='Run only the Java tests.')
286 java_or_python_group.add_argument(
287 '-p', '--python-only', action='store_false',
288 dest='run_java_tests', default=True,
289 help='Run only the host-driven tests.')
290
291 group.add_argument('--host-driven-root',
292 help='Root of the host-driven tests.')
293 group.add_argument('-w', '--wait_debugger', dest='wait_for_debugger',
294 action='store_true',
295 help='Wait for debugger.')
jbudorick911be58d2015-01-13 02:51:06296 group.add_argument('--apk-under-test', dest='apk_under_test',
297 help=('the name of the apk under test.'))
jama47ca85c2014-12-03 18:38:07298 group.add_argument('--test-apk', dest='test_apk', required=True,
299 help=('The name of the apk containing the tests '
300 '(without the .apk extension; '
301 'e.g. "ContentShellTest").'))
302 group.add_argument('--coverage-dir',
303 help=('Directory in which to place all generated '
304 'EMMA coverage files.'))
305 group.add_argument('--device-flags', dest='device_flags', default='',
306 help='The relative filepath to a file containing '
307 'command-line flags to set on the device')
jbudorick911be58d2015-01-13 02:51:06308 group.add_argument('--device-flags-file', default='',
309 help='The relative filepath to a file containing '
310 'command-line flags to set on the device')
jama47ca85c2014-12-03 18:38:07311 group.add_argument('--isolate_file_path',
312 '--isolate-file-path',
313 dest='isolate_file_path',
314 help='.isolate file path to override the default '
315 'path')
316
317 AddCommonOptions(parser)
318 AddDeviceOptions(parser)
[email protected]fbe29322013-07-09 09:03:26319
320
jama47ca85c2014-12-03 18:38:07321def ProcessInstrumentationOptions(args):
[email protected]2a684222013-08-01 16:59:22322 """Processes options/arguments and populate |options| with defaults.
323
324 Args:
jama47ca85c2014-12-03 18:38:07325 args: argparse.Namespace object.
[email protected]2a684222013-08-01 16:59:22326
327 Returns:
328 An InstrumentationOptions named tuple which contains all options relevant to
329 instrumentation tests.
330 """
[email protected]fbe29322013-07-09 09:03:26331
jama47ca85c2014-12-03 18:38:07332 ProcessJavaTestOptions(args)
[email protected]fbe29322013-07-09 09:03:26333
jama47ca85c2014-12-03 18:38:07334 if not args.host_driven_root:
335 args.run_python_tests = False
[email protected]37ee0c792013-08-06 19:10:13336
jama47ca85c2014-12-03 18:38:07337 args.test_apk_path = os.path.join(
[email protected]2eea4872014-07-28 23:06:17338 constants.GetOutDirectory(),
339 constants.SDK_BUILD_APKS_DIR,
jama47ca85c2014-12-03 18:38:07340 '%s.apk' % args.test_apk)
341 args.test_apk_jar_path = os.path.join(
[email protected]ae68d4a2013-09-24 21:57:15342 constants.GetOutDirectory(),
343 constants.SDK_BUILD_TEST_JAVALIB_DIR,
jama47ca85c2014-12-03 18:38:07344 '%s.jar' % args.test_apk)
345 args.test_support_apk_path = '%sSupport%s' % (
346 os.path.splitext(args.test_apk_path))
[email protected]5e2f3f62014-06-23 12:31:46347
jama47ca85c2014-12-03 18:38:07348 args.test_runner = apk_helper.GetInstrumentationName(args.test_apk_path)
[email protected]5e2f3f62014-06-23 12:31:46349
jama47ca85c2014-12-03 18:38:07350 # TODO(jbudorick): Get rid of InstrumentationOptions.
[email protected]2a684222013-08-01 16:59:22351 return instrumentation_test_options.InstrumentationOptions(
jama47ca85c2014-12-03 18:38:07352 args.tool,
353 args.cleanup_test_files,
354 args.annotations,
355 args.exclude_annotations,
356 args.test_filter,
357 args.test_data,
358 args.save_perf_json,
359 args.screenshot_failures,
360 args.wait_for_debugger,
361 args.coverage_dir,
362 args.test_apk,
363 args.test_apk_path,
364 args.test_apk_jar_path,
365 args.test_runner,
366 args.test_support_apk_path,
367 args.device_flags,
davileen98efad12015-01-05 19:48:21368 args.isolate_file_path,
369 args.set_asserts
[email protected]5e2f3f62014-06-23 12:31:46370 )
[email protected]2a684222013-08-01 16:59:22371
[email protected]fbe29322013-07-09 09:03:26372
jama47ca85c2014-12-03 18:38:07373def AddUIAutomatorTestOptions(parser):
374 """Adds UI Automator test options to |parser|."""
[email protected]fbe29322013-07-09 09:03:26375
jama47ca85c2014-12-03 18:38:07376 group = parser.add_argument_group('UIAutomator Test Options')
377 AddJavaTestOptions(group)
378 group.add_argument(
379 '--package', required=True, choices=constants.PACKAGE_INFO.keys(),
380 metavar='PACKAGE', help='Package under test.')
381 group.add_argument(
382 '--test-jar', dest='test_jar', required=True,
[email protected]fbe29322013-07-09 09:03:26383 help=('The name of the dexed jar containing the tests (without the '
384 '.dex.jar extension). Alternatively, this can be a full path '
385 'to the jar.'))
386
jama47ca85c2014-12-03 18:38:07387 AddCommonOptions(parser)
388 AddDeviceOptions(parser)
[email protected]fbe29322013-07-09 09:03:26389
390
jama47ca85c2014-12-03 18:38:07391def ProcessUIAutomatorOptions(args):
[email protected]2a684222013-08-01 16:59:22392 """Processes UIAutomator options/arguments.
393
394 Args:
jama47ca85c2014-12-03 18:38:07395 args: argparse.Namespace object.
[email protected]2a684222013-08-01 16:59:22396
397 Returns:
398 A UIAutomatorOptions named tuple which contains all options relevant to
[email protected]3dbdfa42013-08-08 01:08:14399 uiautomator tests.
[email protected]2a684222013-08-01 16:59:22400 """
[email protected]fbe29322013-07-09 09:03:26401
jama47ca85c2014-12-03 18:38:07402 ProcessJavaTestOptions(args)
[email protected]fbe29322013-07-09 09:03:26403
jama47ca85c2014-12-03 18:38:07404 if os.path.exists(args.test_jar):
[email protected]fbe29322013-07-09 09:03:26405 # The dexed JAR is fully qualified, assume the info JAR lives along side.
jama47ca85c2014-12-03 18:38:07406 args.uiautomator_jar = args.test_jar
[email protected]fbe29322013-07-09 09:03:26407 else:
jama47ca85c2014-12-03 18:38:07408 args.uiautomator_jar = os.path.join(
[email protected]ae68d4a2013-09-24 21:57:15409 constants.GetOutDirectory(),
410 constants.SDK_BUILD_JAVALIB_DIR,
jama47ca85c2014-12-03 18:38:07411 '%s.dex.jar' % args.test_jar)
412 args.uiautomator_info_jar = (
413 args.uiautomator_jar[:args.uiautomator_jar.find('.dex.jar')] +
[email protected]fbe29322013-07-09 09:03:26414 '_java.jar')
415
[email protected]2a684222013-08-01 16:59:22416 return uiautomator_test_options.UIAutomatorOptions(
jama47ca85c2014-12-03 18:38:07417 args.tool,
418 args.cleanup_test_files,
419 args.annotations,
420 args.exclude_annotations,
421 args.test_filter,
422 args.test_data,
423 args.save_perf_json,
424 args.screenshot_failures,
425 args.uiautomator_jar,
426 args.uiautomator_info_jar,
davileen98efad12015-01-05 19:48:21427 args.package,
428 args.set_asserts)
[email protected]2a684222013-08-01 16:59:22429
[email protected]fbe29322013-07-09 09:03:26430
jama47ca85c2014-12-03 18:38:07431def AddJUnitTestOptions(parser):
432 """Adds junit test options to |parser|."""
jbudorick9a6b7b332014-09-20 00:01:07433
jama47ca85c2014-12-03 18:38:07434 group = parser.add_argument_group('JUnit Test Options')
435 group.add_argument(
436 '-s', '--test-suite', dest='test_suite', required=True,
jbudorick9a6b7b332014-09-20 00:01:07437 help=('JUnit test suite to run.'))
jama47ca85c2014-12-03 18:38:07438 group.add_argument(
jbudorick9a6b7b332014-09-20 00:01:07439 '-f', '--test-filter', dest='test_filter',
440 help='Filters tests googletest-style.')
jama47ca85c2014-12-03 18:38:07441 group.add_argument(
jbudorick9a6b7b332014-09-20 00:01:07442 '--package-filter', dest='package_filter',
443 help='Filters tests by package.')
jama47ca85c2014-12-03 18:38:07444 group.add_argument(
jbudorick9a6b7b332014-09-20 00:01:07445 '--runner-filter', dest='runner_filter',
446 help='Filters tests by runner class. Must be fully qualified.')
jama47ca85c2014-12-03 18:38:07447 group.add_argument(
448 '--sdk-version', dest='sdk_version', type=int,
jbudorick9a6b7b332014-09-20 00:01:07449 help='The Android SDK version.')
jama47ca85c2014-12-03 18:38:07450 AddCommonOptions(parser)
jbudorick9a6b7b332014-09-20 00:01:07451
452
jama47ca85c2014-12-03 18:38:07453def AddMonkeyTestOptions(parser):
454 """Adds monkey test options to |parser|."""
jbudorick9a6b7b332014-09-20 00:01:07455
jama47ca85c2014-12-03 18:38:07456 group = parser.add_argument_group('Monkey Test Options')
457 group.add_argument(
458 '--package', required=True, choices=constants.PACKAGE_INFO.keys(),
459 metavar='PACKAGE', help='Package under test.')
460 group.add_argument(
461 '--event-count', default=10000, type=int,
462 help='Number of events to generate (default: %(default)s).')
463 group.add_argument(
[email protected]3dbdfa42013-08-08 01:08:14464 '--category', default='',
[email protected]fb81b982013-08-09 00:07:12465 help='A list of allowed categories.')
jama47ca85c2014-12-03 18:38:07466 group.add_argument(
467 '--throttle', default=100, type=int,
468 help='Delay between events (ms) (default: %(default)s). ')
469 group.add_argument(
470 '--seed', type=int,
[email protected]3dbdfa42013-08-08 01:08:14471 help=('Seed value for pseudo-random generator. Same seed value generates '
472 'the same sequence of events. Seed is randomized by default.'))
jama47ca85c2014-12-03 18:38:07473 group.add_argument(
[email protected]3dbdfa42013-08-08 01:08:14474 '--extra-args', default='',
jama47ca85c2014-12-03 18:38:07475 help=('String of other args to pass to the command verbatim.'))
[email protected]3dbdfa42013-08-08 01:08:14476
jama47ca85c2014-12-03 18:38:07477 AddCommonOptions(parser)
478 AddDeviceOptions(parser)
[email protected]3dbdfa42013-08-08 01:08:14479
jama47ca85c2014-12-03 18:38:07480def ProcessMonkeyTestOptions(args):
[email protected]3dbdfa42013-08-08 01:08:14481 """Processes all monkey test options.
482
483 Args:
jama47ca85c2014-12-03 18:38:07484 args: argparse.Namespace object.
[email protected]3dbdfa42013-08-08 01:08:14485
486 Returns:
487 A MonkeyOptions named tuple which contains all options relevant to
488 monkey tests.
489 """
jama47ca85c2014-12-03 18:38:07490 # TODO(jbudorick): Handle this directly in argparse with nargs='+'
491 category = args.category
[email protected]3dbdfa42013-08-08 01:08:14492 if category:
jama47ca85c2014-12-03 18:38:07493 category = args.category.split(',')
[email protected]3dbdfa42013-08-08 01:08:14494
jama47ca85c2014-12-03 18:38:07495 # TODO(jbudorick): Get rid of MonkeyOptions.
[email protected]3dbdfa42013-08-08 01:08:14496 return monkey_test_options.MonkeyOptions(
jama47ca85c2014-12-03 18:38:07497 args.verbose_count,
498 args.package,
499 args.event_count,
[email protected]3dbdfa42013-08-08 01:08:14500 category,
jama47ca85c2014-12-03 18:38:07501 args.throttle,
502 args.seed,
503 args.extra_args)
[email protected]3dbdfa42013-08-08 01:08:14504
rnephew5c499782014-12-12 19:08:55505def AddUirobotTestOptions(parser):
506 """Adds uirobot test options to |option_parser|."""
507 group = parser.add_argument_group('Uirobot Test Options')
508
509 group.add_argument(
510 '--minutes', default=5, type=int,
511 help='Number of minutes to run uirobot test [default: %default].')
512
513 AddCommonOptions(parser)
514 AddDeviceOptions(parser)
515 AddRemoteDeviceOptions(parser)
[email protected]3dbdfa42013-08-08 01:08:14516
jama47ca85c2014-12-03 18:38:07517def AddPerfTestOptions(parser):
518 """Adds perf test options to |parser|."""
[email protected]ec3170b2013-08-14 14:39:47519
jama47ca85c2014-12-03 18:38:07520 group = parser.add_argument_group('Perf Test Options')
[email protected]ec3170b2013-08-14 14:39:47521
jama47ca85c2014-12-03 18:38:07522 class SingleStepAction(argparse.Action):
523 def __call__(self, parser, namespace, values, option_string=None):
524 if values and not namespace.single_step:
525 parser.error('single step command provided, '
526 'but --single-step not specified.')
527 elif namespace.single_step and not values:
528 parser.error('--single-step specified, '
529 'but no single step command provided.')
530 setattr(namespace, self.dest, values)
531
532 step_group = group.add_mutually_exclusive_group(required=True)
533 # TODO(jbudorick): Revise --single-step to use argparse.REMAINDER.
534 # This requires removing "--" from client calls.
535 step_group.add_argument(
536 '--single-step', action='store_true',
[email protected]def4bce2013-11-12 12:59:52537 help='Execute the given command with retries, but only print the result '
538 'for the "most successful" round.')
jama47ca85c2014-12-03 18:38:07539 step_group.add_argument(
[email protected]181a5c92013-09-06 17:11:46540 '--steps',
[email protected]def4bce2013-11-12 12:59:52541 help='JSON file containing the list of commands to run.')
jama47ca85c2014-12-03 18:38:07542 step_group.add_argument(
543 '--print-step',
544 help='The name of a previously executed perf step to print.')
545
546 group.add_argument(
peterbd4e73d2014-12-03 15:47:36547 '--output-json-list',
548 help='Write a simple list of names from --steps into the given file.')
jama47ca85c2014-12-03 18:38:07549 group.add_argument(
peterbd4e73d2014-12-03 15:47:36550 '--collect-chartjson-data',
551 action='store_true',
552 help='Cache the chartjson output from each step for later use.')
jama47ca85c2014-12-03 18:38:07553 group.add_argument(
peterbd4e73d2014-12-03 15:47:36554 '--output-chartjson-data',
555 default='',
556 help='Write out chartjson into the given file.')
jama47ca85c2014-12-03 18:38:07557 group.add_argument(
558 '--flaky-steps',
559 help=('A JSON file containing steps that are flaky '
560 'and will have its exit code ignored.'))
561 group.add_argument(
[email protected]181a5c92013-09-06 17:11:46562 '--no-timeout', action='store_true',
563 help=('Do not impose a timeout. Each perf step is responsible for '
564 'implementing the timeout logic.'))
jama47ca85c2014-12-03 18:38:07565 group.add_argument(
[email protected]650487c2013-09-30 11:40:49566 '-f', '--test-filter',
567 help=('Test filter (will match against the names listed in --steps).'))
jama47ca85c2014-12-03 18:38:07568 group.add_argument(
569 '--dry-run', action='store_true',
[email protected]650487c2013-09-30 11:40:49570 help='Just print the steps without executing.')
jama47ca85c2014-12-03 18:38:07571 group.add_argument('single_step_command', nargs='*', action=SingleStepAction,
572 help='If --single-step is specified, the command to run.')
573 AddCommonOptions(parser)
574 AddDeviceOptions(parser)
[email protected]ec3170b2013-08-14 14:39:47575
576
jama47ca85c2014-12-03 18:38:07577def ProcessPerfTestOptions(args):
[email protected]ec3170b2013-08-14 14:39:47578 """Processes all perf test options.
579
580 Args:
jama47ca85c2014-12-03 18:38:07581 args: argparse.Namespace object.
[email protected]ec3170b2013-08-14 14:39:47582
583 Returns:
584 A PerfOptions named tuple which contains all options relevant to
585 perf tests.
586 """
jama47ca85c2014-12-03 18:38:07587 # TODO(jbudorick): Move single_step handling down into the perf tests.
588 if args.single_step:
589 args.single_step = ' '.join(args.single_step_command)
590 # TODO(jbudorick): Get rid of PerfOptions.
[email protected]ec3170b2013-08-14 14:39:47591 return perf_test_options.PerfOptions(
jama47ca85c2014-12-03 18:38:07592 args.steps, args.flaky_steps, args.output_json_list,
593 args.print_step, args.no_timeout, args.test_filter,
594 args.dry_run, args.single_step, args.collect_chartjson_data,
595 args.output_chartjson_data)
[email protected]ec3170b2013-08-14 14:39:47596
597
jama47ca85c2014-12-03 18:38:07598def AddPythonTestOptions(parser):
599 group = parser.add_argument_group('Python Test Options')
600 group.add_argument(
601 '-s', '--suite', dest='suite_name', metavar='SUITE_NAME',
602 choices=constants.PYTHON_UNIT_TEST_SUITES.keys(),
603 help='Name of the test suite to run.')
604 AddCommonOptions(parser)
jbudorick256fd532014-10-24 01:50:13605
606
jama47ca85c2014-12-03 18:38:07607def _RunGTests(args, devices):
[email protected]6bc1bda22013-07-19 22:08:37608 """Subcommand of RunTestsCommands which runs gtests."""
[email protected]6bc1bda22013-07-19 22:08:37609 exit_code = 0
jama47ca85c2014-12-03 18:38:07610 for suite_name in args.suite_name:
611 # TODO(jbudorick): Either deprecate multi-suite or move its handling down
612 # into the gtest code.
[email protected]2a684222013-08-01 16:59:22613 gtest_options = gtest_test_options.GTestOptions(
jama47ca85c2014-12-03 18:38:07614 args.tool,
615 args.cleanup_test_files,
616 args.test_filter,
617 args.run_disabled,
618 args.test_arguments,
619 args.timeout,
620 args.isolate_file_path,
[email protected]2a684222013-08-01 16:59:22621 suite_name)
[email protected]f7148dd42013-08-20 14:24:57622 runner_factory, tests = gtest_setup.Setup(gtest_options, devices)
[email protected]6bc1bda22013-07-19 22:08:37623
624 results, test_exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57625 tests, runner_factory, devices, shard=True, test_timeout=None,
jama47ca85c2014-12-03 18:38:07626 num_retries=args.num_retries)
[email protected]6bc1bda22013-07-19 22:08:37627
628 if test_exit_code and exit_code != constants.ERROR_EXIT_CODE:
629 exit_code = test_exit_code
630
631 report_results.LogFull(
632 results=results,
633 test_type='Unit test',
634 test_package=suite_name,
jama47ca85c2014-12-03 18:38:07635 flakiness_server=args.flakiness_dashboard_server)
[email protected]6bc1bda22013-07-19 22:08:37636
jama47ca85c2014-12-03 18:38:07637 if args.json_results_file:
638 json_results.GenerateJsonResultsFile(results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54639
[email protected]6bc1bda22013-07-19 22:08:37640 if os.path.isdir(constants.ISOLATE_DEPS_DIR):
641 shutil.rmtree(constants.ISOLATE_DEPS_DIR)
642
643 return exit_code
644
645
jama47ca85c2014-12-03 18:38:07646def _RunLinkerTests(args, devices):
[email protected]6b6abac6d2013-10-03 11:56:38647 """Subcommand of RunTestsCommands which runs linker tests."""
jama47ca85c2014-12-03 18:38:07648 runner_factory, tests = linker_setup.Setup(args, devices)
[email protected]6b6abac6d2013-10-03 11:56:38649
650 results, exit_code = test_dispatcher.RunTests(
651 tests, runner_factory, devices, shard=True, test_timeout=60,
jama47ca85c2014-12-03 18:38:07652 num_retries=args.num_retries)
[email protected]6b6abac6d2013-10-03 11:56:38653
654 report_results.LogFull(
655 results=results,
656 test_type='Linker test',
[email protected]93c9f9b2014-02-10 16:19:22657 test_package='ChromiumLinkerTest')
[email protected]6b6abac6d2013-10-03 11:56:38658
jama47ca85c2014-12-03 18:38:07659 if args.json_results_file:
660 json_results.GenerateJsonResultsFile(results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54661
[email protected]6b6abac6d2013-10-03 11:56:38662 return exit_code
663
664
jama47ca85c2014-12-03 18:38:07665def _RunInstrumentationTests(args, devices):
[email protected]6bc1bda22013-07-19 22:08:37666 """Subcommand of RunTestsCommands which runs instrumentation tests."""
jama47ca85c2014-12-03 18:38:07667 logging.info('_RunInstrumentationTests(%s, %s)' % (str(args), str(devices)))
[email protected]6bc1bda22013-07-19 22:08:37668
jama47ca85c2014-12-03 18:38:07669 instrumentation_options = ProcessInstrumentationOptions(args)
670
671 if len(devices) > 1 and args.wait_for_debugger:
[email protected]f7148dd42013-08-20 14:24:57672 logging.warning('Debugger can not be sharded, using first available device')
673 devices = devices[:1]
674
[email protected]6bc1bda22013-07-19 22:08:37675 results = base_test_result.TestRunResults()
676 exit_code = 0
677
jama47ca85c2014-12-03 18:38:07678 if args.run_java_tests:
mikecase526d68e2014-11-19 20:02:05679 runner_factory, tests = instrumentation_setup.Setup(
680 instrumentation_options, devices)
[email protected]6bc1bda22013-07-19 22:08:37681
682 test_results, exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57683 tests, runner_factory, devices, shard=True, test_timeout=None,
jama47ca85c2014-12-03 18:38:07684 num_retries=args.num_retries)
[email protected]6bc1bda22013-07-19 22:08:37685
686 results.AddTestRunResults(test_results)
687
jama47ca85c2014-12-03 18:38:07688 if args.run_python_tests:
[email protected]37ee0c792013-08-06 19:10:13689 runner_factory, tests = host_driven_setup.InstrumentationSetup(
jama47ca85c2014-12-03 18:38:07690 args.host_driven_root, args.official_build,
[email protected]37ee0c792013-08-06 19:10:13691 instrumentation_options)
692
[email protected]34020022013-08-06 23:35:34693 if tests:
694 test_results, test_exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57695 tests, runner_factory, devices, shard=True, test_timeout=None,
jama47ca85c2014-12-03 18:38:07696 num_retries=args.num_retries)
[email protected]6bc1bda22013-07-19 22:08:37697
[email protected]34020022013-08-06 23:35:34698 results.AddTestRunResults(test_results)
[email protected]6bc1bda22013-07-19 22:08:37699
[email protected]34020022013-08-06 23:35:34700 # Only allow exit code escalation
701 if test_exit_code and exit_code != constants.ERROR_EXIT_CODE:
702 exit_code = test_exit_code
[email protected]6bc1bda22013-07-19 22:08:37703
jama47ca85c2014-12-03 18:38:07704 if args.device_flags:
705 args.device_flags = os.path.join(constants.DIR_SOURCE_ROOT,
706 args.device_flags)
[email protected]4f777ca2014-08-08 01:45:59707
[email protected]6bc1bda22013-07-19 22:08:37708 report_results.LogFull(
709 results=results,
710 test_type='Instrumentation',
jama47ca85c2014-12-03 18:38:07711 test_package=os.path.basename(args.test_apk),
712 annotation=args.annotations,
713 flakiness_server=args.flakiness_dashboard_server)
[email protected]6bc1bda22013-07-19 22:08:37714
jama47ca85c2014-12-03 18:38:07715 if args.json_results_file:
716 json_results.GenerateJsonResultsFile(results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54717
[email protected]6bc1bda22013-07-19 22:08:37718 return exit_code
719
720
jama47ca85c2014-12-03 18:38:07721def _RunUIAutomatorTests(args, devices):
[email protected]6bc1bda22013-07-19 22:08:37722 """Subcommand of RunTestsCommands which runs uiautomator tests."""
jama47ca85c2014-12-03 18:38:07723 uiautomator_options = ProcessUIAutomatorOptions(args)
[email protected]6bc1bda22013-07-19 22:08:37724
[email protected]37ee0c792013-08-06 19:10:13725 runner_factory, tests = uiautomator_setup.Setup(uiautomator_options)
[email protected]6bc1bda22013-07-19 22:08:37726
[email protected]37ee0c792013-08-06 19:10:13727 results, exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57728 tests, runner_factory, devices, shard=True, test_timeout=None,
jama47ca85c2014-12-03 18:38:07729 num_retries=args.num_retries)
[email protected]6bc1bda22013-07-19 22:08:37730
731 report_results.LogFull(
732 results=results,
733 test_type='UIAutomator',
jama47ca85c2014-12-03 18:38:07734 test_package=os.path.basename(args.test_jar),
735 annotation=args.annotations,
736 flakiness_server=args.flakiness_dashboard_server)
[email protected]6bc1bda22013-07-19 22:08:37737
jama47ca85c2014-12-03 18:38:07738 if args.json_results_file:
739 json_results.GenerateJsonResultsFile(results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54740
[email protected]6bc1bda22013-07-19 22:08:37741 return exit_code
742
743
jama47ca85c2014-12-03 18:38:07744def _RunJUnitTests(args):
jbudorick9a6b7b332014-09-20 00:01:07745 """Subcommand of RunTestsCommand which runs junit tests."""
jama47ca85c2014-12-03 18:38:07746 runner_factory, tests = junit_setup.Setup(args)
jbudorick9a6b7b332014-09-20 00:01:07747 _, exit_code = junit_dispatcher.RunTests(tests, runner_factory)
jbudorick9a6b7b332014-09-20 00:01:07748 return exit_code
749
750
jama47ca85c2014-12-03 18:38:07751def _RunMonkeyTests(args, devices):
[email protected]3dbdfa42013-08-08 01:08:14752 """Subcommand of RunTestsCommands which runs monkey tests."""
jama47ca85c2014-12-03 18:38:07753 monkey_options = ProcessMonkeyTestOptions(args)
[email protected]3dbdfa42013-08-08 01:08:14754
755 runner_factory, tests = monkey_setup.Setup(monkey_options)
756
757 results, exit_code = test_dispatcher.RunTests(
[email protected]181a5c92013-09-06 17:11:46758 tests, runner_factory, devices, shard=False, test_timeout=None,
jama47ca85c2014-12-03 18:38:07759 num_retries=args.num_retries)
[email protected]3dbdfa42013-08-08 01:08:14760
761 report_results.LogFull(
762 results=results,
763 test_type='Monkey',
[email protected]14b3b1202013-08-15 22:25:28764 test_package='Monkey')
[email protected]3dbdfa42013-08-08 01:08:14765
jama47ca85c2014-12-03 18:38:07766 if args.json_results_file:
767 json_results.GenerateJsonResultsFile(results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54768
[email protected]3dbdfa42013-08-08 01:08:14769 return exit_code
770
771
jama47ca85c2014-12-03 18:38:07772def _RunPerfTests(args):
[email protected]ec3170b2013-08-14 14:39:47773 """Subcommand of RunTestsCommands which runs perf tests."""
jama47ca85c2014-12-03 18:38:07774 perf_options = ProcessPerfTestOptions(args)
[email protected]61487ed2014-06-09 12:33:56775
776 # Just save a simple json with a list of test names.
777 if perf_options.output_json_list:
778 return perf_test_runner.OutputJsonList(
779 perf_options.steps, perf_options.output_json_list)
780
[email protected]ad32f312013-11-13 04:03:29781 # Just print the results from a single previously executed step.
[email protected]ec3170b2013-08-14 14:39:47782 if perf_options.print_step:
simonhatch9b9256d2015-01-07 18:03:42783 return perf_test_runner.PrintTestOutput(
784 perf_options.print_step, perf_options.output_chartjson_data)
[email protected]ec3170b2013-08-14 14:39:47785
[email protected]a72f0752014-06-03 23:52:34786 runner_factory, tests, devices = perf_setup.Setup(perf_options)
[email protected]ec3170b2013-08-14 14:39:47787
[email protected]a72f0752014-06-03 23:52:34788 # shard=False means that each device will get the full list of tests
789 # and then each one will decide their own affinity.
790 # shard=True means each device will pop the next test available from a queue,
791 # which increases throughput but have no affinity.
[email protected]86184c7b2013-08-15 15:06:57792 results, _ = test_dispatcher.RunTests(
[email protected]a72f0752014-06-03 23:52:34793 tests, runner_factory, devices, shard=False, test_timeout=None,
jama47ca85c2014-12-03 18:38:07794 num_retries=args.num_retries)
[email protected]ec3170b2013-08-14 14:39:47795
796 report_results.LogFull(
797 results=results,
798 test_type='Perf',
[email protected]865a47a2013-08-16 14:01:12799 test_package='Perf')
[email protected]def4bce2013-11-12 12:59:52800
jama47ca85c2014-12-03 18:38:07801 if args.json_results_file:
802 json_results.GenerateJsonResultsFile(results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54803
[email protected]def4bce2013-11-12 12:59:52804 if perf_options.single_step:
805 return perf_test_runner.PrintTestOutput('single_step')
806
[email protected]11ce8452014-02-17 10:55:03807 perf_test_runner.PrintSummary(tests)
808
[email protected]86184c7b2013-08-15 15:06:57809 # Always return 0 on the sharding stage. Individual tests exit_code
810 # will be returned on the print_step stage.
811 return 0
[email protected]ec3170b2013-08-14 14:39:47812
[email protected]3dbdfa42013-08-08 01:08:14813
jama47ca85c2014-12-03 18:38:07814def _RunPythonTests(args):
jbudorick256fd532014-10-24 01:50:13815 """Subcommand of RunTestsCommand which runs python unit tests."""
jama47ca85c2014-12-03 18:38:07816 suite_vars = constants.PYTHON_UNIT_TEST_SUITES[args.suite_name]
jbudorick256fd532014-10-24 01:50:13817 suite_path = suite_vars['path']
818 suite_test_modules = suite_vars['test_modules']
819
820 sys.path = [suite_path] + sys.path
821 try:
822 suite = unittest.TestSuite()
823 suite.addTests(unittest.defaultTestLoader.loadTestsFromName(m)
824 for m in suite_test_modules)
jama47ca85c2014-12-03 18:38:07825 runner = unittest.TextTestRunner(verbosity=1+args.verbose_count)
jbudorick256fd532014-10-24 01:50:13826 return 0 if runner.run(suite).wasSuccessful() else 1
827 finally:
828 sys.path = sys.path[1:]
829
830
[email protected]f7148dd42013-08-20 14:24:57831def _GetAttachedDevices(test_device=None):
832 """Get all attached devices.
833
834 Args:
835 test_device: Name of a specific device to use.
836
837 Returns:
838 A list of attached devices.
839 """
840 attached_devices = []
841
842 attached_devices = android_commands.GetAttachedDevices()
843 if test_device:
844 assert test_device in attached_devices, (
845 'Did not find device %s among attached device. Attached devices: %s'
846 % (test_device, ', '.join(attached_devices)))
847 attached_devices = [test_device]
848
849 assert attached_devices, 'No devices attached.'
850
851 return sorted(attached_devices)
852
853
jama47ca85c2014-12-03 18:38:07854def RunTestsCommand(args, parser):
[email protected]fbe29322013-07-09 09:03:26855 """Checks test type and dispatches to the appropriate function.
856
857 Args:
jama47ca85c2014-12-03 18:38:07858 args: argparse.Namespace object.
859 parser: argparse.ArgumentParser object.
[email protected]fbe29322013-07-09 09:03:26860
861 Returns:
862 Integer indicated exit code.
[email protected]b3873892013-07-10 04:57:10863
864 Raises:
865 Exception: Unknown command name passed in, or an exception from an
866 individual test runner.
[email protected]fbe29322013-07-09 09:03:26867 """
jama47ca85c2014-12-03 18:38:07868 command = args.command
[email protected]fbe29322013-07-09 09:03:26869
jama47ca85c2014-12-03 18:38:07870 ProcessCommonOptions(args)
[email protected]d82f0252013-07-12 23:22:57871
jama47ca85c2014-12-03 18:38:07872 if args.enable_platform_mode:
rnephew5c499782014-12-12 19:08:55873 return RunTestsInPlatformMode(args, parser)
jbudorick66dc3722014-11-06 21:33:51874
875 if command in constants.LOCAL_MACHINE_TESTS:
jbudorick256fd532014-10-24 01:50:13876 devices = []
877 else:
jama47ca85c2014-12-03 18:38:07878 devices = _GetAttachedDevices(args.test_device)
[email protected]f7148dd42013-08-20 14:24:57879
[email protected]c0662e092013-11-12 11:51:25880 forwarder.Forwarder.RemoveHostLog()
[email protected]6b11583b2013-11-21 16:18:40881 if not ports.ResetTestServerPortAllocation():
882 raise Exception('Failed to reset test server port.')
[email protected]c0662e092013-11-12 11:51:25883
[email protected]fbe29322013-07-09 09:03:26884 if command == 'gtest':
jama47ca85c2014-12-03 18:38:07885 return _RunGTests(args, devices)
[email protected]6b6abac6d2013-10-03 11:56:38886 elif command == 'linker':
jama47ca85c2014-12-03 18:38:07887 return _RunLinkerTests(args, devices)
[email protected]fbe29322013-07-09 09:03:26888 elif command == 'instrumentation':
jama47ca85c2014-12-03 18:38:07889 return _RunInstrumentationTests(args, devices)
[email protected]fbe29322013-07-09 09:03:26890 elif command == 'uiautomator':
jama47ca85c2014-12-03 18:38:07891 return _RunUIAutomatorTests(args, devices)
jbudorick9a6b7b332014-09-20 00:01:07892 elif command == 'junit':
jama47ca85c2014-12-03 18:38:07893 return _RunJUnitTests(args)
[email protected]3dbdfa42013-08-08 01:08:14894 elif command == 'monkey':
jama47ca85c2014-12-03 18:38:07895 return _RunMonkeyTests(args, devices)
[email protected]ec3170b2013-08-14 14:39:47896 elif command == 'perf':
jama47ca85c2014-12-03 18:38:07897 return _RunPerfTests(args)
jbudorick256fd532014-10-24 01:50:13898 elif command == 'python':
jama47ca85c2014-12-03 18:38:07899 return _RunPythonTests(args)
[email protected]fbe29322013-07-09 09:03:26900 else:
[email protected]6bc1bda22013-07-19 22:08:37901 raise Exception('Unknown test type.')
[email protected]fbe29322013-07-09 09:03:26902
[email protected]fbe29322013-07-09 09:03:26903
jbudorick66dc3722014-11-06 21:33:51904_SUPPORTED_IN_PLATFORM_MODE = [
905 # TODO(jbudorick): Add support for more test types.
jbudorick911be58d2015-01-13 02:51:06906 'gtest',
907 'instrumentation',
908 'uirobot',
jbudorick66dc3722014-11-06 21:33:51909]
910
911
jama47ca85c2014-12-03 18:38:07912def RunTestsInPlatformMode(args, parser):
jbudorick66dc3722014-11-06 21:33:51913
jama47ca85c2014-12-03 18:38:07914 if args.command not in _SUPPORTED_IN_PLATFORM_MODE:
915 parser.error('%s is not yet supported in platform mode' % args.command)
jbudorick66dc3722014-11-06 21:33:51916
jama47ca85c2014-12-03 18:38:07917 with environment_factory.CreateEnvironment(args, parser.error) as env:
918 with test_instance_factory.CreateTestInstance(args, parser.error) as test:
jbudorick66dc3722014-11-06 21:33:51919 with test_run_factory.CreateTestRun(
jama47ca85c2014-12-03 18:38:07920 args, env, test, parser.error) as test_run:
jbudorick66dc3722014-11-06 21:33:51921 results = test_run.RunTests()
922
jbudorick911be58d2015-01-13 02:51:06923 if args.environment == 'remote_device' and args.trigger:
rnephew5c499782014-12-12 19:08:55924 return 0 # Not returning results, only triggering.
925
jbudorick66dc3722014-11-06 21:33:51926 report_results.LogFull(
927 results=results,
928 test_type=test.TestType(),
929 test_package=test_run.TestPackage(),
jbudorickf9543672014-12-08 22:36:33930 annotation=getattr(args, 'annotations', None),
931 flakiness_server=getattr(args, 'flakiness_dashboard_server', None))
jbudorick66dc3722014-11-06 21:33:51932
jama47ca85c2014-12-03 18:38:07933 if args.json_results_file:
jbudorickb8c42072014-12-01 18:07:54934 json_results.GenerateJsonResultsFile(
jama47ca85c2014-12-03 18:38:07935 results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54936
rnephew5c499782014-12-12 19:08:55937 return 0 if results.DidRunPass() else 1
jbudorick66dc3722014-11-06 21:33:51938
939
jama47ca85c2014-12-03 18:38:07940CommandConfigTuple = collections.namedtuple(
941 'CommandConfigTuple',
942 ['add_options_func', 'help_txt'])
[email protected]fbe29322013-07-09 09:03:26943VALID_COMMANDS = {
jama47ca85c2014-12-03 18:38:07944 'gtest': CommandConfigTuple(
945 AddGTestOptions,
946 'googletest-based C++ tests'),
947 'instrumentation': CommandConfigTuple(
948 AddInstrumentationTestOptions,
949 'InstrumentationTestCase-based Java tests'),
950 'uiautomator': CommandConfigTuple(
951 AddUIAutomatorTestOptions,
952 "Tests that run via Android's uiautomator command"),
953 'junit': CommandConfigTuple(
954 AddJUnitTestOptions,
955 'JUnit4-based Java tests'),
956 'monkey': CommandConfigTuple(
957 AddMonkeyTestOptions,
958 "Tests based on Android's monkey"),
959 'perf': CommandConfigTuple(
960 AddPerfTestOptions,
961 'Performance tests'),
962 'python': CommandConfigTuple(
963 AddPythonTestOptions,
964 'Python tests based on unittest.TestCase'),
965 'linker': CommandConfigTuple(
966 AddLinkerTestOptions,
967 'Linker tests'),
rnephew5c499782014-12-12 19:08:55968 'uirobot': CommandConfigTuple(
969 AddUirobotTestOptions,
970 'Uirobot test'),
jama47ca85c2014-12-03 18:38:07971}
[email protected]fbe29322013-07-09 09:03:26972
973
[email protected]7c53a602014-03-24 16:21:44974def DumpThreadStacks(_signal, _frame):
[email protected]71aec4b2013-11-20 00:35:24975 for thread in threading.enumerate():
976 reraiser_thread.LogThreadStack(thread)
[email protected]83bb8152013-11-19 15:02:21977
978
[email protected]7c53a602014-03-24 16:21:44979def main():
[email protected]83bb8152013-11-19 15:02:21980 signal.signal(signal.SIGUSR1, DumpThreadStacks)
jama47ca85c2014-12-03 18:38:07981
982 parser = argparse.ArgumentParser()
983 command_parsers = parser.add_subparsers(title='test types',
984 dest='command')
985
986 for test_type, config in sorted(VALID_COMMANDS.iteritems(),
987 key=lambda x: x[0]):
988 subparser = command_parsers.add_parser(
989 test_type, usage='%(prog)s [options]', help=config.help_txt)
990 config.add_options_func(subparser)
991
992 args = parser.parse_args()
jbudorick15cdcd52014-12-03 19:58:49993 return RunTestsCommand(args, parser)
[email protected]fbe29322013-07-09 09:03:26994
[email protected]fbe29322013-07-09 09:03:26995
996if __name__ == '__main__':
[email protected]7c53a602014-03-24 16:21:44997 sys.exit(main())