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 FatalTestException('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.annotation, options.test_filter)

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

    available_tests *= options.number_of_runs
    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)
        apks = [
            apk_info.ApkInfo(options.test_apk_path, options.test_apk_jar_path)
        ]
        test_files_copier = run_java_tests.TestRunner(options, device_id, None,
                                                      False, 0, apks, [])
        test_files_copier.CopyTestFilesOnce()

    # 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 DispatchInstrumentationTests(options):
    """Dispatches the Java and Python instrumentation tests, sharding if possible.

  Uses the logging module to print the combined final results and
  summary of the Java and Python tests. If the java_only option is set, only
  the Java tests run. If the python_only option is set, only the python tests
  run. If neither are set, run both Java and Python tests.

  Args:
    options: command-line options for running the Java and Python tests.

  Returns:
    An integer representing the number of broken tests.
  """
    if not options.keep_test_server_ports:
        # Reset the test port allocation. It's important to do it before starting
        # to dispatch any tests.
        if not ports.ResetTestServerPortAllocation():
            raise Exception('Failed to reset test server port.')

    start_date = int(time.time() * 1000)
    java_results = TestResults()
    python_results = TestResults()

    if options.run_java_tests:
        java_results = run_java_tests.DispatchJavaTests(
            options, [
                apk_info.ApkInfo(options.test_apk_path,
                                 options.test_apk_jar_path)
            ])
    if options.run_python_tests:
        python_results = run_python_tests.DispatchPythonTests(options)

    all_results = TestResults.FromTestResults([java_results, python_results])

    all_results.LogFull(test_type='Instrumentation',
                        test_package=options.test_apk,
                        annotation=options.annotation,
                        build_type=options.build_type,
                        flakiness_server=options.flakiness_dashboard_server)

    return len(all_results.GetAllBroken())
    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:
      TestResults object with a single test result.
    """
        test = self._ComposeFullTestName(fname, suite, test)
        apks = [
            apk_info.ApkInfo(self.options.test_apk_path,
                             self.options.test_apk_jar_path)
        ]
        java_test_runner = TestRunner(self.options, self.device_id, [test],
                                      False, self.shard_index, apks,
                                      self.ports_to_forward)
        return java_test_runner.Run()