def Run(self, options, adb):
        """Run the provided test suite.

    Builds up an adb instrument command using provided input arguments.

    Args:
      options: command line options to provide to test run
      adb: adb_interface to device under test

    Raises:
      errors.AbortError: if fatal error occurs
    """

        test_class = self.GetClassName()
        if options.test_class is not None:
            test_class = options.test_class.lstrip()
            if test_class.startswith("."):
                test_class = self.GetPackageName() + test_class
        if options.test_method is not None:
            test_class = "%s#%s" % (test_class, options.test_method)

        test_package = self.GetJavaPackageFilter()
        if options.test_package:
            test_package = options.test_package

        if test_class and test_package:
            logger.Log(
                'Error: both class and java package options are specified')

        instrumentation_args = {}
        if test_class is not None:
            instrumentation_args["class"] = test_class
        if test_package:
            instrumentation_args["package"] = test_package
        if options.test_size:
            instrumentation_args["size"] = options.test_size
        if options.wait_for_debugger:
            instrumentation_args["debug"] = "true"
        if options.suite_assign_mode:
            instrumentation_args["suiteAssignment"] = "true"
        if options.coverage:
            instrumentation_args["coverage"] = "true"
        if options.test_annotation:
            instrumentation_args["annotation"] = options.test_annotation
        if options.test_not_annotation:
            instrumentation_args["notAnnotation"] = options.test_not_annotation
        if options.preview:
            adb_cmd = adb.PreviewInstrumentationCommand(
                package_name=self.GetPackageName(),
                runner_name=self.GetRunnerName(),
                raw_mode=options.raw_mode,
                instrumentation_args=instrumentation_args)
            logger.Log(adb_cmd)
        elif options.coverage:
            coverage_gen = coverage.CoverageGenerator(adb)
            if not coverage_gen.TestDeviceCoverageSupport():
                raise errors.AbortError
            adb.WaitForInstrumentation(self.GetPackageName(),
                                       self.GetRunnerName())
            # need to parse test output to determine path to coverage file
            logger.Log("Running in coverage mode, suppressing test output")
            try:
                (test_results,
                 status_map) = adb.StartInstrumentationForPackage(
                     package_name=self.GetPackageName(),
                     runner_name=self.GetRunnerName(),
                     timeout_time=60 * 60,
                     instrumentation_args=instrumentation_args)
            except errors.InstrumentationError, errors.DeviceUnresponsiveError:
                return
            self._PrintTestResults(test_results)
            device_coverage_path = status_map.get("coverageFilePath", None)
            if device_coverage_path is None:
                logger.Log("Error: could not find coverage data on device")
                return

            coverage_file = coverage_gen.ExtractReport(
                self, device_coverage_path, test_qualifier=options.test_size)
            if coverage_file is not None:
                logger.Log("Coverage report generated at %s" % coverage_file)
Beispiel #2
0
    def _ProcessOptions(self):
        """Processes command-line options."""
        # TODO error messages on once-only or mutually-exclusive options.
        user_test_default = os.path.join(os.environ.get("HOME"), ".android",
                                         self._TEST_FILE_NAME)

        parser = optparse.OptionParser(usage=self._RUNTEST_USAGE)

        parser.add_option("-l",
                          "--list-tests",
                          dest="only_list_tests",
                          default=False,
                          action="store_true",
                          help="To view the list of tests")
        parser.add_option("-b",
                          "--skip-build",
                          dest="skip_build",
                          default=False,
                          action="store_true",
                          help="Skip build - just launch")
        parser.add_option("-n",
                          "--skip_execute",
                          dest="preview",
                          default=False,
                          action="store_true",
                          help="Do not execute, just preview commands")
        parser.add_option("-r",
                          "--raw-mode",
                          dest="raw_mode",
                          default=False,
                          action="store_true",
                          help="Raw mode (for output to other tools)")
        parser.add_option("-a",
                          "--suite-assign",
                          dest="suite_assign_mode",
                          default=False,
                          action="store_true",
                          help="Suite assignment (for details & usage see "
                          "InstrumentationTestRunner)")
        parser.add_option("-v",
                          "--verbose",
                          dest="verbose",
                          default=False,
                          action="store_true",
                          help="Increase verbosity of %s" % sys.argv[0])
        parser.add_option("-w",
                          "--wait-for-debugger",
                          dest="wait_for_debugger",
                          default=False,
                          action="store_true",
                          help="Wait for debugger before launching tests")
        parser.add_option("-c",
                          "--test-class",
                          dest="test_class",
                          help="Restrict test to a specific class")
        parser.add_option("-m",
                          "--test-method",
                          dest="test_method",
                          help="Restrict test to a specific method")
        parser.add_option("-u",
                          "--user-tests-file",
                          dest="user_tests_file",
                          metavar="FILE",
                          default=user_test_default,
                          help="Alternate source of user test definitions")
        parser.add_option("-o",
                          "--coverage",
                          dest="coverage",
                          default=False,
                          action="store_true",
                          help="Generate code coverage metrics for test(s)")
        parser.add_option("-t",
                          "--all-tests",
                          dest="all_tests",
                          default=False,
                          action="store_true",
                          help="Run all defined tests")
        parser.add_option(
            "--continuous",
            dest="continuous_tests",
            default=False,
            action="store_true",
            help="Run all tests defined as part of the continuous "
            "test set")

        group = optparse.OptionGroup(
            parser, "Targets",
            "Use these options to direct tests to a specific "
            "Android target")
        group.add_option("-e",
                         "--emulator",
                         dest="emulator",
                         default=False,
                         action="store_true",
                         help="use emulator")
        group.add_option("-d",
                         "--device",
                         dest="device",
                         default=False,
                         action="store_true",
                         help="use device")
        group.add_option("-s",
                         "--serial",
                         dest="serial",
                         help="use specific serial")
        parser.add_option_group(group)

        self._options, self._test_args = parser.parse_args()

        if (not self._options.only_list_tests and not self._options.all_tests
                and not self._options.continuous_tests
                and len(self._test_args) < 1):
            parser.print_help()
            logger.SilentLog("at least one test name must be specified")
            raise errors.AbortError

        self._adb = adb_interface.AdbInterface()
        if self._options.emulator:
            self._adb.SetEmulatorTarget()
        elif self._options.device:
            self._adb.SetDeviceTarget()
        elif self._options.serial is not None:
            self._adb.SetTargetSerial(self._options.serial)

        if self._options.verbose:
            logger.SetVerbose(True)

        self._root_path = android_build.GetTop()

        self._known_tests = self._ReadTests()

        self._coverage_gen = coverage.CoverageGenerator(
            android_root_path=self._root_path, adb_interface=self._adb)