The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # |
| 3 | # Copyright 2008, The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | """Command line utility for running a pre-defined test. |
| 18 | |
| 19 | Based on previous <androidroot>/development/tools/runtest shell script. |
| 20 | """ |
| 21 | |
| 22 | # Python imports |
| 23 | import glob |
| 24 | import optparse |
| 25 | import os |
| 26 | from sets import Set |
| 27 | import sys |
| 28 | |
| 29 | # local imports |
| 30 | import adb_interface |
| 31 | import android_build |
| 32 | import coverage |
| 33 | import errors |
| 34 | import logger |
| 35 | import run_command |
| 36 | import test_defs |
| 37 | |
| 38 | |
| 39 | class TestRunner(object): |
| 40 | """Command line utility class for running pre-defined Android test(s).""" |
| 41 | |
Brett Chabot | f61f43e | 2009-04-02 11:52:48 -0700 | [diff] [blame^] | 42 | _TEST_FILE_NAME = "test_defs.xml" |
| 43 | |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 44 | # file path to android core platform tests, relative to android build root |
| 45 | # TODO move these test data files to another directory |
Brett Chabot | f61f43e | 2009-04-02 11:52:48 -0700 | [diff] [blame^] | 46 | _CORE_TEST_PATH = os.path.join("development", "testrunner", |
| 47 | _TEST_FILE_NAME) |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 48 | |
| 49 | # vendor glob file path patterns to tests, relative to android |
| 50 | # build root |
| 51 | _VENDOR_TEST_PATH = os.path.join("vendor", "*", "tests", "testinfo", |
Brett Chabot | f61f43e | 2009-04-02 11:52:48 -0700 | [diff] [blame^] | 52 | _TEST_FILE_NAME) |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 53 | |
| 54 | _RUNTEST_USAGE = ( |
| 55 | "usage: runtest.py [options] short-test-name[s]\n\n" |
| 56 | "The runtest script works in two ways. You can query it " |
| 57 | "for a list of tests, or you can launch one or more tests.") |
| 58 | |
Brett Chabot | 72731f3 | 2009-03-31 11:14:05 -0700 | [diff] [blame] | 59 | def __init__(self): |
| 60 | # disable logging of timestamp |
| 61 | logger.SetTimestampLogging(False) |
| 62 | |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 63 | def _ProcessOptions(self): |
| 64 | """Processes command-line options.""" |
| 65 | # TODO error messages on once-only or mutually-exclusive options. |
| 66 | user_test_default = os.path.join(os.environ.get("HOME"), ".android", |
Brett Chabot | f61f43e | 2009-04-02 11:52:48 -0700 | [diff] [blame^] | 67 | self._TEST_FILE_NAME) |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 68 | |
| 69 | parser = optparse.OptionParser(usage=self._RUNTEST_USAGE) |
| 70 | |
| 71 | parser.add_option("-l", "--list-tests", dest="only_list_tests", |
| 72 | default=False, action="store_true", |
| 73 | help="To view the list of tests") |
| 74 | parser.add_option("-b", "--skip-build", dest="skip_build", default=False, |
| 75 | action="store_true", help="Skip build - just launch") |
| 76 | parser.add_option("-n", "--skip_execute", dest="preview", default=False, |
| 77 | action="store_true", |
| 78 | help="Do not execute, just preview commands") |
| 79 | parser.add_option("-r", "--raw-mode", dest="raw_mode", default=False, |
| 80 | action="store_true", |
| 81 | help="Raw mode (for output to other tools)") |
| 82 | parser.add_option("-a", "--suite-assign", dest="suite_assign_mode", |
| 83 | default=False, action="store_true", |
| 84 | help="Suite assignment (for details & usage see " |
| 85 | "InstrumentationTestRunner)") |
| 86 | parser.add_option("-v", "--verbose", dest="verbose", default=False, |
| 87 | action="store_true", |
| 88 | help="Increase verbosity of %s" % sys.argv[0]) |
| 89 | parser.add_option("-w", "--wait-for-debugger", dest="wait_for_debugger", |
| 90 | default=False, action="store_true", |
| 91 | help="Wait for debugger before launching tests") |
| 92 | parser.add_option("-c", "--test-class", dest="test_class", |
| 93 | help="Restrict test to a specific class") |
| 94 | parser.add_option("-m", "--test-method", dest="test_method", |
| 95 | help="Restrict test to a specific method") |
| 96 | parser.add_option("-u", "--user-tests-file", dest="user_tests_file", |
| 97 | metavar="FILE", default=user_test_default, |
| 98 | help="Alternate source of user test definitions") |
| 99 | parser.add_option("-o", "--coverage", dest="coverage", |
| 100 | default=False, action="store_true", |
| 101 | help="Generate code coverage metrics for test(s)") |
| 102 | parser.add_option("-t", "--all-tests", dest="all_tests", |
| 103 | default=False, action="store_true", |
| 104 | help="Run all defined tests") |
| 105 | parser.add_option("--continuous", dest="continuous_tests", |
| 106 | default=False, action="store_true", |
| 107 | help="Run all tests defined as part of the continuous " |
| 108 | "test set") |
| 109 | |
| 110 | group = optparse.OptionGroup( |
| 111 | parser, "Targets", "Use these options to direct tests to a specific " |
| 112 | "Android target") |
| 113 | group.add_option("-e", "--emulator", dest="emulator", default=False, |
| 114 | action="store_true", help="use emulator") |
| 115 | group.add_option("-d", "--device", dest="device", default=False, |
| 116 | action="store_true", help="use device") |
| 117 | group.add_option("-s", "--serial", dest="serial", |
| 118 | help="use specific serial") |
| 119 | parser.add_option_group(group) |
| 120 | |
| 121 | self._options, self._test_args = parser.parse_args() |
| 122 | |
| 123 | if (not self._options.only_list_tests and not self._options.all_tests |
| 124 | and not self._options.continuous_tests and len(self._test_args) < 1): |
| 125 | parser.print_help() |
| 126 | logger.SilentLog("at least one test name must be specified") |
| 127 | raise errors.AbortError |
| 128 | |
| 129 | self._adb = adb_interface.AdbInterface() |
| 130 | if self._options.emulator: |
| 131 | self._adb.SetEmulatorTarget() |
| 132 | elif self._options.device: |
| 133 | self._adb.SetDeviceTarget() |
| 134 | elif self._options.serial is not None: |
| 135 | self._adb.SetTargetSerial(self._options.serial) |
| 136 | |
| 137 | if self._options.verbose: |
| 138 | logger.SetVerbose(True) |
| 139 | |
| 140 | self._root_path = android_build.GetTop() |
| 141 | |
| 142 | self._known_tests = self._ReadTests() |
| 143 | |
| 144 | self._coverage_gen = coverage.CoverageGenerator( |
| 145 | android_root_path=self._root_path, adb_interface=self._adb) |
| 146 | |
| 147 | def _ReadTests(self): |
| 148 | """Parses the set of test definition data. |
| 149 | |
| 150 | Returns: |
| 151 | A TestDefinitions object that contains the set of parsed tests. |
| 152 | Raises: |
| 153 | AbortError: If a fatal error occurred when parsing the tests. |
| 154 | """ |
| 155 | core_test_path = os.path.join(self._root_path, self._CORE_TEST_PATH) |
| 156 | try: |
| 157 | known_tests = test_defs.TestDefinitions() |
| 158 | known_tests.Parse(core_test_path) |
Brett Chabot | 2d85c0e | 2009-03-31 15:19:13 -0700 | [diff] [blame] | 159 | # read all <android root>/vendor/*/tests/testinfo/test_defs.xml paths |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 160 | vendor_tests_pattern = os.path.join(self._root_path, |
| 161 | self._VENDOR_TEST_PATH) |
| 162 | test_file_paths = glob.glob(vendor_tests_pattern) |
| 163 | for test_file_path in test_file_paths: |
| 164 | known_tests.Parse(test_file_path) |
| 165 | if os.path.isfile(self._options.user_tests_file): |
| 166 | known_tests.Parse(self._options.user_tests_file) |
| 167 | return known_tests |
| 168 | except errors.ParseError: |
| 169 | raise errors.AbortError |
| 170 | |
| 171 | def _DumpTests(self): |
| 172 | """Prints out set of defined tests.""" |
| 173 | print "The following tests are currently defined:" |
| 174 | for test in self._known_tests: |
| 175 | print test.GetName() |
| 176 | |
| 177 | def _DoBuild(self): |
| 178 | logger.SilentLog("Building tests...") |
| 179 | target_set = Set() |
| 180 | for test_suite in self._GetTestsToRun(): |
| 181 | self._AddBuildTarget(test_suite.GetBuildPath(), target_set) |
| 182 | |
| 183 | if target_set: |
| 184 | if self._options.coverage: |
| 185 | self._coverage_gen.EnableCoverageBuild() |
| 186 | self._AddBuildTarget(self._coverage_gen.GetEmmaBuildPath(), target_set) |
| 187 | target_build_string = " ".join(list(target_set)) |
Brett Chabot | 72731f3 | 2009-03-31 11:14:05 -0700 | [diff] [blame] | 188 | logger.Log("mmm %s" % target_build_string) |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 189 | cmd = 'ONE_SHOT_MAKEFILE="%s" make -C "%s" files' % (target_build_string, |
| 190 | self._root_path) |
Brett Chabot | 72731f3 | 2009-03-31 11:14:05 -0700 | [diff] [blame] | 191 | if self._options.preview: |
| 192 | # in preview mode, just display to the user what command would have been |
| 193 | # run |
| 194 | logger.Log("adb sync") |
| 195 | else: |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 196 | run_command.RunCommand(cmd, return_output=False) |
| 197 | logger.Log("Syncing to device...") |
| 198 | self._adb.Sync() |
| 199 | |
| 200 | def _AddBuildTarget(self, build_dir, target_set): |
| 201 | if build_dir is not None: |
| 202 | build_file_path = os.path.join(build_dir, "Android.mk") |
| 203 | if os.path.isfile(os.path.join(self._root_path, build_file_path)): |
| 204 | target_set.add(build_file_path) |
| 205 | |
| 206 | def _GetTestsToRun(self): |
| 207 | """Get a list of TestSuite objects to run, based on command line args.""" |
| 208 | if self._options.all_tests: |
| 209 | return self._known_tests.GetTests() |
| 210 | if self._options.continuous_tests: |
| 211 | return self._known_tests.GetContinuousTests() |
| 212 | tests = [] |
| 213 | for name in self._test_args: |
| 214 | test = self._known_tests.GetTest(name) |
| 215 | if test is None: |
| 216 | logger.Log("Error: Could not find test %s" % name) |
| 217 | self._DumpTests() |
| 218 | raise errors.AbortError |
| 219 | tests.append(test) |
| 220 | return tests |
| 221 | |
| 222 | def _RunTest(self, test_suite): |
| 223 | """Run the provided test suite. |
| 224 | |
| 225 | Builds up an adb instrument command using provided input arguments. |
| 226 | |
| 227 | Args: |
| 228 | test_suite: TestSuite to run |
| 229 | """ |
| 230 | |
| 231 | test_class = test_suite.GetClassName() |
| 232 | if self._options.test_class is not None: |
| 233 | test_class = self._options.test_class |
| 234 | if self._options.test_method is not None: |
| 235 | test_class = "%s#%s" % (test_class, self._options.test_method) |
| 236 | |
| 237 | instrumentation_args = {} |
| 238 | if test_class is not None: |
| 239 | instrumentation_args["class"] = test_class |
| 240 | if self._options.wait_for_debugger: |
| 241 | instrumentation_args["debug"] = "true" |
| 242 | if self._options.suite_assign_mode: |
| 243 | instrumentation_args["suiteAssignment"] = "true" |
| 244 | if self._options.coverage: |
| 245 | instrumentation_args["coverage"] = "true" |
| 246 | if self._options.preview: |
| 247 | adb_cmd = self._adb.PreviewInstrumentationCommand( |
| 248 | package_name=test_suite.GetPackageName(), |
| 249 | runner_name=test_suite.GetRunnerName(), |
| 250 | raw_mode=self._options.raw_mode, |
| 251 | instrumentation_args=instrumentation_args) |
| 252 | logger.Log(adb_cmd) |
| 253 | else: |
| 254 | self._adb.StartInstrumentationNoResults( |
| 255 | package_name=test_suite.GetPackageName(), |
| 256 | runner_name=test_suite.GetRunnerName(), |
| 257 | raw_mode=self._options.raw_mode, |
| 258 | instrumentation_args=instrumentation_args) |
| 259 | if self._options.coverage and test_suite.GetTargetName() is not None: |
| 260 | coverage_file = self._coverage_gen.ExtractReport(test_suite) |
| 261 | if coverage_file is not None: |
| 262 | logger.Log("Coverage report generated at %s" % coverage_file) |
| 263 | |
| 264 | def RunTests(self): |
| 265 | """Main entry method - executes the tests according to command line args.""" |
| 266 | try: |
| 267 | run_command.SetAbortOnError() |
| 268 | self._ProcessOptions() |
| 269 | if self._options.only_list_tests: |
| 270 | self._DumpTests() |
| 271 | return |
| 272 | |
Brett Chabot | 72731f3 | 2009-03-31 11:14:05 -0700 | [diff] [blame] | 273 | if not self._adb.IsDevicePresent(): |
| 274 | logger.Log("Error: specified device cannot be found") |
| 275 | return |
| 276 | |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 277 | if not self._options.skip_build: |
| 278 | self._DoBuild() |
| 279 | |
| 280 | for test_suite in self._GetTestsToRun(): |
| 281 | self._RunTest(test_suite) |
| 282 | except KeyboardInterrupt: |
| 283 | logger.Log("Exiting...") |
| 284 | except errors.AbortError: |
| 285 | logger.SilentLog("Exiting due to AbortError...") |
| 286 | except errors.WaitForResponseTimedOutError: |
| 287 | logger.Log("Timed out waiting for response") |
| 288 | |
| 289 | |
| 290 | def RunTests(): |
| 291 | runner = TestRunner() |
| 292 | runner.RunTests() |
| 293 | |
| 294 | if __name__ == "__main__": |
| 295 | RunTests() |