Best Python code snippet using localstack_python
test_runner.py
Source:test_runner.py  
1#!/usr/bin/env python2#3# Copyright 2018 The Chromium Authors. All rights reserved.4# Use of this source code is governed by a BSD-style license that can be5# found in the LICENSE file.6"""Deploys and runs a test package on a Fuchsia target."""7import argparse8import os9import runner_logs10import sys11from common_args import AddCommonArgs, ConfigureLogging, GetDeploymentTargetForArgs12from net_test_server import SetupTestServer13from run_package import RunPackage, RunPackageArgs, SystemLogReader14from runner_exceptions import HandleExceptionAndReturnExitCode15from runner_logs import RunnerLogManager16from symbolizer import BuildIdsPaths17DEFAULT_TEST_SERVER_CONCURRENCY = 418TEST_RESULT_PATH = '/data/test_summary.json'19TEST_FILTER_PATH = '/data/test_filter.txt'20def main():21  parser = argparse.ArgumentParser()22  AddCommonArgs(parser)23  parser.add_argument('--gtest_filter',24                      help='GTest filter to use in place of any default.')25  parser.add_argument('--gtest_repeat',26                      help='GTest repeat value to use. This also disables the '27                           'test launcher timeout.')28  parser.add_argument('--test-launcher-retry-limit',29                      help='Number of times that test suite will retry failing '30                           'tests. This is multiplicative with --gtest_repeat.')31  parser.add_argument(32      '--test-launcher-shard-index',33      type=int,34      default=os.environ.get('GTEST_SHARD_INDEX'),35      help='Index of this instance amongst swarming shards.')36  parser.add_argument(37      '--test-launcher-total-shards',38      type=int,39      default=os.environ.get('GTEST_TOTAL_SHARDS'),40      help='Total number of swarming shards of this suite.')41  parser.add_argument('--gtest_break_on_failure', action='store_true',42                      default=False,43                      help='Should GTest break on failure; useful with '44                           '--gtest_repeat.')45  parser.add_argument('--single-process-tests', action='store_true',46                      default=False,47                      help='Runs the tests and the launcher in the same '48                           'process. Useful for debugging.')49  parser.add_argument('--test-launcher-batch-limit',50                      type=int,51                      help='Sets the limit of test batch to run in a single '52                      'process.')53  # --test-launcher-filter-file is specified relative to --output-dir,54  # so specifying type=os.path.* will break it.55  parser.add_argument('--test-launcher-filter-file',56                      default=None,57                      help='Override default filter file passed to target test '58                      'process. Set an empty path to disable filtering.')59  parser.add_argument('--test-launcher-jobs',60                      type=int,61                      help='Sets the number of parallel test jobs.')62  parser.add_argument('--test-launcher-summary-output',63                      help='Where the test launcher will output its json.')64  parser.add_argument('--enable-test-server', action='store_true',65                      default=False,66                      help='Enable Chrome test server spawner.')67  parser.add_argument('--test-launcher-bot-mode', action='store_true',68                      default=False,69                      help='Informs the TestLauncher to that it should enable '70                      'special allowances for running on a test bot.')71  parser.add_argument('--child-arg', action='append',72                      help='Arguments for the test process.')73  parser.add_argument('child_args', nargs='*',74                      help='Arguments for the test process.')75  args = parser.parse_args()76  # Flag output_dir is required for tests launched with this script.77  if not args.output_dir:78    raise ValueError("output-dir must be specified.")79  ConfigureLogging(args)80  child_args = []81  if args.test_launcher_shard_index != None:82    child_args.append(83        '--test-launcher-shard-index=%d' % args.test_launcher_shard_index)84  if args.test_launcher_total_shards != None:85    child_args.append(86        '--test-launcher-total-shards=%d' % args.test_launcher_total_shards)87  if args.single_process_tests:88    child_args.append('--single-process-tests')89  if args.test_launcher_bot_mode:90    child_args.append('--test-launcher-bot-mode')91  if args.test_launcher_batch_limit:92    child_args.append('--test-launcher-batch-limit=%d' %93                       args.test_launcher_batch_limit)94  # Only set --test-launcher-jobs if the caller specifies it, in general.95  # If the caller enables the test-server then we need to launch the right96  # number of instances to match the maximum number of parallel test jobs, so97  # in that case we set --test-launcher-jobs based on the number of CPU cores98  # specified for the emulator to use.99  test_concurrency = None100  if args.test_launcher_jobs:101    test_concurrency = args.test_launcher_jobs102  elif args.enable_test_server:103    if args.device == 'device':104      test_concurrency = DEFAULT_TEST_SERVER_CONCURRENCY105    else:106      test_concurrency = args.cpu_cores107  if test_concurrency:108    child_args.append('--test-launcher-jobs=%d' % test_concurrency)109  if args.gtest_filter:110    child_args.append('--gtest_filter=' + args.gtest_filter)111  if args.gtest_repeat:112    child_args.append('--gtest_repeat=' + args.gtest_repeat)113    child_args.append('--test-launcher-timeout=-1')114  if args.test_launcher_retry_limit:115    child_args.append(116        '--test-launcher-retry-limit=' + args.test_launcher_retry_limit)117  if args.gtest_break_on_failure:118    child_args.append('--gtest_break_on_failure')119  if args.test_launcher_summary_output:120    child_args.append('--test-launcher-summary-output=' + TEST_RESULT_PATH)121  if args.child_arg:122    child_args.extend(args.child_arg)123  if args.child_args:124    child_args.extend(args.child_args)125  try:126    with GetDeploymentTargetForArgs() as target, \127         SystemLogReader() as system_logger, \128         RunnerLogManager(args.runner_logs_dir, BuildIdsPaths(args.package)):129      target.Start()130      if args.system_log_file and args.system_log_file != '-':131        system_logger.Start(target, args.package, args.system_log_file)132      if args.test_launcher_filter_file:133        target.PutFile(args.test_launcher_filter_file, TEST_FILTER_PATH,134                      for_package=args.package_name)135        child_args.append('--test-launcher-filter-file=' + TEST_FILTER_PATH)136      test_server = None137      if args.enable_test_server:138        assert test_concurrency139        test_server = SetupTestServer(target, test_concurrency,140                                      args.package_name)141      run_package_args = RunPackageArgs.FromCommonArgs(args)142      returncode = RunPackage(args.output_dir, target, args.package,143                              args.package_name, child_args, run_package_args)144      if test_server:145        test_server.Stop()146      if args.test_launcher_summary_output:147        target.GetFile(TEST_RESULT_PATH, args.test_launcher_summary_output,148                      for_package=args.package_name)149      return returncode150  except:151    return HandleExceptionAndReturnExitCode()152if __name__ == '__main__':...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
