示例#1
0
    def _RunJavaTest(self, fname, suite, test):
        """Runs a single Java test with a Java TestRunner.

    Args:
      fname: filename for the test (e.g. foo/bar/baz/tests/FooTest.py)
      suite: name of the Java test suite (e.g. FooTest)
      test: name of the test method to run (e.g. testFooBar)

    Returns:
      TestRunResults object with a single test result.
    """
        test = self._ComposeFullTestName(fname, suite, test)
        test_pkg = test_package.TestPackage(self.options.test_apk_path,
                                            self.options.test_apk_jar_path)
        instrumentation_options = test_options.InstrumentationOptions(
            self.options.build_type, self.options.tool,
            self.options.cleanup_test_files, self.options.push_deps,
            self.options.annotations, self.options.exclude_annotations,
            self.options.test_filter, self.options.test_data,
            self.options.save_perf_json, self.options.screenshot_failures,
            self.options.disable_assertions, self.options.wait_for_debugger,
            self.options.test_apk, self.options.test_apk_path,
            self.options.test_apk_jar_path)
        java_test_runner = test_runner.TestRunner(instrumentation_options,
                                                  self.device_id,
                                                  self.shard_index, test_pkg,
                                                  self.ports_to_forward)
        try:
            java_test_runner.SetUp()
            return java_test_runner.RunTest(test)[0]
        finally:
            java_test_runner.TearDown()
示例#2
0
def Setup(test_options):
    """Create and return the test runner factory and tests.

  Args:
    test_options: An InstrumentationOptions object.

  Returns:
    A tuple of (TestRunnerFactory, tests).
  """
    if (test_options.coverage_dir
            and not os.path.exists(test_options.coverage_dir)):
        os.makedirs(test_options.coverage_dir)

    test_pkg = test_package.TestPackage(test_options.test_apk_path,
                                        test_options.test_apk_jar_path)
    tests = test_pkg.GetAllMatchingTests(test_options.annotations,
                                         test_options.exclude_annotations,
                                         test_options.test_filter)
    if not tests:
        logging.error('No instrumentation tests to run with current args.')

    def TestRunnerFactory(device, shard_index):
        return test_runner.TestRunner(test_options, device, shard_index,
                                      test_pkg)

    return (TestRunnerFactory, tests)
示例#3
0
  def __RunJavaTest(self, package_name, test_case, test_method):
    """Runs a single Java test method with a Java TestRunner.

    Args:
      package_name: Package name in which the java tests live
          (e.g. foo.bar.baz.tests)
      test_case: Name of the Java test case (e.g. FooTest)
      test_method: Name of the test method to run (e.g. testFooBar)

    Returns:
      TestRunResults object with a single test result.
    """
    test = '%s.%s#%s' % (package_name, test_case, test_method)
    test_pkg = test_package.TestPackage(
        self.instrumentation_options.test_apk_path,
        self.instrumentation_options.test_apk_jar_path)
    java_test_runner = test_runner.TestRunner(self.instrumentation_options,
                                              self.device_id,
                                              self.shard_index, test_pkg,
                                              self.ports_to_forward)
    try:
      java_test_runner.SetUp()
      return java_test_runner.RunTest(test)[0]
    finally:
      java_test_runner.TearDown()
示例#4
0
    def _RunJavaTestFilters(self, test_filters, additional_flags=None):
        """Calls a list of tests and stops at the first test failure.

    This method iterates until either it encounters a non-passing test or it
    exhausts the list of tests. Then it returns the appropriate overall result.

    Test cases may make use of this method internally to assist in running
    instrumentation tests. This function relies on instrumentation_options
    being defined.

    Args:
      test_filters: A list of Java test filters.
      additional_flags: A list of addition flags to add to the command line.

    Returns:
      A TestRunResults object containing an overall result for this set of Java
      tests. If any Java tests do not pass, this is a fail overall.
    """
        test_type = base_test_result.ResultType.PASS
        log = ''

        test_pkg = test_package.TestPackage(
            self.instrumentation_options.test_apk_path,
            self.instrumentation_options.test_apk_jar_path,
            self.instrumentation_options.test_support_apk_path)

        start_ms = int(time.time()) * 1000
        done = False
        for test_filter in test_filters:
            tests = test_pkg.GetAllMatchingTests(None, None, test_filter,
                                                 [self.device])
            # Filters should always result in >= 1 test.
            if len(tests) == 0:
                raise Exception('Java test filter "%s" returned no tests.' %
                                test_filter)
            for test in tests:
                # We're only running one test at a time, so this TestRunResults object
                # will hold only one result.
                java_result = self.__RunJavaTest(test, test_pkg,
                                                 additional_flags)
                assert len(java_result.GetAll()) == 1
                if not java_result.DidRunPass():
                    result = java_result.GetNotPass().pop()
                    log = result.GetLog()
                    log += self.__GetHostForwarderLog()
                    test_type = result.GetType()
                    done = True
                    break
            if done:
                break
        duration_ms = int(time.time()) * 1000 - start_ms

        overall_result = base_test_result.TestRunResults()
        overall_result.AddResult(
            test_result.InstrumentationTestResult(self.tagged_name,
                                                  test_type,
                                                  start_ms,
                                                  duration_ms,
                                                  log=log))
        return overall_result
def Setup(test_options, devices):
    """Create and return the test runner factory and tests.

  Args:
    test_options: An InstrumentationOptions object.

  Returns:
    A tuple of (TestRunnerFactory, tests).
  """
    if (test_options.coverage_dir
            and not os.path.exists(test_options.coverage_dir)):
        os.makedirs(test_options.coverage_dir)

    test_pkg = test_package.TestPackage(
        test_options.test_apk_path,
        test_options.test_apk_jar_path,
        test_options.test_support_apk_path,
        additional_apks=test_options.additional_apks,
        apk_under_test=test_options.apk_under_test,
        test_apk_incremental_install_script=test_options.
        test_apk_incremental_install_script,
        apk_under_test_incremental_install_script=test_options.
        apk_under_test_incremental_install_script)
    tests = test_pkg.GetAllMatchingTests(test_options.annotations,
                                         test_options.exclude_annotations,
                                         test_options.test_filter, devices)
    if not tests:
        logging.error('No instrumentation tests to run with current args.')

    if test_options.test_data:
        device_utils.DeviceUtils.parallel(devices).pMap(
            _PushDataDeps, test_options)

    if test_options.isolate_file_path:
        isolator = base_setup.GenerateDepsDirUsingIsolate(
            test_options.test_apk, test_options.isolate_file_path,
            ISOLATE_FILE_PATHS, DEPS_EXCLUSION_LIST)

        def push_data_deps_to_device_dir(device):
            base_setup.PushDataDeps(device, isolator.isolate_deps_dir,
                                    device.GetExternalStoragePath(),
                                    test_options)

        device_utils.DeviceUtils.parallel(devices).pMap(
            push_data_deps_to_device_dir)
        if isolator:
            isolator.Clear()

    device_utils.DeviceUtils.parallel(devices).pMap(_PushExtraSuiteDataDeps,
                                                    test_options.test_apk)

    def TestRunnerFactory(device, shard_index):
        return test_runner.TestRunner(test_options, device, shard_index,
                                      test_pkg)

    return (TestRunnerFactory, tests)
示例#6
0
def DispatchPythonTests(options):
    """Dispatches the Python tests. If there are multiple devices, use sharding.

  Args:
    options: command line options.

  Returns:
    A list of test results.
  """

    attached_devices = android_commands.GetAttachedDevices()
    if not attached_devices:
        raise Exception('You have no devices attached or visible!')
    if options.device:
        attached_devices = [options.device]

    test_collection = TestInfoCollection()
    all_tests = _GetAllTests(options.python_test_root, options.official_build)
    test_collection.AddTests(all_tests)
    test_names = [t.qualified_name for t in all_tests]
    logging.debug('All available tests: ' + str(test_names))

    available_tests = test_collection.GetAvailableTests(
        options.annotations, options.exclude_annotations, options.test_filter)

    if not available_tests:
        logging.warning('No Python tests to run with current args.')
        return base_test_result.TestRunResults()

    test_names = [t.qualified_name for t in available_tests]
    logging.debug('Final list of tests to run: ' + str(test_names))

    # Copy files to each device before running any tests.
    for device_id in attached_devices:
        logging.debug('Pushing files to device %s', device_id)
        test_pkg = test_package.TestPackage(options.test_apk_path,
                                            options.test_apk_jar_path)
        test_files_copier = test_runner.TestRunner(options, device_id, 0,
                                                   test_pkg, [])
        test_files_copier.InstallTestPackage()
        if options.push_deps:
            logging.info('Pushing data deps to device.')
            test_files_copier.PushDataDeps()
        else:
            logging.warning('Skipping pushing data deps to device.')

    # Actually run the tests.
    if len(attached_devices) > 1 and options.wait_for_debugger:
        logging.warning('Debugger can not be sharded, '
                        'using first available device')
        attached_devices = attached_devices[:1]
    logging.debug('Running Python tests')
    sharder = PythonTestSharder(attached_devices, available_tests, options)
    test_results = sharder.RunShardedTests()

    return test_results
    def _RunJavaTest(self, fname, suite, test):
        """Runs a single Java test with a Java TestRunner.

    Args:
      fname: filename for the test (e.g. foo/bar/baz/tests/FooTest.py)
      suite: name of the Java test suite (e.g. FooTest)
      test: name of the test method to run (e.g. testFooBar)

    Returns:
      TestRunResults object with a single test result.
    """
        test = self._ComposeFullTestName(fname, suite, test)
        test_pkg = test_package.TestPackage(self.options.test_apk_path,
                                            self.options.test_apk_jar_path)
        java_test_runner = test_runner.TestRunner(self.options, self.device_id,
                                                  self.shard_index, test_pkg,
                                                  self.ports_to_forward)
        try:
            java_test_runner.SetUp()
            return java_test_runner.RunTest(test)[0]
        finally:
            java_test_runner.TearDown()