def __extend_paths(python_paths, class_paths): from robot import pythonpathsetter __extend_classpath(class_paths) for path in python_paths + class_paths: pythonpathsetter.add_path(path)
def main(): #Push required data in builtins from optparse import OptionParser parser = OptionParser() parser.add_option("-t", "--test", dest="testlist", help="Specify file path of TestList to be executed", metavar="path-to-file") parser.add_option("-b", "--browser", action="store", dest="browser") parser.add_option("-i", "--includetag", dest="testincludetags", default=False, help="Run tests of given tag only") parser.add_option("-e", "--excludetag", dest="testexcludetags", default=False, help="Exclude tests of given tag") parser.add_option("-r", "--rungiventests", dest="rungiventests", default=False, help="Execute only given test case from suite") parser.add_option("-m", "--randomize",action="store_true", dest="runrandomize", default=False, help="Randomizes the test execution order") parser.add_option("-f", "--exitonfailure",action="store_true", dest="exitonfailure", default=False, help="Exit suite if test failed") parser.add_option("-s", "--runsuite", action="store", dest="runsuitesetup", help="Run suite setup") parser.add_option("-g", "--debugfile", action="store_true", dest="debugfile", default=False, help="Create debug log file") parser.add_option("-u", "--baseurl", action="store", dest="baseurl") parser.add_option("-n", "--TestName", action="store", dest="test_name") parser.add_option("-M", "--MetaData", action="store", dest="metadata") # parser.add_option("-a", "--abortonfailure", action="store_true", dest="abortonfailure", default=False, help="Abort suite on first test failure ") s,remainder = parser.parse_args() global tr tr = os.getcwd() from robot import pythonpathsetter pythonpathsetter.add_path(tr) robot.run(s.testlist)
def _extend_sys_path(start_path): import sys import pkgutil from robot import pythonpathsetter path_walk = start_path if start_path in sys.path else _find_module_path(start_path) for loader, _, _ in pkgutil.walk_packages([path_walk]): pythonpathsetter.add_path(loader.path)
def execute(self): if self.debug: print("Environment: %s" % self.full_environment) print("Commandline: %s" % self.commandline) # pylint: disable=unused-variable for key, value in self.new_environment_variables.items(): newpaths = value.split(os.pathsep) for path in newpaths: pythonpathsetter.add_path(path, True) return run_cli(self.commandline[1:], exit=False) # pylint: disable=unsubscriptable-object
def __extend_paths(python_paths, class_paths): import platform from robot import pythonpathsetter if 'Jython' in platform.python_implementation(): for class_path in class_paths: __extend_classpath(class_path) for path in python_paths + class_paths: pythonpathsetter.add_path(path)
def create_libdoc(result_filepath, libname, python_paths, class_paths): import robot from robot import pythonpathsetter from robot.libdoc import libdoc __extend_classpath(class_paths) for path in python_paths + class_paths: pythonpathsetter.add_path(path) libdoc(libname, result_filepath, format='XML') for path in python_paths + class_paths: pythonpathsetter.remove_path(path)
def get_module_path(module_name, python_paths, class_paths): import red_modules import platform from robot import pythonpathsetter __extend_classpath(class_paths) for path in python_paths + class_paths: pythonpathsetter.add_path(path) module_path = red_modules.get_module_path(module_name) for path in python_paths + class_paths: pythonpathsetter.remove_path(path) return module_path
def _call_with_extended_sys_path(to_call, path): import sys from robot import pythonpathsetter old_sys_path = list(sys.path) pythonpathsetter.add_path(path) try: return to_call() except: raise finally: sys.path = old_sys_path
def __extend_paths(to_call, python_paths, class_paths): import sys from robot import pythonpathsetter old_sys_path = list(sys.path) __extend_classpath(class_paths) for path in python_paths + class_paths: pythonpathsetter.add_path(path) try: return to_call() except: raise finally: sys.path = old_sys_path
def get_classes_from_module(module_location): module_directory = os.path.dirname(module_location) module_name = _find_module_name_by_path(module_location) module_names_collector = _get_module_names_collector() class_names = set() if module_location.lower().endswith('__init__.py'): pythonpathsetter.add_path(os.path.dirname(module_directory)) inside_file_names = module_names_collector._try_to_find_names_in_module( module_name, module_location) inside_file_names.extend( module_names_collector._try_to_find_names_in_path( [module_directory], _find_names_in_module)) class_names.update( _combine_module_name_parts(inside_file_names, module_name)) elif module_location.lower().endswith('.py'): pythonpathsetter.add_path(module_directory) inside_file_names = module_names_collector._try_to_find_names_in_module( module_name, module_location) class_names.update( _combine_module_name_parts(inside_file_names, module_name)) elif isJarOrZip(module_location): pythonpathsetter.add_path(module_location) class_names.update( module_names_collector._try_to_find_names_in_archive( [module_location], _find_names_in_archive_module)) else: raise Exception('Unrecognized library path: ' + module_location) return sorted(class_names)
def get_classes_from_module(module_location): module_directory = os.path.dirname(module_location) module_name = _find_module_name_by_path(module_location) class_names = list() if module_location.endswith('__init__.py'): pythonpathsetter.add_path(os.path.dirname(module_directory)) class_names.extend(_try_to_find_names_in_module(module_name)) class_names.extend( _try_to_find_names_in_path([module_directory], _find_names_in_module)) class_names = [ _adjust_class_name(module_name, class_name) for class_name in class_names ] elif module_location.endswith('.py'): pythonpathsetter.add_path(module_directory) class_names.extend(_try_to_find_names_in_module(module_name)) elif module_location.endswith(".zip") or module_location.endswith(".jar"): pythonpathsetter.add_path(module_location) class_names.extend( _try_to_find_names_in_path([module_location], _find_names_in_archive_module)) else: raise Exception('Unrecognized library path: ' + module_location) class_names.extend(_find_missing_names(class_names, module_name)) return sorted(set(class_names))
def get_classes_from_module(module_location, module_name): import os import pkgutil from robot import pythonpathsetter if module_location.endswith('__init__.py'): module_path = os.path.dirname(module_location) module_name_from_location = os.path.basename(module_path) pythonpathsetter.add_path(os.path.dirname(module_path)) loaded_module = _load_module(module_location, module_name, module_name_from_location) class_names = _find_names_in_module(loaded_module, module_name_from_location) for loader, name, _ in pkgutil.walk_packages([module_path]): loaded_module = loader.find_module(name).load_module(name) class_names.extend( _find_names_in_module(loaded_module, module_name_from_location)) return _get_names_combinations( class_names, _get_module_name_by_path(module_location)) elif module_location.endswith('.py'): module_name_from_location = os.path.basename(module_location)[:-3] pythonpathsetter.add_path(os.path.dirname(module_location)) loaded_module = _load_module(module_location, module_name, module_name_from_location) class_names = _find_names_in_module(loaded_module, module_name_from_location) return _get_names_combinations( class_names, _get_module_name_by_path(module_location)) elif module_location.endswith(".zip") or module_location.endswith(".jar"): pythonpathsetter.add_path(module_location) class_names = list() for loader, name, _ in pkgutil.walk_packages([module_location]): loaded_module = loader.load_module(name) map(__import__, [name]) class_names.extend( _find_names_in_archive_module(loaded_module, name)) return class_names else: raise Exception('Unrecognized library path: ' + module_location)
def get_classes_from_module(module_location): import os from robot import pythonpathsetter import importlib import pkgutil module_directory = os.path.dirname(module_location) module_name = _find_module_name_by_path(module_location) class_names = list() if module_location.endswith('__init__.py'): pythonpathsetter.add_path(os.path.dirname(module_directory)) module = importlib.import_module(module_name) class_names.extend(_find_names_in_module(module, module_name)) for loader, name, _ in pkgutil.walk_packages([module_directory]): try: module = loader.find_module(name).load_module(name) class_names.extend(_find_names_in_module(module, name)) except: pass # some modules can't be loaded separately class_names = [n if n.startswith(module_name) else module_name + "." + n for n in class_names] elif module_location.endswith('.py'): pythonpathsetter.add_path(module_directory) module = importlib.import_module(module_name) class_names.extend(_find_names_in_module(module, module_name)) elif module_location.endswith(".zip") or module_location.endswith(".jar"): pythonpathsetter.add_path(module_location) for loader, name, _ in pkgutil.walk_packages([module_location]): try: module = loader.find_module(name).load_module(name) class_names.extend(_find_names_in_archive_module(module, name)) except: pass # some modules can't be loaded separately else: raise Exception('Unrecognized library path: ' + module_location) return sorted(set(class_names))
const=True, nargs="?", help=u'本参数在没有输入 filename 时生效,用于跳过打包测试') parser.add_argument('filename', default='', nargs="?", help='robot test case filename, e.g. robot/package.robot') parser.add_argument('--test_case', '-t', default='', nargs="?", help=u'robot test case e.g. 构建nginx安装包') args = parser.parse_args() project_dir = os.path.dirname(os.path.abspath(__file__)) pythonpathsetter.add_path(project_dir) def exit(code): pythonpathsetter.remove_path(project_dir) sys.exit(code) def convert_code(str): try: return str.decode('utf8') except: return str.decode('gbk') if not args.filename:
def _run_task(self): self.options["vars"].append("org:{}".format(self.org_config.name)) options = self.options["options"].copy() for option in ("test", "include", "exclude", "xunit", "name"): if option in self.options: options[option] = self.options[option] options["variable"] = self.options.get("vars") or [] options["outputdir"] = os.path.relpath( os.path.join(self.working_path, options.get("outputdir", ".")), os.getcwd() ) # get_namespace will potentially download sources that have # yet to be downloaded. For these downloaded sources we'll add # the cached directories to PYTHONPATH before running. source_paths = {} for source in self.options["sources"]: try: source_config = self.project_config.get_namespace(source) source_paths[source] = source_config.repo_root except NamespaceNotFoundError: raise TaskOptionsError(f"robot source '{source}' could not be found") # replace namespace prefixes with path to cached folder for i, path in enumerate(self.options["suites"]): prefix, _, path = path.rpartition(":") if prefix in source_paths: self.options["suites"][i] = os.path.join(source_paths[prefix], path) if self.options["processes"] > 1: # Since pabot runs multiple robot processes, and because # those processes aren't cci tasks, we have to set up the # environment to match what we do with a cci task. Specifically, # we need to add the repo root to PYTHONPATH (via the --pythonpath # option). Otherwise robot won't be able to find libraries and # resource files referenced as relative to the repo root cmd = [ sys.executable, "-m", "pabot.pabot", "--pabotlib", "--processes", str(self.options["processes"]), "--pythonpath", str(self.project_config.repo_root), ] # We need to convert options to their commandline equivalent for option, value in options.items(): if isinstance(value, list): for item in value: cmd.extend([f"--{option}", str(item)]) else: cmd.extend([f"--{option}", str(value)]) # Add each source to pythonpath. Use --pythonpath since # pybot will need to use that option for each process that # it spawns. for path in source_paths.values(): cmd.extend(["--pythonpath", path]) cmd.extend(self.options["suites"]) self.logger.info( f"pabot command: {' '.join([shlex.quote(x) for x in cmd])}" ) result = subprocess.run(cmd) num_failed = result.returncode else: # Add each source to PYTHONPATH. Robot recommends that we # use pythonpathsetter instead of directly setting # sys.path. <shrug> for path in source_paths.values(): pythonpathsetter.add_path(path, end=True) num_failed = robot_run(*self.options["suites"], **options) # These numbers are from the robot framework user guide: # http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#return-codes if 0 < num_failed < 250: raise RobotTestFailure( f"{num_failed} test{'' if num_failed == 1 else 's'} failed." ) elif num_failed == 250: raise RobotTestFailure("250 or more tests failed.") elif num_failed == 251: raise RobotTestFailure("Help or version information printed.") elif num_failed == 252: raise RobotTestFailure("Invalid test data or command line options.") elif num_failed == 253: raise RobotTestFailure("Test execution stopped by user.") elif num_failed >= 255: raise RobotTestFailure("Unexpected internal error")
def start_parallel(self): datasources = os.path.join(os.getcwd(), 'test', self.aut) print('Datasource: {}'.format(datasources)) suite_ = TestSuiteFactory(datasources) print('AUT Suite: Name: {} Test Count: {}'.format( suite_.name, suite_.test_count)) # filter if self.suite: print('Applying suite filter: {}'.format(self.suite)) suite_.filter(included_suites=self.suite) print('New Test Count: {}'.format(suite_.test_count)) if self.test: print('Applying test filter: {}'.format(self.test)) suite_.filter(included_tests=self.test) print('Applying tags filter: included: {} excluded: {}'.format( self.include, self.exclude)) included = self.include.split() excluded = self.exclude.split() print('split included: {}'.format(included)) suite_.filter(included_tags=included, excluded_tags=excluded) if suite_.test_count == 0: print('no tests in suite') return tests = get_tests_in_suite(suite_) executor = ProcessPoolExecutor(max_workers=20) serial = common.get_random_alphanumeric_string(8) # tests = tests[:2] pythonpathsetter.add_path(default_pythonpath) fs = [] for test in tests: fs.append(executor.submit(_api_run_test, datasources, serial, test)) first = True todos = [] m = [] for f in as_completed(fs): r = f.result() pth = './results/{}/tests/{}/output.xml'.format(serial, r) todos.append(pth) m.append(r) rebot(*todos, merge=True, outputdir='./results/{}'.format(serial), output='./results/{}/output.xml'.format(serial)) # copy the screenshots so that they can be included in our new report for r in m: for ff in os.listdir('./results/{}/tests/{}'.format(serial, r)): if ff.lower().endswith('.png'): shutil.copyfile( './results/{}/tests/{}/{}'.format(serial, r, ff), './results/{}/{}'.format(serial, ff))