OLD | NEW |
---|---|
(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. | |
craigdh
2013/06/12 18:37:03
I don't like the name run_all_tests.py. This name
gkanwar
2013/06/13 00:12:34
I agree that run_all_tests.py is not the best name
| |
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/12 04:31:46
Let's make test type a positional arg instead of a
gkanwar
2013/06/13 00:12:34
Done.
| |
12 2. ContentBrowser unit tests (--contentbrowsertests) | |
13 Example: ./adb_install_apk.py --apk=ContentShell.apk --release | |
frankf
2013/06/12 04:31:46
As said before, remove --release.
gkanwar
2013/06/13 00:12:34
Done.
| |
14 ./run_all_tests.py --contentbrowsertests --test-apk=ContentShellTest | |
frankf
2013/06/12 04:31:46
content_browsertests
gkanwar
2013/06/13 00:12:34
Done.
| |
15 --release | |
16 3. Instrumentation tests (--instrumentationtests): Both Python host-driven and | |
17 Java instrumentation tests are run by default. Use --python_only or | |
18 --java_only to select one or the other. | |
19 3a. Python host-driven | |
frankf
2013/06/12 04:31:46
Let's remove instructions for host-driven all toge
gkanwar
2013/06/13 00:12:34
Done.
| |
20 Example: ./run_all_tests.py --instrumentationtests --python_only --release | |
21 --test-apk=ChromiumTestShellTest | |
22 --python_test_root=src/chrome/android/ | |
23 3b. Java instrumentation tests | |
24 Example: ./adb_install_apk.py --apk=ChromiumTestShellTest.apk --release | |
25 ./run_all_tests.py --instrumentationtests --java_only --release | |
26 --test-apk=ChromiumTestShellTest | |
27 4. UIAutomator tests (--uiautomatortests): Both Python host-drive and Java | |
28 UIAutomator tests are run by default. Use --python_only or --java_only to | |
29 select one or the other. | |
30 4a. Python host-driven (see 3a) | |
31 4b. Java UIAutomator tests | |
32 Example: ./run_all_tests.py --uiautomatortests --java_only | |
33 --test-jar=chromium_testshell_uiautomator_tests | |
34 --package-name=org.chromium.chrome.testshell --release | |
35 | |
36 TODO(gkanwar): | |
37 * Incorporate the functionality of adb_install_apk.py to allow | |
38 installing the APK for a test in the same command as running the test. | |
39 * Add options to run Monkey tests. | |
40 * Allow running many test types non-exclusively from the same command. | |
frankf
2013/06/12 04:31:46
Running multiple types of tests in not an importan
gkanwar
2013/06/13 00:12:34
Done.
| |
41 """ | |
42 | |
43 import optparse | |
44 import os | |
45 import sys | |
46 | |
47 from pylib import cmd_helper | |
48 from pylib import constants | |
49 from pylib import ports | |
50 from pylib.browsertests import dispatch as browsertests_dispatch | |
51 from pylib.gtest import dispatch as gtest_dispatch | |
52 from pylib.host_driven import run_python_tests as python_dispatch | |
53 from pylib.instrumentation import dispatch as instrumentation_dispatch | |
54 from pylib.uiautomator import dispatch as uiautomator_dispatch | |
55 from pylib.utils import emulator | |
56 from pylib.utils import run_tests_helper | |
57 | |
58 _SDK_OUT_DIR = os.path.join(constants.DIR_SOURCE_ROOT, 'out') | |
59 | |
60 | |
61 def AddTestTypeOption(option_parser): | |
62 """Decorates OptionParser with test type options in an OptionGroup.""" | |
craigdh
2013/06/12 18:37:03
Decorate has a specific meaning in Python which do
gkanwar
2013/06/13 00:12:34
I was attempting to follow the pattern of comments
| |
63 | |
64 option_group = optparse.OptionGroup(option_parser, 'Test Type Options', | |
65 'Select one of the test types ' | |
66 'below to run.') | |
67 | |
68 # TODO(gkanwar): Support running multiple test types from | |
69 # one command. | |
70 option_group.add_option('--gtests', action='store_true', | |
71 dest='gtests_only', default=False, | |
72 help='If set, run GTest unit tests. See the ' | |
73 'GTest flags for more detail on options.') | |
74 option_group.add_option('--contentbrowsertests', action='store_true', | |
75 dest='contentbrowsertests_only', default=False, | |
76 help='If set, run contentbrowser unit tests. ' | |
craigdh
2013/06/12 18:37:03
contentbrowser -> content_browsertests
gkanwar
2013/06/13 00:12:34
Done.
| |
77 'See the contentbrowser flags for more detail ' | |
78 'on options.') | |
79 option_group.add_option('--instrumentationtests', action='store_true', | |
80 dest='instrumentationtests_only', default=False, | |
81 help='If set, run java/python instrumentation ' | |
82 'tests. See the instrumentation flags for more ' | |
83 'detail on options.') | |
84 option_group.add_option('--uiautomatortests', action='store_true', | |
85 dest='uiautomatortests_only', default=False, | |
86 help='If set, run UIAutomator tests. See the ' | |
87 ' UIAutomator flags for more detail on options.') | |
88 | |
89 option_parser.add_option_group(option_group) | |
90 | |
91 | |
92 def ValidateTestTypeOption(options, option_parser): | |
93 """Validates that exactly one of the test type options are set.""" | |
94 if sum([options.gtests_only, options.contentbrowsertests_only, | |
95 options.instrumentationtests_only, | |
96 options.uiautomatortests_only]) != 1: | |
97 option_parser.error('Exactly one of the test type flags must be set') | |
98 | |
99 | |
100 def AddBuildTypeOption(option_container): | |
101 """Decorates OptionContainer with build type option.""" | |
102 default_build_type = 'Debug' | |
103 if 'BUILDTYPE' in os.environ: | |
104 default_build_type = os.environ['BUILDTYPE'] | |
105 option_container.add_option('--debug', action='store_const', const='Debug', | |
106 dest='build_type', default=default_build_type, | |
107 help='If set, run test suites under out/Debug. ' | |
108 'Default is env var BUILDTYPE or Debug.') | |
109 option_container.add_option('--release', action='store_const', | |
110 const='Release', dest='build_type', | |
111 help='If set, run test suites under out/Release. ' | |
112 'Default is env var BUILDTYPE or Debug.') | |
113 | |
114 | |
115 def AddDeviceOptions(option_container): | |
116 """Decorates OptionContainer with all device-related options.""" | |
117 | |
118 option_container.add_option('-d', '--device', dest='test_device', | |
119 help='Target device for the test suite ' | |
120 'to run on.') | |
121 option_container.add_option('-e', '--emulator', dest='use_emulator', | |
122 action='store_true', | |
123 help='Run tests in a new instance of emulator.') | |
124 option_container.add_option('-n', '--emulator-count', | |
125 type='int', default=1, | |
126 help='Number of emulators to launch for ' | |
127 'running the tests.') | |
128 option_container.add_option('--abi', default='armeabi-v7a', | |
129 help='Platform of emulators to launch.') | |
130 | |
131 | |
132 def ProcessDeviceOptions(options): | |
133 """Processes emulator and device options.""" | |
134 if options.use_emulator: | |
135 emulator.DeleteAllTempAVDs() | |
136 | |
137 | |
138 def AddCommonOptions(option_parser, default_timeout=60): | |
139 """Decorates OptionParser with all common options in an OptionGroup.""" | |
140 | |
141 option_group = optparse.OptionGroup(option_parser, "Common Options", | |
142 "Options that apply to all test types.") | |
143 | |
144 AddBuildTypeOption(option_group) | |
145 AddDeviceOptions(option_group) | |
146 | |
147 # --gtest_filter is DEPRECATED. Added for backwards compatibility | |
148 # with the syntax of the old run_tests.py script. | |
149 option_group.add_option('-f', '--test_filter', '--gtest_filter', | |
150 dest='test_filter', | |
151 help='Test filter (if not fully qualified, ' | |
152 'will run all matches).') | |
153 option_group.add_option('--out-directory', dest='out_directory', | |
154 help='Path to the out/ directory, irrespective of ' | |
155 'the build type. Only for non-Chromium uses.') | |
156 option_group.add_option('-t', dest='timeout', | |
157 help='Timeout to wait for each test', | |
158 type='int', | |
159 default=default_timeout) | |
160 option_group.add_option('-c', dest='cleanup_test_files', | |
161 help='Cleanup test files on the device after run', | |
162 action='store_true') | |
163 option_group.add_option('--num_retries', dest='num_retries', type='int', | |
164 default=2, | |
165 help='Number of retries for a test before ' | |
166 'giving up.') | |
craigdh
2013/06/12 18:37:03
Be consistent (and follow PEP8) with indenting. Co
gkanwar
2013/06/13 00:12:34
Done.
| |
167 option_group.add_option('-v', | |
168 '--verbose', | |
169 dest='verbose_count', | |
170 default=0, | |
171 action='count', | |
172 help='Verbose level (multiple times for more)') | |
173 profilers = ['devicestatsmonitor', 'chrometrace', 'dumpheap', 'smaps', | |
174 'traceview'] | |
175 option_group.add_option('--profiler', dest='profilers', action='append', | |
176 choices=profilers, | |
177 help='Profiling tool to run during test. ' | |
178 'Pass multiple times to run multiple profilers. ' | |
179 'Available profilers: %s' % profilers) | |
180 option_group.add_option('--tool', | |
181 dest='tool', | |
182 help='Run the test under a tool ' | |
183 '(use --tool help to list them)') | |
184 option_group.add_option('--flakiness-dashboard-server', | |
185 dest='flakiness_dashboard_server', | |
186 help=('Address of the server that is hosting the ' | |
187 'Chrome for Android flakiness dashboard.')) | |
188 option_group.add_option('--skip-deps-push', dest='push_deps', | |
189 action='store_false', default=True, | |
190 help='Do not push dependencies to the device. ' | |
191 'Use this at own risk for speeding up test ' | |
192 'execution on local machine.') | |
193 option_group.add_option('--exit-code', action='store_true', | |
194 help='If set, the exit code will be total number ' | |
195 'of failures.') | |
196 option_group.add_option('--buildbot-step-failure', | |
197 action='store_true', | |
198 help=('If present, will set the buildbot status ' | |
199 'as STEP_FAILURE, otherwise as STEP_WARNINGS ' | |
200 'when test(s) fail.')) | |
201 option_parser.add_option_group(option_group) | |
202 | |
203 | |
204 def ProcessCommonOptions(options): | |
205 """Process and handle all common options.""" | |
206 if options.out_directory: | |
207 cmd_helper.OutDirectory.set(options.out_directory) | |
208 run_tests_helper.SetLogLevel(options.verbose_count) | |
209 | |
210 | |
211 def AddGTestContentBrowserTestOptions(option_parser): | |
frankf
2013/06/12 04:31:46
As said before, this should be AddGTestOptions, th
gkanwar
2013/06/13 00:12:34
Done.
| |
212 """Decorates OptionParser with gtest/browsertest options in an OptionGroup.""" | |
213 | |
214 option_group = optparse.OptionGroup(option_parser, | |
215 'GTest/ContentBrowserTest Options', | |
216 'Use these options to choose which ' | |
217 'test suites to run and how.') | |
218 | |
219 option_group.add_option('-s', '--suite', dest='test_suite', | |
220 help='Executable name of the test suite to run ' | |
221 '(use -s help to list them).') | |
222 option_group.add_option('-a', '--test_arguments', dest='test_arguments', | |
223 help='Additional arguments to pass to the test.') | |
224 option_group.add_option('-x', '--xvfb', dest='use_xvfb', | |
225 action='store_true', | |
226 help='Use Xvfb around tests (ignored if not Linux).') | |
227 option_group.add_option('--webkit', action='store_true', | |
228 help='Run the tests from a WebKit checkout.') | |
229 option_group.add_option('--exe', action='store_true', | |
230 help='If set, use the exe test runner instead of ' | |
231 'the APK.') | |
232 | |
233 option_parser.add_option_group(option_group) | |
234 | |
235 | |
236 def AddInstrumentationUIAutomatorOptions(option_parser): | |
frankf
2013/06/12 04:31:46
Let's call these 'java tests' here and in the help
gkanwar
2013/06/13 00:12:34
Done.
| |
237 """Decorates OptionParser with Instrumentation/UIAutomator test options.""" | |
238 | |
239 option_group = optparse.OptionGroup(option_parser, | |
240 'InstrumentationTest/UIAutomator Options', | |
241 'Use these options to choose the details ' | |
242 'of which tests to run, and how to run ' | |
243 'them.') | |
244 option_group.add_option( | |
245 '-A', '--annotation', dest='annotation_str', | |
246 help=('Comma-separated list of annotations. Run only tests with any of ' | |
247 'the given annotations. An annotation can be either a key or a ' | |
248 'key-values pair. A test that has no annotation is considered ' | |
249 '"SmallTest".')) | |
250 option_group.add_option( | |
251 '-E', '--exclude-annotation', dest='exclude_annotation_str', | |
252 help=('Comma-separated list of annotations. Exclude tests with these ' | |
253 'annotations.')) | |
254 option_group.add_option('-j', '--java_only', action='store_true', | |
255 default=False, help='Run only the Java tests.') | |
256 option_group.add_option('-p', '--python_only', action='store_true', | |
257 default=False, help='Run only the Python tests.') | |
frankf
2013/06/12 04:31:46
Python -> host-driven
gkanwar
2013/06/13 00:12:34
Done.
| |
258 option_group.add_option('--screenshot', dest='screenshot_failures', | |
259 action='store_true', | |
260 help='Capture screenshots of test failures') | |
261 option_group.add_option('--save-perf-json', action='store_true', | |
262 help='Saves the JSON file for each UI Perf test.') | |
263 option_group.add_option('--shard_retries', type=int, default=1, | |
264 help=('Number of times to retry each failure when ' | |
265 'sharding.')) | |
266 option_group.add_option('--official-build', help='Run official build tests.') | |
267 option_group.add_option('--python_test_root', | |
268 help='Root of the python-driven tests.') | |
frankf
2013/06/12 04:31:46
Same here.
gkanwar
2013/06/13 00:12:34
Done.
| |
269 option_group.add_option('--keep_test_server_ports', | |
270 action='store_true', | |
271 help='Indicates the test server ports must be ' | |
272 'kept. When this is run via a sharder ' | |
273 'the test server ports should be kept and ' | |
274 'should not be reset.') | |
275 option_group.add_option('--disable_assertions', action='store_true', | |
276 help='Run with java assertions disabled.') | |
277 option_group.add_option('--test_data', action='append', default=[], | |
278 help=('Each instance defines a directory of test ' | |
279 'data that should be copied to the target(s) ' | |
280 'before running the tests. The argument ' | |
281 'should be of the form <target>:<source>, ' | |
282 '<target> is relative to the device data' | |
283 'directory, and <source> is relative to the ' | |
284 'chromium build directory.')) | |
285 | |
286 AddInstrumentationOptions(option_group) | |
287 AddUIAutomatorOptions(option_group) | |
288 | |
289 option_parser.add_option_group(option_group) | |
290 | |
291 | |
292 def ValidateInstrumentationUIAutomatorOptions(options, option_parser): | |
293 """Validate options/arguments and populate options with defaults.""" | |
294 | |
295 # Common options | |
296 if options.java_only and options.python_only: | |
297 option_parser.error('Options java_only (-j) and python_only (-p) ' | |
298 'are mutually exclusive.') | |
299 options.run_java_tests = True | |
300 options.run_python_tests = True | |
301 if options.java_only: | |
302 options.run_python_tests = False | |
303 elif options.python_only: | |
304 options.run_java_tests = False | |
305 | |
306 if not options.python_test_root: | |
307 options.run_python_tests = False | |
308 | |
309 if options.annotation_str: | |
310 options.annotations = options.annotation_str.split(',') | |
311 elif options.test_filter: | |
312 options.annotations = [] | |
313 else: | |
314 options.annotations = ['Smoke', 'SmallTest', 'MediumTest', 'LargeTest'] | |
315 | |
316 if options.exclude_annotation_str: | |
317 options.exclude_annotations = options.exclude_annotation_str.split(',') | |
318 else: | |
319 options.exclude_annotations = [] | |
320 | |
321 if not options.keep_test_server_ports: | |
322 if not ports.ResetTestServerPortAllocation(): | |
323 raise Exception('Failed to reset test server port.') | |
324 | |
325 # Validate only one of the UIAutomator or Instrumentation options, | |
326 # determined based on the --test-apk or --test-jar flag. | |
frankf
2013/06/12 04:31:46
Why don't you validate based on the test type arg?
gkanwar
2013/06/13 00:12:34
This was a remnant of having Instrumentation and U
| |
327 options.run_uiautomator_tests = False | |
328 options.run_nonuiautomator_tests = False | |
329 if not (options.test_apk or options.test_jar): | |
330 option_parser.error('You must specify at least one of --test-apk ' | |
331 '(for Instrumentation tests) and --test-jar ' | |
332 '(for UIAutomator tests).') | |
333 if options.test_apk: # Assume that having an APK means Instrumentation | |
334 options.run_nonuiautomator_tests = True | |
335 ValidateInstrumentationOptions(options, option_parser) | |
336 if options.test_jar: # Assume that having a JAR means UIAutomator | |
337 options.run_uiautomator_tests = True | |
338 ValidateUIAutomatorOptions(options, option_parser) | |
339 | |
340 | |
341 def AddInstrumentationOptions(option_container): | |
342 """Add java/python instrumentation test options.""" | |
343 option_container.add_option('-w', '--wait_debugger', dest='wait_for_debugger', | |
344 action='store_true', | |
345 help='(Instrumentation only) Wait for debugger.') | |
346 option_container.add_option('-I', dest='install_apk', action='store_true', | |
347 help='(Instrumentation only) Install APK.') | |
348 option_container.add_option( | |
349 '--test-apk', dest='test_apk', | |
350 help=('(Instrumentation only) The name of the apk containing the tests ' | |
351 '(without the .apk extension; e.g. "ContentShellTest"). ' | |
352 'Alternatively, this can be a full path to the apk.')) | |
353 | |
354 | |
355 def ValidateInstrumentationOptions(options, option_parser): | |
356 """Validate options/arguments and populate options with defaults.""" | |
357 | |
358 if not options.test_apk: | |
359 option_parser.error('--test-apk must be specified.') | |
360 | |
361 if os.path.exists(options.test_apk): | |
362 # The APK is fully qualified, assume the JAR lives along side. | |
363 options.test_apk_path = options.test_apk | |
364 options.test_apk_jar_path = (os.path.splitext(options.test_apk_path)[0] + | |
365 '.jar') | |
366 else: | |
367 options.test_apk_path = os.path.join(_SDK_OUT_DIR, | |
368 options.build_type, | |
369 constants.SDK_BUILD_APKS_DIR, | |
370 '%s.apk' % options.test_apk) | |
371 options.test_apk_jar_path = os.path.join( | |
372 _SDK_OUT_DIR, options.build_type, constants.SDK_BUILD_TEST_JAVALIB_DIR, | |
373 '%s.jar' % options.test_apk) | |
374 | |
375 | |
376 def AddUIAutomatorOptions(option_container): | |
377 """Add UI Automator test options.""" | |
378 option_container.add_option( | |
379 '--package-name', | |
380 help=('(UIAutomator only) The package name used by the apk ' | |
381 'containing the application.')) | |
382 option_container.add_option( | |
383 '--test-jar', dest='test_jar', | |
384 help=('(UIAutomator only) The name of the dexed jar containing the tests ' | |
385 '(without the .dex.jar extension). Alternatively, this can be a ' | |
386 'full path to the jar.')) | |
387 | |
388 | |
389 def ValidateUIAutomatorOptions(options, option_parser): | |
390 """Validate UIAutomator options/arguments.""" | |
391 | |
392 if not options.package_name: | |
393 option_parser.error('--package-name must be specified.') | |
394 | |
395 if not options.test_jar: | |
396 option_parser.error('--test-jar must be specified.') | |
397 | |
398 if os.path.exists(options.test_jar): | |
399 # The dexed JAR is fully qualified, assume the info JAR lives along side. | |
400 options.uiautomator_jar = options.test_jar | |
401 else: | |
402 options.uiautomator_jar = os.path.join( | |
403 _SDK_OUT_DIR, options.build_type, constants.SDK_BUILD_JAVALIB_DIR, | |
404 '%s.dex.jar' % options.test_jar) | |
405 options.uiautomator_info_jar = ( | |
406 options.uiautomator_jar[:options.uiautomator_jar.find('.dex.jar')] + | |
407 '_java.jar') | |
408 | |
409 | |
410 def RunTests(options, option_parser): | |
411 """Checks test type and dispatches to the appropriate function.""" | |
412 | |
413 total_failed = 0 | |
414 if options.gtests_only: | |
415 total_failed += gtest_dispatch.Dispatch(options) | |
416 elif options.contentbrowsertests_only: | |
417 total_failed += browsertests_dispatch.Dispatch(options) | |
418 elif options.instrumentationtests_only: | |
419 ValidateInstrumentationUIAutomatorOptions(options, option_parser) | |
420 if options.run_python_tests: | |
421 total_failed += python_dispatch.Dispatch(options) | |
422 if options.run_java_tests: | |
423 total_failed += instrumentation_dispatch.Dispatch(options) | |
424 elif options.uiautomatortests_only: | |
425 ValidateInstrumentationUIAutomatorOptions(options, option_parser) | |
426 if options.run_python_tests: | |
427 total_failed += python_dispatch.Dispatch(options) | |
428 if options.run_java_tests: | |
429 total_failed += uiautomator_dispatch.Dispatch(options) | |
430 else: | |
431 raise Exception('Unknown test type state') | |
432 | |
433 return total_failed | |
434 | |
435 | |
436 def main(argv): | |
437 option_parser = optparse.OptionParser() | |
438 AddTestTypeOption(option_parser) | |
439 AddCommonOptions(option_parser) | |
440 AddGTestContentBrowserTestOptions(option_parser) | |
441 AddInstrumentationUIAutomatorOptions(option_parser) | |
442 options, args = option_parser.parse_args(argv) | |
443 | |
444 ProcessCommonOptions(options) | |
445 ProcessDeviceOptions(options) | |
446 | |
447 ValidateTestTypeOption(options, option_parser) | |
frankf
2013/06/12 04:31:46
Move this to line 443
gkanwar
2013/06/13 00:12:34
Done.
| |
448 | |
449 failed_tests_count = RunTests(options, option_parser) | |
450 | |
451 # Failures of individual test suites are communicated by printing a | |
452 # STEP_FAILURE message. | |
453 # Returning a success exit status also prevents the buildbot from incorrectly | |
454 # marking the last suite as failed if there were failures in other suites in | |
455 # the batch (this happens because the exit status is a sum of all failures | |
456 # from all suites, but the buildbot associates the exit status only with the | |
457 # most recent step). | |
458 if options.exit_code: | |
459 return failed_tests_count | |
460 return 0 | |
461 | |
462 | |
463 if __name__ == '__main__': | |
464 sys.exit(main(sys.argv)) | |
OLD | NEW |