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

Side by Side Diff: build/android/run_all_tests.py

Issue 15942016: Creates a new test running script test_runner.py (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Several more fixes Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 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
7 """Runs all types of tests from one unified interface.
8
9 Types of tests supported:
10 1. GTest native unit tests (gtests)
11 Example: ./run_all_tests.py gtests -s android_webview_unittests
frankf 2013/06/13 23:17:48 It'd be good to give such canonical examples on --
gkanwar 2013/06/17 21:04:43 Done.
12 2. ContentBrowser unit tests (content_browsertests)
13 Example: ./adb_install_apk.py --apk=ContentShell.apk
14 ./run_all_tests.py content_browsertests --test-apk=ContentShellTest
frankf 2013/06/13 23:17:48 Why does this take --test-apk. These tests don't t
gkanwar 2013/06/17 21:04:43 Done.
15 3. Instrumentation tests (instrumentationtests): Both Python host-driven and
16 Java instrumentation tests are run by default. Use --python_only or
17 --java_only to select one or the other.
18 Example: ./adb_install_apk.py --apk=ChromiumTestShellTest.apk
frankf 2013/06/13 23:17:48 this is already done by -I option. Please update t
gkanwar 2013/06/17 21:04:43 Done.
19 ./run_all_tests.py instrumentationtests
20 --test-apk=ChromiumTestShellTest
21 4. UIAutomator tests (uiautomatortests): Both Python host-driven and Java
22 UIAutomator tests are run by default. Use --python_only or --java_only to
23 select one or the other.
24 Example: ./run_all_tests.py uiautomatortests
25 --test-jar=chromium_testshell_uiautomator_tests
26 --package-name=org.chromium.chrome.testshell
27
28 TODO(gkanwar):
29 * Incorporate the functionality of adb_install_apk.py to allow
30 installing the APK for a test in the same command as running the test.
31 * Add options to run Monkey tests.
32 """
33
34 import optparse
35 import os
36 import sys
37
38 from pylib import cmd_helper
39 from pylib import constants
40 from pylib import ports
41 from pylib.browsertests import dispatch as browsertests_dispatch
42 from pylib.gtest import dispatch as gtest_dispatch
43 from pylib.host_driven import run_python_tests as python_dispatch
44 from pylib.instrumentation import dispatch as instrumentation_dispatch
45 from pylib.uiautomator import dispatch as uiautomator_dispatch
46 from pylib.utils import emulator
47 from pylib.utils import run_tests_helper
48
49 _SDK_OUT_DIR = os.path.join(constants.DIR_SOURCE_ROOT, 'out')
50 VALID_TEST_TYPES = ["gtests", "content_browsertests", "instrumentationtests",
frankf 2013/06/13 23:17:48 Use single quotes consistantly. Please run gpylint
gkanwar 2013/06/17 21:04:43 Done.
51 "uiautomatortests"]
frankf 2013/06/13 23:17:48 instrumentationtests -> instrumentation uiautomato
gkanwar 2013/06/17 21:04:43 Done.
52
53
54 def ValidateTestTypeArg(options, args, option_parser):
55 """Validates that the first arg is a valid test type keyword."""
56 if len(args) < 2:
57 option_parser.error("You must specify a test type.")
frankf 2013/06/13 23:17:48 List the test types here
frankf 2013/06/13 23:17:48 Perhaps only pass in the error method as a paramet
gkanwar 2013/06/17 21:04:43 Test types -- Done.
gkanwar 2013/06/17 21:04:43 Error method -- good idea, changed.
58 if args[1] not in VALID_TEST_TYPES:
59 option_parser.error("Invalid test type. The test type must be one of: " +
60 ', '.join(VALID_TEST_TYPES))
61 options.test_type = args[1]
62
63
64 def AddBuildTypeOption(option_container):
65 """Adds the build type option to the OptionContainer."""
66 default_build_type = 'Debug'
67 if 'BUILDTYPE' in os.environ:
68 default_build_type = os.environ['BUILDTYPE']
69 option_container.add_option('--debug', action='store_const', const='Debug',
70 dest='build_type', default=default_build_type,
71 help=('If set, run test suites under out/Debug. '
72 'Default is env var BUILDTYPE or Debug.'))
73 option_container.add_option('--release', action='store_const',
74 const='Release', dest='build_type',
75 help=('If set, run test suites under out/Release.'
76 ' Default is env var BUILDTYPE or Debug.'))
77
78
79 def AddDeviceOptions(option_container):
80 """Adds all device-related options to the OptionContainer."""
81
82 option_container.add_option('-d', '--device', dest='test_device',
83 help=('Target device for the test suite '
84 'to run on.'))
85 option_container.add_option('-e', '--emulator', dest='use_emulator',
frankf 2013/06/13 23:17:48 You've moved options such as this to common, but t
gkanwar 2013/06/17 21:04:43 Done.
86 action='store_true',
87 help='Run tests in a new instance of emulator.')
88 option_container.add_option('-n', '--emulator-count',
89 type='int', default=1,
90 help=('Number of emulators to launch for '
91 'running the tests.'))
92 option_container.add_option('--abi', default='armeabi-v7a',
93 help='Platform of emulators to launch.')
94
95
96 def ProcessDeviceOptions(options):
97 """Processes emulator and device options."""
98 if options.use_emulator:
99 emulator.DeleteAllTempAVDs()
100
101
102 def AddCommonOptions(option_parser, default_timeout=60):
103 """Adds all common options in an OptionGroup to the OptionParser."""
104
105 option_group = optparse.OptionGroup(option_parser, "Common Options",
106 "Options that apply to all test types.")
107
108 AddBuildTypeOption(option_group)
109 AddDeviceOptions(option_group)
110
111 # --gtest_filter is DEPRECATED. Added for backwards compatibility
112 # with the syntax of the old run_tests.py script.
113 option_group.add_option('-f', '--test_filter', '--gtest_filter',
114 dest='test_filter',
115 help=('Test filter (if not fully qualified, '
116 'will run all matches).'))
117 option_group.add_option('--out-directory', dest='out_directory',
118 help=('Path to the out/ directory, irrespective of '
119 'the build type. Only for non-Chromium uses.'))
120 option_group.add_option('-t', dest='timeout',
121 help='Timeout to wait for each test',
122 type='int',
123 default=default_timeout)
124 option_group.add_option('-c', dest='cleanup_test_files',
125 help='Cleanup test files on the device after run',
126 action='store_true')
127 option_group.add_option('--num_retries', dest='num_retries', type='int',
128 default=2,
129 help=('Number of retries for a test before '
130 'giving up.'))
131 option_group.add_option('-v',
132 '--verbose',
133 dest='verbose_count',
134 default=0,
135 action='count',
136 help='Verbose level (multiple times for more)')
137 profilers = ['devicestatsmonitor', 'chrometrace', 'dumpheap', 'smaps',
138 'traceview']
139 option_group.add_option('--profiler', dest='profilers', action='append',
140 choices=profilers,
141 help=('Profiling tool to run during test. Pass '
142 'multiple times to run multiple profilers. '
143 'Available profilers: %s' % profilers))
144 option_group.add_option('--tool',
145 dest='tool',
146 help=('Run the test under a tool '
147 '(use --tool help to list them)'))
148 option_group.add_option('--flakiness-dashboard-server',
149 dest='flakiness_dashboard_server',
150 help=('Address of the server that is hosting the '
151 'Chrome for Android flakiness dashboard.'))
152 option_group.add_option('--skip-deps-push', dest='push_deps',
153 action='store_false', default=True,
154 help=('Do not push dependencies to the device. '
155 'Use this at own risk for speeding up test '
156 'execution on local machine.'))
157 option_group.add_option('--exit-code', action='store_true',
158 help=('If set, the exit code will be total number '
159 'of failures.'))
160 option_group.add_option('--buildbot-step-failure',
161 action='store_true',
162 help=('If present, will set the buildbot status '
163 'as STEP_FAILURE, otherwise as STEP_WARNINGS '
164 'when test(s) fail.'))
165 option_parser.add_option_group(option_group)
166
167
168 def ProcessCommonOptions(options):
169 """Processes and handles all common options."""
170 if options.out_directory:
171 cmd_helper.OutDirectory.set(options.out_directory)
172 run_tests_helper.SetLogLevel(options.verbose_count)
173
174
175 def AddGTestTestOptions(option_parser):
frankf 2013/06/13 23:17:48 second Test is redunant
gkanwar 2013/06/17 21:04:43 Done.
176 """Adds gtest options in an OptionGroup to the OptionParser"""
frankf 2013/06/13 23:17:48 Rephrase this. e.g. Add gtest options to |option_p
gkanwar 2013/06/17 21:04:43 Done.
177
178 option_group = optparse.OptionGroup(option_parser,
179 'GTest Options',
180 'Use these options to choose which '
181 'test suites to run and how.')
182
183 option_group.add_option('-s', '--suite', dest='test_suite',
184 help=('Executable name of the test suite to run '
185 '(use -s help to list them).'))
186 option_group.add_option('-a', '--test_arguments', dest='test_arguments',
187 help='Additional arguments to pass to the test.')
188 option_group.add_option('-x', '--xvfb', dest='use_xvfb',
189 action='store_true',
190 help='Use Xvfb around tests (ignored if not Linux).')
191 option_group.add_option('--webkit', action='store_true',
192 help='Run the tests from a WebKit checkout.')
193 option_group.add_option('--exe', action='store_true',
194 help='If set, use the exe test runner instead of '
195 'the APK.')
196
197 option_parser.add_option_group(option_group)
198
199
200 def AddJavaTestOptions(option_parser):
201 """Adds the Java test options in an OptionGroup to the OptionContainer."""
202
203 option_group = optparse.OptionGroup(option_parser,
204 'Java Test Options',
205 'Use these options to choose the details '
206 'of which tests to run, and how to run '
frankf 2013/06/13 23:17:48 Not sure if this doc is conveying any information.
gkanwar 2013/06/17 21:04:43 Done.
207 'them.')
208 option_group.add_option(
209 '-A', '--annotation', dest='annotation_str',
210 help=('Comma-separated list of annotations. Run only tests with any of '
211 'the given annotations. An annotation can be either a key or a '
212 'key-values pair. A test that has no annotation is considered '
213 '"SmallTest".'))
214 option_group.add_option(
215 '-E', '--exclude-annotation', dest='exclude_annotation_str',
216 help=('Comma-separated list of annotations. Exclude tests with these '
217 'annotations.'))
218 option_group.add_option('-j', '--java_only', action='store_true',
219 default=False, help='Run only the Java tests.')
220 option_group.add_option('-p', '--python_only', action='store_true',
221 default=False, help='Run only the host-driven tests.')
222 option_group.add_option('--screenshot', dest='screenshot_failures',
223 action='store_true',
224 help='Capture screenshots of test failures')
225 option_group.add_option('--save-perf-json', action='store_true',
226 help='Saves the JSON file for each UI Perf test.')
227 option_group.add_option('--shard_retries', type=int, default=1,
frankf 2013/06/13 23:17:48 I think this is not used anywhere and is specific
gkanwar 2013/06/17 21:04:43 Done.
228 help=('Number of times to retry each failure when '
229 'sharding.'))
230 option_group.add_option('--official-build', help='Run official build tests.')
231 option_group.add_option('--python_test_root',
232 help='Root of the host-driven tests.')
233 option_group.add_option('--keep_test_server_ports',
234 action='store_true',
235 help=('Indicates the test server ports must be '
236 'kept. When this is run via a sharder '
237 'the test server ports should be kept and '
238 'should not be reset.'))
239 option_group.add_option('--disable_assertions', action='store_true',
240 help='Run with java assertions disabled.')
241 option_group.add_option('--test_data', action='append', default=[],
242 help=('Each instance defines a directory of test '
243 'data that should be copied to the target(s) '
244 'before running the tests. The argument '
245 'should be of the form <target>:<source>, '
246 '<target> is relative to the device data'
247 'directory, and <source> is relative to the '
248 'chromium build directory.'))
249
250 AddInstrumentationOptions(option_group)
251 AddUIAutomatorOptions(option_group)
frankf 2013/06/13 23:17:48 To avoid confusion, let's have separate OptionGrou
gkanwar 2013/06/17 21:04:43 Done.
252
253 option_parser.add_option_group(option_group)
254
255
256 def ValidateJavaTestOptions(options, option_parser):
257 """Validates options/arguments and populates options with defaults."""
frankf 2013/06/13 23:17:48 Let's just call these Process instead of Validate
gkanwar 2013/06/17 21:04:43 Done.
258
259 if options.java_only and options.python_only:
260 option_parser.error('Options java_only (-j) and python_only (-p) '
261 'are mutually exclusive.')
262 options.run_java_tests = True
263 options.run_python_tests = True
264 if options.java_only:
265 options.run_python_tests = False
266 elif options.python_only:
267 options.run_java_tests = False
268
269 if not options.python_test_root:
270 options.run_python_tests = False
271
272 if options.annotation_str:
273 options.annotations = options.annotation_str.split(',')
274 elif options.test_filter:
275 options.annotations = []
276 else:
277 options.annotations = ['Smoke', 'SmallTest', 'MediumTest', 'LargeTest']
278
279 if options.exclude_annotation_str:
280 options.exclude_annotations = options.exclude_annotation_str.split(',')
281 else:
282 options.exclude_annotations = []
283
284 if not options.keep_test_server_ports:
285 if not ports.ResetTestServerPortAllocation():
286 raise Exception('Failed to reset test server port.')
287
288
289 def AddInstrumentationOptions(option_container):
290 """Adds java/python instrumentation test options to the OptionContainer."""
frankf 2013/06/13 23:17:48 remove "java/python"
frankf 2013/06/13 23:17:48 What's an OptionContainer, you mean OptionGroup?
gkanwar 2013/06/17 21:04:43 "java/python" -- Done.
gkanwar 2013/06/17 21:04:43 I used OptionContainer here because these could be
291 option_container.add_option('-w', '--wait_debugger', dest='wait_for_debugger',
292 action='store_true',
293 help='(Instrumentation only) Wait for debugger.')
294 option_container.add_option('-I', dest='install_apk', action='store_true',
295 help='(Instrumentation only) Install APK.')
frankf 2013/06/13 23:17:48 "Install test APK"
gkanwar 2013/06/17 21:04:43 Done.
296 option_container.add_option(
297 '--test-apk', dest='test_apk',
298 help=('(Instrumentation only) The name of the apk containing the tests '
299 '(without the .apk extension; e.g. "ContentShellTest"). '
300 'Alternatively, this can be a full path to the apk.'))
301
302
303 def ValidateInstrumentationOptions(options, option_parser):
304 """Validates options/arguments and populate options with defaults."""
305
306 ValidateJavaTestOptions(options, option_parser)
307
308 if not options.test_apk:
309 option_parser.error('--test-apk must be specified.')
310
311 if os.path.exists(options.test_apk):
312 # The APK is fully qualified, assume the JAR lives along side.
313 options.test_apk_path = options.test_apk
314 options.test_apk_jar_path = (os.path.splitext(options.test_apk_path)[0] +
315 '.jar')
316 else:
317 options.test_apk_path = os.path.join(_SDK_OUT_DIR,
318 options.build_type,
319 constants.SDK_BUILD_APKS_DIR,
320 '%s.apk' % options.test_apk)
321 options.test_apk_jar_path = os.path.join(
322 _SDK_OUT_DIR, options.build_type, constants.SDK_BUILD_TEST_JAVALIB_DIR,
323 '%s.jar' % options.test_apk)
324
325
326 def AddUIAutomatorOptions(option_container):
327 """Adds UI Automator test options to the OptionContainer."""
328 option_container.add_option(
329 '--package-name',
330 help=('(UIAutomator only) The package name used by the apk '
331 'containing the application.'))
332 option_container.add_option(
333 '--test-jar', dest='test_jar',
334 help=('(UIAutomator only) The name of the dexed jar containing the tests '
335 '(without the .dex.jar extension). Alternatively, this can be a '
336 'full path to the jar.'))
337
338
339 def ValidateUIAutomatorOptions(options, option_parser):
340 """Validates UIAutomator options/arguments."""
341
342 ValidateJavaTestOptions(options, option_parser)
343
344 if not options.package_name:
345 option_parser.error('--package-name must be specified.')
346
347 if not options.test_jar:
348 option_parser.error('--test-jar must be specified.')
349
350 if os.path.exists(options.test_jar):
351 # The dexed JAR is fully qualified, assume the info JAR lives along side.
352 options.uiautomator_jar = options.test_jar
353 else:
354 options.uiautomator_jar = os.path.join(
355 _SDK_OUT_DIR, options.build_type, constants.SDK_BUILD_JAVALIB_DIR,
356 '%s.dex.jar' % options.test_jar)
357 options.uiautomator_info_jar = (
358 options.uiautomator_jar[:options.uiautomator_jar.find('.dex.jar')] +
359 '_java.jar')
360
361
362 def RunTests(options, option_parser):
363 """Checks test type and dispatches to the appropriate function."""
364
365 total_failed = 0
366 if options.test_type == "gtests":
367 total_failed += gtest_dispatch.Dispatch(options)
frankf 2013/06/13 23:17:48 No need to aggregate the count since we only run o
gkanwar 2013/06/17 21:04:43 Done.
368 elif options.test_type == "content_browsertests":
369 total_failed += browsertests_dispatch.Dispatch(options)
370 elif options.test_type == "instrumentationtests":
371 ValidateInstrumentationOptions(options, option_parser)
372 if options.run_python_tests:
373 total_failed += python_dispatch.Dispatch(options)
frankf 2013/06/13 23:17:48 This is run after instrumentation tests. Same belo
gkanwar 2013/06/17 21:04:43 Done.
374 if options.run_java_tests:
375 total_failed += instrumentation_dispatch.Dispatch(options)
376 elif options.test_type == "uiautomatortests":
377 ValidateUIAutomatorOptions(options, option_parser)
378 if options.run_python_tests:
379 total_failed += python_dispatch.Dispatch(options)
380 if options.run_java_tests:
381 total_failed += uiautomator_dispatch.Dispatch(options)
382 else:
383 raise Exception('Unknown test type state')
384
385 return total_failed
386
387
388 def main(argv):
389 option_parser = optparse.OptionParser(
390 usage="Usage: %prog test_type [options]")
391 AddCommonOptions(option_parser)
392 AddGTestTestOptions(option_parser)
393 AddJavaTestOptions(option_parser)
394 options, args = option_parser.parse_args(argv)
395
396 ValidateTestTypeArg(options, args, option_parser)
397 ProcessCommonOptions(options)
398 ProcessDeviceOptions(options)
399
400 failed_tests_count = RunTests(options, option_parser)
401
402 # Failures of individual test suites are communicated by printing a
403 # STEP_FAILURE message.
404 # Returning a success exit status also prevents the buildbot from incorrectly
405 # marking the last suite as failed if there were failures in other suites in
406 # the batch (this happens because the exit status is a sum of all failures
407 # from all suites, but the buildbot associates the exit status only with the
408 # most recent step).
409 if options.exit_code:
410 return failed_tests_count
411 return 0
412
413
414 if __name__ == '__main__':
415 sys.exit(main(sys.argv))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698