OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
frankf
2013/07/01 22:36:21
How about naming this test_runner.py?
gkanwar
2013/07/01 23:46:37
I feel like test_runner.py makes me feel like the
| |
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 (gtest) | |
frankf
2013/07/01 22:36:21
--help provides all this info, let's just remove t
gkanwar
2013/07/01 23:46:37
Done.
| |
11 2. ContentBrowser unit tests (content_browsertests) | |
12 3. Instrumentation tests (instrumentation): Both Python host-driven and | |
13 Java instrumentation tests are run by default. Use --python_only or | |
14 --java_only to select one or the other. | |
15 4. UIAutomator tests (uiautomator): Both Python host-driven and Java | |
16 UIAutomator tests are run by default. Use --python_only or --java_only to | |
17 select one or the other. | |
18 | |
19 TODO(gkanwar): | |
20 * Add options to run Monkey tests. | |
21 """ | |
22 | |
23 import collections | |
24 import optparse | |
25 import os | |
26 import sys | |
27 | |
28 from pylib import cmd_helper | |
29 from pylib import constants | |
30 from pylib import ports | |
31 from pylib.browsertests import dispatch as browsertests_dispatch | |
32 from pylib.gtest import dispatch as gtest_dispatch | |
33 from pylib.host_driven import run_python_tests as python_dispatch | |
34 from pylib.instrumentation import dispatch as instrumentation_dispatch | |
35 from pylib.uiautomator import dispatch as uiautomator_dispatch | |
36 from pylib.utils import emulator | |
37 from pylib.utils import run_tests_helper | |
38 | |
39 _SDK_OUT_DIR = os.path.join(constants.DIR_SOURCE_ROOT, 'out') | |
40 | |
41 | |
42 def AddBuildTypeOption(option_parser): | |
43 """Adds the build type option to option_parser.""" | |
44 default_build_type = 'Debug' | |
45 if 'BUILDTYPE' in os.environ: | |
46 default_build_type = os.environ['BUILDTYPE'] | |
47 option_parser.add_option('--debug', action='store_const', const='Debug', | |
48 dest='build_type', default=default_build_type, | |
49 help=('If set, run test suites under out/Debug. ' | |
50 'Default is env var BUILDTYPE or Debug.')) | |
51 option_parser.add_option('--release', action='store_const', | |
52 const='Release', dest='build_type', | |
53 help=('If set, run test suites under out/Release.' | |
54 ' Default is env var BUILDTYPE or Debug.')) | |
55 | |
56 | |
57 def AddEmulatorOptions(option_parser): | |
58 """Adds all emulator-related options to option_parser.""" | |
59 | |
60 # TODO(gkanwar): Figure out what we're doing with the emulator setup | |
61 # and determine whether these options should be deprecated/removed. | |
62 option_parser.add_option('-e', '--emulator', dest='use_emulator', | |
63 action='store_true', | |
64 help='Run tests in a new instance of emulator.') | |
65 option_parser.add_option('-n', '--emulator-count', | |
66 type='int', default=1, | |
67 help=('Number of emulators to launch for ' | |
68 'running the tests.')) | |
69 option_parser.add_option('--abi', default='armeabi-v7a', | |
70 help='Platform of emulators to launch.') | |
71 | |
72 | |
73 def ProcessEmulatorOptions(options): | |
74 """Processes emulator options.""" | |
75 if options.use_emulator: | |
76 emulator.DeleteAllTempAVDs() | |
77 | |
78 | |
79 def AddCommonOptions(option_parser): | |
80 """Adds all common options to option_parser.""" | |
81 | |
82 AddBuildTypeOption(option_parser) | |
83 | |
84 # --gtest_filter is DEPRECATED. Added for backwards compatibility | |
85 # with the syntax of the old run_tests.py script. | |
86 option_parser.add_option('-f', '--test_filter', '--gtest_filter', | |
frankf
2013/06/28 23:07:10
Let's not make this a common option. gtest_filter
gkanwar
2013/07/01 22:12:00
Done.
| |
87 dest='test_filter', | |
88 help=('Test filter (if not fully qualified, ' | |
89 'will run all matches).')) | |
90 option_parser.add_option('--out-directory', dest='out_directory', | |
91 help=('Path to the out/ directory, irrespective of ' | |
92 'the build type. Only for non-Chromium uses.')) | |
93 option_parser.add_option('-c', dest='cleanup_test_files', | |
94 help='Cleanup test files on the device after run', | |
95 action='store_true') | |
96 option_parser.add_option('--num_retries', dest='num_retries', type='int', | |
97 default=2, | |
98 help=('Number of retries for a test before ' | |
99 'giving up.')) | |
100 option_parser.add_option('-v', | |
101 '--verbose', | |
102 dest='verbose_count', | |
103 default=0, | |
104 action='count', | |
105 help='Verbose level (multiple times for more)') | |
106 profilers = ['devicestatsmonitor', 'chrometrace', 'dumpheap', 'smaps', | |
107 'traceview'] | |
108 option_parser.add_option('--profiler', dest='profilers', action='append', | |
109 choices=profilers, | |
110 help=('Profiling tool to run during test. Pass ' | |
111 'multiple times to run multiple profilers. ' | |
112 'Available profilers: %s' % profilers)) | |
113 option_parser.add_option('--tool', | |
114 dest='tool', | |
115 help=('Run the test under a tool ' | |
116 '(use --tool help to list them)')) | |
117 option_parser.add_option('--flakiness-dashboard-server', | |
118 dest='flakiness_dashboard_server', | |
119 help=('Address of the server that is hosting the ' | |
craigdh
2013/07/01 18:28:52
no need for () around multi-line strings that are
gkanwar
2013/07/01 22:12:00
This was the style from the previous file (test_op
| |
120 'Chrome for Android flakiness dashboard.')) | |
121 option_parser.add_option('--skip-deps-push', dest='push_deps', | |
122 action='store_false', default=True, | |
123 help=('Do not push dependencies to the device. ' | |
124 'Use this at own risk for speeding up test ' | |
125 'execution on local machine.')) | |
126 # TODO(gkanwar): This option is deprecated. Remove it in the future. | |
127 option_parser.add_option('--exit-code', action='store_true', | |
128 help=('(DEPRECATED) If set, the exit code will be ' | |
129 'total number of failures.')) | |
130 # TODO(gkanwar): This option is deprecated. It is currently used to run tests | |
131 # with the FlakyTest annotation to prevent the bots going red downstream. We | |
132 # should instead use exit codes and let the Buildbot scripts deal with test | |
133 # failures appropriately. See crbug.com/170477. | |
134 option_parser.add_option('--buildbot-step-failure', | |
135 action='store_true', | |
136 help=('(DEPRECATED) If present, will set the ' | |
137 'buildbot status as STEP_FAILURE, otherwise ' | |
138 'as STEP_WARNINGS when test(s) fail.')) | |
139 option_parser.add_option('-d', '--device', dest='test_device', | |
140 help=('Target device for the test suite ' | |
141 'to run on.')) | |
142 | |
143 | |
144 def ProcessCommonOptions(options): | |
145 """Processes and handles all common options.""" | |
146 if options.out_directory: | |
147 cmd_helper.OutDirectory.set(options.out_directory) | |
148 run_tests_helper.SetLogLevel(options.verbose_count) | |
149 | |
150 | |
151 def AddContentBrowserOptions(option_parser): | |
frankf
2013/06/28 23:07:10
Be consistent with naming. ContentBrowserTests/Ins
gkanwar
2013/07/01 22:12:00
Done.
| |
152 """Adds Content Browser test options to option_parser.""" | |
153 | |
154 option_parser.usage = '%prog content_browsertests [options]' | |
155 option_parser.command_list = [] | |
156 option_parser.example = '%prog content_browsertests' | |
157 | |
158 AddCommonOptions(option_parser) | |
159 | |
160 | |
161 def AddGTestOptions(option_parser, default_timeout=60): | |
162 """Adds gtest options to option_parser.""" | |
163 | |
164 option_parser.usage = '%prog gtest [options]' | |
165 option_parser.command_list = [] | |
166 option_parser.example = '%prog gtest -s android_webview_unittests' | |
167 | |
168 option_parser.add_option('-s', '--suite', dest='test_suite', | |
169 help=('Executable name of the test suite to run ' | |
170 '(use -s help to list them).')) | |
171 option_parser.add_option('-a', '--test_arguments', dest='test_arguments', | |
172 help='Additional arguments to pass to the test.') | |
173 # TODO(gkanwar): Most likely deprecate/remove this option once we've pinned | |
174 # down what we're doing with the emulator setup. | |
175 option_parser.add_option('-x', '--xvfb', dest='use_xvfb', | |
176 action='store_true', | |
177 help='Use Xvfb around tests (ignored if not Linux).') | |
178 # TODO(gkanwar): Possible deprecate this flag. Waiting on word from Peter | |
179 # Beverloo. | |
180 option_parser.add_option('--webkit', action='store_true', | |
181 help='Run the tests from a WebKit checkout.') | |
182 option_parser.add_option('--exe', action='store_true', | |
183 help='If set, use the exe test runner instead of ' | |
184 'the APK.') | |
185 option_parser.add_option('-t', dest='timeout', | |
186 help='Timeout to wait for each test', | |
187 type='int', | |
188 default=default_timeout) | |
189 | |
190 # TODO(gkanwar): Move these to Common Options once we have the plumbing | |
191 # in our other test types to handle these commands | |
192 AddEmulatorOptions(option_parser) | |
193 AddCommonOptions(option_parser) | |
194 | |
195 | |
196 def AddJavaTestOptions(option_parser): | |
197 """Adds the Java test options to option_parser.""" | |
198 | |
199 option_parser.add_option( | |
200 '-A', '--annotation', dest='annotation_str', | |
201 help=('Comma-separated list of annotations. Run only tests with any of ' | |
202 'the given annotations. An annotation can be either a key or a ' | |
203 'key-values pair. A test that has no annotation is considered ' | |
204 '"SmallTest".')) | |
205 option_parser.add_option( | |
206 '-E', '--exclude-annotation', dest='exclude_annotation_str', | |
207 help=('Comma-separated list of annotations. Exclude tests with these ' | |
208 'annotations.')) | |
209 option_parser.add_option('-j', '--java_only', action='store_true', | |
210 default=False, help='Run only the Java tests.') | |
211 option_parser.add_option('-p', '--python_only', action='store_true', | |
212 default=False, | |
213 help='Run only the host-driven tests.') | |
214 option_parser.add_option('--screenshot', dest='screenshot_failures', | |
215 action='store_true', | |
216 help='Capture screenshots of test failures') | |
217 option_parser.add_option('--save-perf-json', action='store_true', | |
218 help='Saves the JSON file for each UI Perf test.') | |
219 # TODO(gkanwar): Remove this option. It is not used anywhere. | |
220 option_parser.add_option('--shard_retries', type=int, default=1, | |
221 help=('Number of times to retry each failure when ' | |
222 'sharding.')) | |
223 option_parser.add_option('--official-build', help='Run official build tests.') | |
224 option_parser.add_option('--python_test_root', | |
225 help='Root of the host-driven tests.') | |
226 option_parser.add_option('--keep_test_server_ports', | |
227 action='store_true', | |
228 help=('Indicates the test server ports must be ' | |
229 'kept. When this is run via a sharder ' | |
230 'the test server ports should be kept and ' | |
231 'should not be reset.')) | |
232 # TODO(gkanwar): This option is deprecated. Remove it in the future. | |
233 option_parser.add_option('--disable_assertions', action='store_true', | |
234 help=('(DEPRECATED) Run with java assertions ' | |
235 'disabled.')) | |
236 option_parser.add_option('--test_data', action='append', default=[], | |
237 help=('Each instance defines a directory of test ' | |
238 'data that should be copied to the target(s) ' | |
239 'before running the tests. The argument ' | |
240 'should be of the form <target>:<source>, ' | |
241 '<target> is relative to the device data' | |
242 'directory, and <source> is relative to the ' | |
243 'chromium build directory.')) | |
244 | |
245 | |
246 def ProcessJavaTestOptions(options, errorf): | |
247 """Processes options/arguments and populates options with defaults.""" | |
248 | |
249 if options.java_only and options.python_only: | |
250 errorf('Options java_only (-j) and python_only (-p) ' | |
251 'are mutually exclusive.') | |
252 options.run_java_tests = True | |
253 options.run_python_tests = True | |
254 if options.java_only: | |
255 options.run_python_tests = False | |
256 elif options.python_only: | |
257 options.run_java_tests = False | |
258 | |
259 if not options.python_test_root: | |
260 options.run_python_tests = False | |
261 | |
262 if options.annotation_str: | |
263 options.annotations = options.annotation_str.split(',') | |
264 elif options.test_filter: | |
265 options.annotations = [] | |
266 else: | |
267 options.annotations = ['Smoke', 'SmallTest', 'MediumTest', 'LargeTest'] | |
268 | |
269 if options.exclude_annotation_str: | |
270 options.exclude_annotations = options.exclude_annotation_str.split(',') | |
271 else: | |
272 options.exclude_annotations = [] | |
273 | |
274 if not options.keep_test_server_ports: | |
275 if not ports.ResetTestServerPortAllocation(): | |
276 raise Exception('Failed to reset test server port.') | |
277 | |
278 | |
279 def AddInstrumentationOptions(option_parser): | |
280 """Adds Instrumentation test options to option_parser.""" | |
frankf
2013/07/01 22:36:21
|option_parser|
gkanwar
2013/07/01 23:46:37
Done. Also fixed in several other docstring commen
| |
281 | |
282 option_parser.usage = '%prog instrumentation [options]' | |
283 option_parser.command_list = [] | |
284 option_parser.example = ('%prog instrumentation -I ' | |
285 '--test-apk=ChromiumTestShellTest') | |
286 | |
287 option_parser.add_option('-w', '--wait_debugger', dest='wait_for_debugger', | |
288 action='store_true', | |
289 help='Wait for debugger.') | |
290 option_parser.add_option('-I', dest='install_apk', action='store_true', | |
291 help='Install test APK.') | |
292 option_parser.add_option( | |
293 '--test-apk', dest='test_apk', | |
294 help=('The name of the apk containing the tests ' | |
295 '(without the .apk extension; e.g. "ContentShellTest"). ' | |
296 'Alternatively, this can be a full path to the apk.')) | |
297 | |
298 AddJavaTestOptions(option_parser) | |
frankf
2013/07/01 22:36:21
Move these before the specific options.
gkanwar
2013/07/01 23:46:37
Done. Similar changes where I had more general opt
| |
299 AddCommonOptions(option_parser) | |
300 | |
301 | |
302 def ProcessInstrumentationOptions(options, errorf): | |
303 """Processes options/arguments and populate options with defaults.""" | |
304 | |
305 ProcessJavaTestOptions(options, errorf) | |
306 | |
307 if not options.test_apk: | |
308 errorf('--test-apk must be specified.') | |
309 | |
310 if os.path.exists(options.test_apk): | |
311 # The APK is fully qualified, assume the JAR lives along side. | |
312 options.test_apk_path = options.test_apk | |
313 options.test_apk_jar_path = (os.path.splitext(options.test_apk_path)[0] + | |
314 '.jar') | |
315 else: | |
316 options.test_apk_path = os.path.join(_SDK_OUT_DIR, | |
317 options.build_type, | |
318 constants.SDK_BUILD_APKS_DIR, | |
319 '%s.apk' % options.test_apk) | |
320 options.test_apk_jar_path = os.path.join( | |
321 _SDK_OUT_DIR, options.build_type, constants.SDK_BUILD_TEST_JAVALIB_DIR, | |
322 '%s.jar' % options.test_apk) | |
323 | |
324 | |
325 def AddUIAutomatorOptions(option_parser): | |
326 """Adds UI Automator test options to option_parser.""" | |
327 | |
328 option_parser.usage = '%prog uiautomator [options]' | |
329 option_parser.command_list = [] | |
330 option_parser.example = ('%prog uiautomator ' | |
frankf
2013/06/28 23:07:10
Indent the line itself not the output.
gkanwar
2013/07/01 22:12:00
Got it. Done.
| |
331 '--test-jar=chromium_testshell_uiautomator_tests' | |
332 ' --package-name=org.chromium.chrome.testshell') | |
333 option_parser.add_option( | |
334 '--package-name', | |
335 help='The package name used by the apk containing the application.') | |
336 option_parser.add_option( | |
337 '--test-jar', dest='test_jar', | |
338 help=('The name of the dexed jar containing the tests (without the ' | |
339 '.dex.jar extension). Alternatively, this can be a full path ' | |
340 'to the jar.')) | |
341 | |
342 AddJavaTestOptions(option_parser) | |
343 AddCommonOptions(option_parser) | |
344 | |
345 | |
346 def ProcessUIAutomatorOptions(options, errorf): | |
frankf
2013/06/28 23:07:10
error_func
gkanwar
2013/07/01 22:12:00
Done.
| |
347 """Processes UIAutomator options/arguments.""" | |
348 | |
349 ProcessJavaTestOptions(options, errorf) | |
350 | |
351 if not options.package_name: | |
352 errorf('--package-name must be specified.') | |
353 | |
354 if not options.test_jar: | |
355 errorf('--test-jar must be specified.') | |
356 | |
357 if os.path.exists(options.test_jar): | |
358 # The dexed JAR is fully qualified, assume the info JAR lives along side. | |
359 options.uiautomator_jar = options.test_jar | |
360 else: | |
361 options.uiautomator_jar = os.path.join( | |
362 _SDK_OUT_DIR, options.build_type, constants.SDK_BUILD_JAVALIB_DIR, | |
363 '%s.dex.jar' % options.test_jar) | |
364 options.uiautomator_info_jar = ( | |
365 options.uiautomator_jar[:options.uiautomator_jar.find('.dex.jar')] + | |
366 '_java.jar') | |
367 | |
368 | |
369 def RunTestsCommand(command, options, args, option_parser): | |
370 """Checks test type and dispatches to the appropriate function.""" | |
371 | |
372 ProcessCommonOptions(options) | |
373 | |
374 total_failed = 0 | |
375 if command == 'gtest': | |
376 # TODO(gkanwar): See the emulator TODO above -- this call should either go | |
377 # away or become generalized. | |
378 ProcessEmulatorOptions(options) | |
379 total_failed = gtest_dispatch.Dispatch(options) | |
380 elif command == 'content_browsertests': | |
381 total_failed = browsertests_dispatch.Dispatch(options) | |
382 elif command == 'instrumentation': | |
383 ProcessInstrumentationOptions(options, option_parser.error) | |
384 if options.run_java_tests: | |
385 total_failed += instrumentation_dispatch.Dispatch(options) | |
386 if options.run_python_tests: | |
387 total_failed += python_dispatch.Dispatch(options) | |
388 elif command == 'uiautomator': | |
389 ProcessUIAutomatorOptions(options, option_parser.error) | |
390 if options.run_java_tests: | |
391 total_failed += uiautomator_dispatch.Dispatch(options) | |
392 if options.run_python_tests: | |
393 total_failed += python_dispatch.Dispatch(options) | |
394 else: | |
395 raise Exception('Unknown test type state') | |
396 | |
397 return total_failed | |
398 | |
399 | |
400 def HelpCommand(command, options, args, option_parser): | |
401 """Display help for a certain command, or overall help.""" | |
frankf
2013/06/28 23:07:10
Add args and returns section to docstrings where a
gkanwar
2013/07/01 22:12:00
Done.
| |
402 # If we don't have any args, display overall help | |
403 if len(args) < 3: | |
404 option_parser.print_help() | |
405 return | |
406 | |
407 command = args[2] | |
408 if command not in COMMANDS: | |
frankf
2013/06/28 23:07:10
Blank lines around ifs
gkanwar
2013/07/01 22:12:00
Done.
| |
409 option_parser.error('Unrecognized command.') | |
410 # Treat the help command as a special case. We don't care about showing a | |
411 # specific help page for itself. | |
412 if command == 'help': | |
413 option_parser.print_help() | |
414 return 0 | |
415 COMMANDS[command].add_options_func(option_parser) | |
416 option_parser.usage = '%prog ' + command + ' [options]' | |
417 option_parser.command_list = None | |
418 option_parser.print_help() | |
419 | |
420 return 0 | |
421 | |
422 | |
423 # Define a named tuple for the values in the COMMANDS dictionary so the syntax | |
424 # is a bit prettier. The tuple is two functions: (add options, run command). | |
425 Command = collections.namedtuple('Command', | |
frankf
2013/06/28 23:07:10
Command and COMMANDS are too generic.
gkanwar
2013/07/01 22:12:00
Done.
| |
426 ['add_options_func', 'run_command_func']) | |
427 COMMANDS = { | |
428 'gtest': Command(AddGTestOptions, RunTestsCommand), | |
429 'content_browsertests': Command(AddContentBrowserOptions, RunTestsCommand), | |
430 'instrumentation': Command(AddInstrumentationOptions, RunTestsCommand), | |
431 'uiautomator': Command(AddUIAutomatorOptions, RunTestsCommand), | |
432 'help': Command(lambda option_parser: None, HelpCommand) | |
433 } | |
434 | |
435 | |
436 class CommandOptionParser(optparse.OptionParser): | |
437 """Wrapper class for OptionParser to help with listing commands.""" | |
438 | |
439 def __init__(self, *args, **kwargs): | |
440 self.command_list = kwargs.pop('command_list', []) | |
441 self.example = kwargs.pop('example', '') | |
442 optparse.OptionParser.__init__(self, *args, **kwargs) | |
frankf
2013/06/28 23:07:10
Can you use super?
gkanwar
2013/07/01 22:12:00
Unfortunately not, since optparse.OptionParser is
| |
443 | |
444 def get_usage(self): | |
frankf
2013/06/28 23:07:10
add #override before overriden functions.
gkanwar
2013/07/01 22:12:00
Done.
| |
445 normal_usage = optparse.OptionParser.get_usage(self) | |
446 command_list = self.get_command_list() | |
447 example = self.get_example() | |
448 return self.expand_prog_name(normal_usage + example + command_list) | |
449 | |
450 def get_command_list(self): | |
451 if self.command_list: | |
452 return '\nCommands:\n %s\n\n' % '\n '.join(sorted(self.command_list)) | |
453 return '' | |
454 | |
455 def get_example(self): | |
456 if self.example: | |
457 return '\nExample:\n %s\n\n' % self.example | |
458 return '' | |
459 | |
460 def main(argv): | |
461 option_parser = CommandOptionParser( | |
462 usage='Usage: %prog <command> [options]', | |
463 command_list=COMMANDS.keys()) | |
464 | |
465 if len(argv) < 2: | |
466 option_parser.print_help() | |
467 return 0 | |
468 command = argv[1] | |
469 if command not in COMMANDS: | |
frankf
2013/06/28 23:07:10
Combine the ifs
gkanwar
2013/07/01 22:12:00
Done.
| |
470 option_parser.print_help() | |
471 return 0 | |
472 COMMANDS[command].add_options_func(option_parser) | |
473 options, args = option_parser.parse_args(argv) | |
474 exit_code = COMMANDS[command].run_command_func(command, options, args, | |
frankf
2013/06/28 23:07:10
Indent on next line
gkanwar
2013/07/01 22:12:00
Done.
| |
475 option_parser) | |
476 | |
477 # Failures of individual test suites are communicated by printing a | |
478 # STEP_FAILURE message. | |
479 # Returning a success exit status also prevents the buildbot from incorrectly | |
480 # marking the last suite as failed if there were failures in other suites in | |
481 # the batch (this happens because the exit status is a sum of all failures | |
482 # from all suites, but the buildbot associates the exit status only with the | |
483 # most recent step). | |
484 return exit_code | |
485 | |
486 | |
487 if __name__ == '__main__': | |
488 sys.exit(main(sys.argv)) | |
OLD | NEW |