class RunPlanDistPackage(Command): """Run test plan in distribution package """ name = "runplan" parser = argparse.ArgumentParser("Run test plan in distribution package") parser.add_argument("--recreate-venv", action="store_true", help="force recreate virtualenv") parser.add_argument("--venv", help="designate virtualenv path manually") parser.add_argument("--report-type", help="report type", choices=report_types.keys(), default="stream") parser.add_argument("--report-args", help="additional arguments for specific report", default="") parser.add_argument("--report-args-help", help="show help information for specific report arguemnts", choices=report_types.keys()) parser.add_argument("--runner-type", help="test runner type", choices=runner_types.keys(), default="basic") parser.add_argument("--runner-args", help="additional arguments for specific runner", default="") parser.add_argument("--runner-args-help", help="show help information for specific runner arguemnts", choices=runner_types.keys()) parser.add_argument("--resmgr-backend-type", help="test resource manager backend type", choices=resmgr_backend_types.keys(), default="local") parser.add_argument("--disable-run-on-child", help="do not create child process to run", action="store_true") parser.add_argument("package", help="QTA sdist package") parser.add_argument("plan", help="designate a test plan to run") def execute(self, args): if args.report_args_help: report_types[args.report_args_help].get_parser().print_help() return if args.runner_args_help: runner_types[args.runner_args_help].get_parser().print_help() return venv = VirtuelEnv( args.package, args.venv, args.recreate_venv) venv.activate() report_type = report_types[args.report_type] report = report_type.parse_args(shlex.split(args.report_args)) resmgr_backend = resmgr_backend_types[args.resmgr_backend_type]() runner_type = runner_types[args.runner_type] runner = runner_type.parse_args(shlex.split(args.runner_args), report, resmgr_backend) planname = args.plan planmodulename, planclsname = planname.rsplit(".", 1) __import__(planmodulename) planmod = sys.modules[planmodulename] plancls = getattr(planmod, planclsname) runner.run(plancls()) if not report.is_passed(): sys.exit(1)
class RunPlan(Command): """执行测试计划 """ name = "runplan" parser = argparse.ArgumentParser("Run QTA test plan") parser.add_argument("--report-type", help="report type", choices=report_types.keys(), default="stream") parser.add_argument("--report-args", help="additional arguments for specific report", default="") parser.add_argument("--report-args-help", help="show help information for specific report arguemnts", choices=report_types.keys()) parser.add_argument("--runner-type", help="test runner type", choices=runner_types.keys(), default="basic") parser.add_argument("--runner-args", help="additional arguments for specific runner", default="") parser.add_argument("--runner-args-help", help="show help information for specific runner arguemnts", choices=runner_types.keys()) parser.add_argument("--resmgr-backend-type", help="test resource manager backend type", choices=resmgr_backend_types.keys(), default="local") parser.add_argument("plan", help="designate a test plan to run") def execute(self, args): """执行过程 """ if args.report_args_help: report_types[args.report_args_help].get_parser().print_help() return if args.runner_args_help: runner_types[args.runner_args_help].get_parser().print_help() return report_type = report_types[args.report_type] report = report_type.parse_args(shlex.split(args.report_args)) resmgr_backend = resmgr_backend_types[args.resmgr_backend_type]() runner_type = runner_types[args.runner_type] runner = runner_type.parse_args(shlex.split(args.runner_args), report, resmgr_backend) planname = args.plan planmodulename, planclsname = planname.rsplit(".", 1) __import__(planmodulename) planmod = sys.modules[planmodulename] plancls = getattr(planmod, planclsname) runner.run(plancls()) if not report.is_passed(): sys.exit(1)
class RunTestDistPackage(Command): """Run tests in distribution package """ name = "runtest" parser = argparse.ArgumentParser("Run tests in distribution package") parser.add_argument("--recreate-venv", action="store_true", help="force recreate virtualenv") parser.add_argument("--venv", help="designate virtualenv path manually") parser.add_argument( "--priority", help="run test cases with specific priority, accept multiple options", choices=[ TestCasePriority.BVT, TestCasePriority.High, TestCasePriority.Normal, TestCasePriority.Low ], action="append", dest="priorities") parser.add_argument( "--status", help="run test cases with specific status, accept multiple options", choices=[ TestCaseStatus.Design, TestCaseStatus.Implement, TestCaseStatus.Ready, TestCaseStatus.Review, TestCaseStatus.Suspend ], action="append", dest="status") parser.add_argument( "--owner", help="run test cases with specific owner, accept multiple options", action="append", dest="owners") parser.add_argument( "--excluded-name", help= "exclude test cases with specific name prefix , accept multiple options", action="append", dest="excluded_names") parser.add_argument( "--tag", help="run test cases with specific tag, accept multiple options", action="append", dest="tags") parser.add_argument( "--excluded-tag", help= "exclude test cases with specific name tag , accept multiple options", action="append", dest="excluded_tags") parser.add_argument("--report-type", help="report type", choices=report_types.keys(), default="stream") parser.add_argument("--report-args", help="additional arguments for specific report", default="") parser.add_argument( "--report-args-help", help="show help information for specific report arguemnts", choices=report_types.keys()) parser.add_argument("--runner-type", help="test runner type", choices=runner_types.keys(), default="basic") parser.add_argument("--runner-args", help="additional arguments for specific runner", default="") parser.add_argument( "--runner-args-help", help="show help information for specific runner arguemnts", choices=runner_types.keys()) parser.add_argument("--resmgr-backend-type", help="test resource manager backend type", choices=resmgr_backend_types.keys(), default="local") parser.add_argument("--disable-run-on-child", help="do not create child process to run", action="store_true") parser.add_argument("package", help="QTA sdist package") parser.add_argument("tests", nargs='*', help="designate test names to run") def execute(self, args): if args.report_args_help: report_types[args.report_args_help].get_parser().print_help() return if args.runner_args_help: runner_types[args.runner_args_help].get_parser().print_help() return venv = VirtuelEnv(args.package, args.venv, args.recreate_venv) venv.activate() report_type = report_types[args.report_type] report = report_type.parse_args(shlex.split(args.report_args)) resmgr_backend = resmgr_backend_types[args.resmgr_backend_type]() runner_type = runner_types[args.runner_type] runner_args = args.runner_args + " " # incase user input has no space runner = runner_type.parse_args(shlex.split(runner_args), report, resmgr_backend) test = TestCaseSettings(args.tests, args.excluded_names, args.priorities, args.status, args.owners, args.tags, args.excluded_tags) runner.run(test) if not report.is_passed(): sys.exit(1)
class RunTest(Command): """批量执行测试用例 """ name = 'runtest' parser = argparse.ArgumentParser("Run QTA testcases") parser.add_argument( "tests", metavar="TEST", nargs='*', help="testcase set to executive, eg: zoo.xxx.HelloTest") parser.add_argument('-w', '--working-dir', default=None, help="working directory to store all result files", dest="working_dir") parser.add_argument( '--priority', help="run test cases with specific priority, accept multiple options", dest="priorities", action="append", choices=[ TestCasePriority.BVT, TestCasePriority.High, TestCasePriority.Normal, TestCasePriority.Low ]) parser.add_argument( '--status', default=None, help="run test cases with specific status, accept multiple options", dest="status", action="append", choices=[ TestCaseStatus.Design, TestCaseStatus.Implement, TestCaseStatus.Ready, TestCaseStatus.Review, TestCaseStatus.Suspend ]) parser.add_argument( "--excluded-name", help= "exclude test cases with specific name prefix , accept multiple options", action="append", dest="excluded_names", metavar="EXCLUDED_NAME") parser.add_argument( "--owner", help="run test cases with specific owner, accept multiple options", action="append", dest="owners", metavar="OWNER") parser.add_argument( "--tag", help="run test cases with specific tag, accept multiple options", action="append", dest="tags", metavar="TAG") parser.add_argument( "--excluded-tag", help= "exclude test cases with specific name tag, accept multiple options", action="append", dest="excluded_tags", metavar="EXCLUDED_TAG") parser.add_argument("--report-type", help="report type", choices=report_types.keys(), default="stream") parser.add_argument("--report-args", help="additional arguments for specific report", default="") parser.add_argument( "--report-args-help", help="show help information for specific report arguemnts", choices=report_types.keys()) parser.add_argument("--resmgr-backend-type", help="test resource manager backend type", choices=resmgr_backend_types.keys(), default="local") parser.add_argument("--runner-type", help="test runner type", choices=runner_types.keys(), default="basic") parser.add_argument("--runner-args", help="additional arguments for specific runner", default="") parser.add_argument( "--runner-args-help", help="show help information for specific runner arguemnts", choices=runner_types.keys()) def execute(self, args): """执行过程 """ if args.report_args_help: report_types[args.report_args_help].get_parser().print_help() return if args.runner_args_help: runner_types[args.runner_args_help].get_parser().print_help() return if not args.tests: logger.info("no test set specified") exit(1) if args.working_dir is None: args.working_dir = os.getcwd() prev_dir = os.getcwd() if not path_exists(args.working_dir): os.makedirs(args.working_dir) os.chdir(args.working_dir) priorities = args.priorities or [ TestCase.EnumPriority.Low, TestCase.EnumPriority.Normal, TestCase.EnumPriority.High, TestCase.EnumPriority.BVT ] status = args.status or [ TestCase.EnumStatus.Design, TestCase.EnumStatus.Implement, TestCase.EnumStatus.Review, TestCase.EnumStatus.Ready ] test_conf = TestCaseSettings(names=args.tests, excluded_names=args.excluded_names, priorities=priorities, status=status, owners=args.owners, tags=args.tags, excluded_tags=args.excluded_tags) report_type = report_types[args.report_type] if args.report_type == 'xml': class VerboseXMLTestReport(report_types[args.report_type]): def log_test_result(self, testcase, testresult): logger.info("run test case: %s(pass?:%s)" % (testcase.test_name, testresult.passed)) super(VerboseXMLTestReport, self).log_test_result(testcase, testresult) report_type = VerboseXMLTestReport elif args.report_type == 'online': class VerboseOnlineTestReport(report_types[args.report_type]): def log_test_result(self, testcase, testresult): logger.info("run test case: %s(pass?:%s)" % (testcase.test_name, testresult.passed)) super(VerboseOnlineTestReport, self).log_test_result(testcase, testresult) def begin_report(self): super(VerboseOnlineTestReport, self).begin_report() print("report url: %s" % self.url) with codecs_open(os.path.join(os.getcwd(), "report_url.txt"), "w", encoding="utf-8") as fd: fd.write(self.url) report_type = VerboseOnlineTestReport report = report_type.parse_args(shlex.split(args.report_args)) resmgr_backend = resmgr_backend_types[args.resmgr_backend_type]() runner_type = runner_types[args.runner_type] runner = runner_type.parse_args(shlex.split(args.runner_args), report, resmgr_backend) runner.run(test_conf) os.chdir(prev_dir) if args.report_type == 'online': if sys.platform == "win32": logger.info("opening online report url:%s" % report.url) os.system("start %s" % report.url) else: logger.info("online report generated: %s" % report.url) elif args.report_type == 'xml': if sys.platform == "win32": logger.info("opening XML report with IE...") report_xml = os.path.abspath( os.path.join(args.working_dir, "TestReport.xml")) os.system("start iexplore %s" % report_xml) else: logger.info("XML report generated: %s" % os.path.abspath("TestReport.xml")) if not report.is_passed(): sys.exit(1)
class RunTest(Command): """批量执行测试用例 """ name = 'runtest' parser = argparse.ArgumentParser("Run QTA testcases") parser.add_argument( "tests", metavar="TEST", nargs='*', help="testcase set to executive, eg: zoo.xxx.HelloTest") parser.add_argument('-w', '--working-dir', default=None, help="working directory to store all result files", dest="working_dir") parser.add_argument( '--priority', help="run test cases with specific priority, accept multiple options", dest="priorities", action="append", choices=[ TestCasePriority.BVT, TestCasePriority.High, TestCasePriority.Normal, TestCasePriority.Low ]) parser.add_argument( '--status', default=None, help="run test cases with specific status, accept multiple options", dest="status", action="append", choices=[ TestCaseStatus.Design, TestCaseStatus.Implement, TestCaseStatus.Ready, TestCaseStatus.Review, TestCaseStatus.Suspend ]) parser.add_argument( "--excluded-name", help= "exclude test cases with specific name prefix , accept multiple options", action="append", dest="excluded_names", metavar="EXCLUDED_NAME") parser.add_argument( "--owner", help="run test cases with specific owner, accept multiple options", action="append", dest="owners", metavar="OWNER") parser.add_argument( "--tag", help="run test cases with specific tag, accept multiple options", action="append", dest="tags", metavar="TAG") parser.add_argument( "--excluded-tag", help= "exclude test cases with specific name tag, accept multiple options", action="append", dest="excluded_tags", metavar="EXCLUDED_TAG") parser.add_argument("--report-type", help="report type", choices=report_types.keys(), default="stream") parser.add_argument("--report-args", help="additional arguments for specific report", default="") parser.add_argument( "--report-args-help", help="show help information for specific report arguemnts", choices=report_types.keys()) parser.add_argument("--resmgr-backend-type", help="test resource manager backend type", choices=resmgr_backend_types.keys(), default="local") parser.add_argument("--runner-type", help="test runner type", choices=runner_types.keys(), default="basic") parser.add_argument("--runner-args", help="additional arguments for specific runner", default="") parser.add_argument( "--runner-args-help", help="show help information for specific runner arguemnts", choices=runner_types.keys()) parser.add_argument("--global-parameters", help="global parameters", default="") parser.add_argument("--config-file", help="runtime config file path") def run_args_parser(self, runner_args): """兼容参数传入concurrency=5,retries=1,支持subprocess shell=False""" regex_c = re.compile(r'(\w+=\w+)+') regex = regex_c.search(runner_args) if regex: # concurrency=5,retries=1 ret = [] args = regex_c.findall(runner_args) for arg in args: tmp = arg.split("=") ret.append("--%s %s" % (tmp[0], tmp[1])) return " ".join(ret) else: # --concurrency 5 --retries 1 return runner_args def execute(self, args): """执行过程 """ if args.config_file: with open(args.config_file, 'r') as fp: data = json.load(fp) for k, value in data.items(): setattr(args, k, value) if args.report_args_help: report_types[args.report_args_help].get_parser().print_help() return 1 if args.runner_args_help: runner_types[args.runner_args_help].get_parser().print_help() return 1 if not args.tests: logger.info("no test set specified") return 1 if args.working_dir is None: args.working_dir = os.getcwd() prev_dir = os.getcwd() if not path_exists(args.working_dir): os.makedirs(args.working_dir) os.chdir(args.working_dir) priorities = args.priorities or [ TestCase.EnumPriority.Low, TestCase.EnumPriority.Normal, TestCase.EnumPriority.High, TestCase.EnumPriority.BVT ] status = args.status or [ TestCase.EnumStatus.Design, TestCase.EnumStatus.Implement, TestCase.EnumStatus.Review, TestCase.EnumStatus.Ready ] if args.global_parameters and isinstance(args.global_parameters, six.string_types): args.global_parameters = json.loads(args.global_parameters) test_conf = TestCaseSettings(names=args.tests, excluded_names=args.excluded_names, priorities=priorities, status=status, owners=args.owners, tags=args.tags, excluded_tags=args.excluded_tags, global_parameters=args.global_parameters) report_type = report_types[args.report_type] if args.report_type == 'xml': class VerboseXMLTestReport(report_types[args.report_type]): def log_test_result(self, testcase, testresult): logger.info("run test case: %s(pass?:%s)" % (testcase.test_name, testresult.passed)) super(VerboseXMLTestReport, self).log_test_result(testcase, testresult) report_type = VerboseXMLTestReport elif args.report_type == 'online': class VerboseOnlineTestReport(report_types[args.report_type]): def log_test_result(self, testcase, testresult): logger.info("run test case: %s(pass?:%s)" % (testcase.test_name, testresult.passed)) super(VerboseOnlineTestReport, self).log_test_result(testcase, testresult) def begin_report(self): super(VerboseOnlineTestReport, self).begin_report() print("report url: %s" % self.url) with codecs_open(os.path.join(os.getcwd(), "report_url.txt"), "w", encoding="utf-8") as fd: fd.write(self.url) report_type = VerboseOnlineTestReport report = report_type.parse_args(shlex.split(args.report_args)) resmgr_backend = resmgr_backend_types[args.resmgr_backend_type]() runner_type = runner_types[args.runner_type] # 解析runner_args参数, 用于支持命令行传入concurrency=5,retries=1 runner_args = self.run_args_parser(args.runner_args) runner = runner_type.parse_args(shlex.split(runner_args), report, resmgr_backend) runner.run(test_conf) os.chdir(prev_dir) if args.report_type == 'online': if sys.platform == "win32" and os.environ.get( "AUTO_OPEN_URL", "1") == "1": logger.info("opening online report url:%s" % report.url) os.system("start %s" % report.url) else: logger.info("online report generated: %s" % report.url) elif args.report_type == 'xml': if sys.platform == "win32" and os.environ.get( "AUTO_OPEN_URL", "1") == "1": logger.info("opening XML report with IE...") report_xml = os.path.abspath( os.path.join(args.working_dir, "TestReport.xml")) os.system("start iexplore %s" % report_xml) else: logger.info("XML report generated: %s" % os.path.abspath("TestReport.xml")) if not report.is_passed(): return 1