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