Ejemplo n.º 1
0
Archivo: cli.py Proyecto: gzs4850/IFTC
def single_run(testset):
    """ API test: parse command line options and run commands.
    """
    parser = argparse.ArgumentParser(
        description='HTTP test runner, not just about api test and load test.')
    parser.add_argument('-V',
                        '--version',
                        dest='version',
                        action='store_true',
                        help="show version")
    parser.add_argument('testset_paths', nargs='*', help="testset file path")
    parser.add_argument('--log-level',
                        default='INFO',
                        help="Specify logging level, default is INFO.")
    parser.add_argument(
        '--failfast',
        action='store_true',
        default=False,
        help="Stop the test run on the first error or failure.")
    parser.add_argument('--startproject', help="Specify new project name.")

    args = parser.parse_args()
    logger.setup_logger(args.log_level)

    args.testset_paths = 'E:\case'

    kwargs = {
        "output": os.path.join(os.getcwd(), "reports"),
        "failfast": args.failfast
    }
    test_runner = HTMLTestRunner(**kwargs)
    result = run_suite_path(testset, {}, test_runner)
    print_output(result.output)

    return 0 if result.success else 1
Ejemplo n.º 2
0
    def run(self, path_or_testsets, mapping=None):
        """ start to run test with varaibles mapping
        @param path_or_testsets: YAML/JSON testset file path or testset list
            path: path could be in several type
                - absolute/relative file path
                - absolute/relative folder path
                - list/set container with file(s) and/or folder(s)
            testsets: testset or list of testset
                - (dict) testset_dict
                - (list) list of testset_dict
                    [
                        testset_dict_1,
                        testset_dict_2
                    ]
        @param (dict) mapping:
            if mapping specified, it will override variables in config block
        """
        try:
            test_suite_list = init_test_suites(path_or_testsets, mapping)
        except exceptions.TestcaseNotFound:
            logger.log_error("Testcases not found in {}".format(path_or_testsets))
            sys.exit(1)

        self.summary = {
            "success": True,
            "stat": {},
            "time": {},
            "platform": get_platform(),
            "details": []
        }

        def accumulate_stat(origin_stat, new_stat):
            """ accumulate new_stat to origin_stat
            """
            for key in new_stat:
                if key not in origin_stat:
                    origin_stat[key] = new_stat[key]
                elif key == "start_at":
                    # start datetime
                    origin_stat[key] = min(origin_stat[key], new_stat[key])
                else:
                    origin_stat[key] += new_stat[key]

        for test_suite in test_suite_list:
            result = self.runner.run(test_suite)
            test_suite_summary = get_summary(result)

            self.summary["success"] &= test_suite_summary["success"]
            test_suite_summary["name"] = test_suite.config.get("name")
            test_suite_summary["base_url"] = test_suite.config.get("request", {}).get("base_url", "")
            test_suite_summary["output"] = test_suite.output
            print_output(test_suite_summary["output"])

            accumulate_stat(self.summary["stat"], test_suite_summary["stat"])
            accumulate_stat(self.summary["time"], test_suite_summary["time"])

            self.summary["details"].append(test_suite_summary)

        return self
Ejemplo n.º 3
0
def main_hrun():
    """ API test: parse command line options and run commands.
    """
    parser = argparse.ArgumentParser(
        description='HTTP test runner, not just about api test and load test.')
    parser.add_argument('-V',
                        '--version',
                        dest='version',
                        action='store_true',
                        help="show version")
    parser.add_argument('testset_paths', nargs='*', help="testset file path")
    parser.add_argument(
        '--html-report-name',
        help=
        "specify html report name, only effective when generating html report."
    )
    parser.add_argument('--html-report-template',
                        help="specify html report template path.")
    parser.add_argument('--log-level',
                        default='INFO',
                        help="Specify logging level, default is INFO.")
    parser.add_argument(
        '--dot-env-path',
        help=
        "Specify .env file path, which is useful for keeping production credentials."
    )
    parser.add_argument(
        '--failfast',
        action='store_true',
        default=False,
        help="Stop the test run on the first error or failure.")
    parser.add_argument('--startproject', help="Specify new project name.")

    args = parser.parse_args()
    logger.setup_logger(args.log_level)

    if args.version:
        logger.color_print("{}".format(__version__), "GREEN")
        exit(0)

    dot_env_path = args.dot_env_path or os.path.join(os.getcwd(), ".env")
    if dot_env_path:
        load_dot_env_file(dot_env_path)

    project_name = args.startproject
    if project_name:
        project_path = os.path.join(os.getcwd(), project_name)
        create_scaffold(project_path)
        exit(0)

    result = HttpRunner(
        args.testset_paths,
        failfast=args.failfast).run(html_report_name=args.html_report_name)

    print_output(result["output"])
    return 0 if result["success"] else 1
Ejemplo n.º 4
0
    def generate_output(self, output_variables_list):
        """ generate and print output
        """
        variables_mapping = self.context.get_testcase_variables_mapping()
        output = {
            variable: variables_mapping[variable]
            for variable in output_variables_list
        }
        utils.print_output(output)

        return output
Ejemplo n.º 5
0
    def generate_output(self, output_variables_list):
        """ generate and print output
        """
        variables_mapping = self.context.get_testcase_variables_mapping()
        output = {
            variable: variables_mapping[variable]
            for variable in output_variables_list
        }
        utils.print_output(output)

        return output
Ejemplo n.º 6
0
Archivo: cli.py Proyecto: gzs4850/IFTC
def main_hrun():
    """ API test: parse command line options and run commands.
    """
    parser = argparse.ArgumentParser(
        description='HTTP test runner, not just about api test and load test.')
    parser.add_argument('-V',
                        '--version',
                        dest='version',
                        action='store_true',
                        help="show version")
    parser.add_argument('testset_paths', nargs='*', help="testset file path")
    parser.add_argument('--log-level',
                        default='INFO',
                        help="Specify logging level, default is INFO.")
    parser.add_argument(
        '--failfast',
        action='store_true',
        default=False,
        help="Stop the test run on the first error or failure.")
    parser.add_argument('--startproject', help="Specify new project name.")

    args = parser.parse_args()
    logger.setup_logger(args.log_level)

    args.testset_paths = 'E:\case'

    if args.version:
        logger.color_print("HttpRunner version: {}".format(hrun_version),
                           "GREEN")
        logger.color_print("PyUnitReport version: {}".format(pyu_version),
                           "GREEN")
        exit(0)

    project_name = args.startproject
    if project_name:
        project_path = os.path.join(os.getcwd(), project_name)
        create_scaffold(project_path)
        exit(0)

    kwargs = {
        "output": os.path.join(os.getcwd(), "reports"),
        "failfast": args.failfast
    }
    test_runner = HTMLTestRunner(**kwargs)
    result = run_suite_path(args.testset_paths, {}, test_runner)
    print_output(result.output)

    return 0 if result.success else 1
Ejemplo n.º 7
0
    def generate_output(self, output_variables_list):
        """ generate and print output
        """
        variables_mapping = self.context.get_testcase_variables_mapping()

        output = {}
        for variable in output_variables_list:
            if variable not in variables_mapping:
                logging.warning("variable '{}' can not be found in variables mapping, failed to ouput!")
                continue

            output[variable] = variables_mapping[variable]

        utils.print_output(output)

        return output
Ejemplo n.º 8
0
def main_hrun():
    """ API test: parse command line options and run commands.
    """
    parser = argparse.ArgumentParser(description=__description__)
    parser.add_argument('-V',
                        '--version',
                        dest='version',
                        action='store_true',
                        help="show version")
    parser.add_argument('testset_paths', nargs='*', help="testset file path")
    parser.add_argument('--no-html-report',
                        action='store_true',
                        default=False,
                        help="do not generate html report.")
    parser.add_argument(
        '--html-report-name',
        help=
        "specify html report name, only effective when generating html report."
    )
    parser.add_argument('--html-report-template',
                        help="specify html report template path.")
    parser.add_argument('--log-level',
                        default='INFO',
                        help="Specify logging level, default is INFO.")
    parser.add_argument('--log-file',
                        help="Write logs to specified file path.")
    parser.add_argument(
        '--dot-env-path',
        help=
        "Specify .env file path, which is useful for keeping production credentials."
    )
    parser.add_argument(
        '--failfast',
        action='store_true',
        default=False,
        help="Stop the test run on the first error or failure.")
    parser.add_argument('--startproject', help="Specify new project name.")
    parser.add_argument('--validate',
                        nargs='*',
                        help="Validate JSON testset format.")
    parser.add_argument('--prettify',
                        nargs='*',
                        help="Prettify JSON testset format.")

    args = parser.parse_args()
    logger.setup_logger(args.log_level, args.log_file)

    if is_py2:
        logger.log_warning(get_python2_retire_msg())

    if args.version:
        logger.color_print("{}".format(__version__), "GREEN")
        exit(0)

    if args.validate:
        validate_json_file(args.validate)
        exit(0)
    if args.prettify:
        prettify_json_file(args.prettify)
        exit(0)

    project_name = args.startproject
    if project_name:
        project_path = os.path.join(os.getcwd(), project_name)
        create_scaffold(project_path)
        exit(0)

    runner = HttpRunner(failfast=args.failfast,
                        dot_env_path=args.dot_env_path).run(args.testset_paths)

    if not args.no_html_report:
        runner.gen_html_report(html_report_name=args.html_report_name,
                               html_report_template=args.html_report_template)

    summary = runner.summary
    print_output(summary["output"])
    return 0 if summary["success"] else 1
Ejemplo n.º 9
0
    def run3(self, path_or_testsets, mapping=None):
        try:
            test_suite_list = init_test_suites2(path_or_testsets)
        except exceptions.TestcaseNotFound:
            logger.log_error(
                "Testcases not found in {}".format(path_or_testsets))
            sys.exit(1)

        self.summary = {
            "success": True,
            "stat": {},
            "time": {},
            "platform": get_platform(),
            "details": []
        }

        mapping = mapping or {}

        def accumulate_stat(origin_stat, new_stat):
            """ accumulate new_stat to origin_stat
            """
            for key in new_stat:
                if key not in origin_stat:
                    origin_stat[key] = new_stat[key]
                elif key == "start_at":
                    # start datetime
                    origin_stat[key] = min(origin_stat[key], new_stat[key])
                else:
                    origin_stat[key] += new_stat[key]

        # 各用例提取的变量
        extract_parameter = {}

        for test_suite in test_suite_list:
            extract_list = test_suite.get('testcases', {})[0].get('extract')

            # # 合并各用例提取的变量
            # variables = test_suite.testcase_parser.variables
            # test_suite.testcase_parser.update_binded_variables(dict(variables, **extract_parameter))

            try:
                test_suite = TestSuite_ext(test_suite,
                                           dict(extract_parameter, **mapping))
            except exceptions.ParamsError as e:
                raise Exception("出现异常,参数错误: {0}".format(e))
            except exceptions.VariableNotFound as e:
                raise Exception("出现异常,变量不存在: {0}".format(e))
            except BaseException as e:
                raise Exception("出现异常: {0}".format(e))

            result = self.runner.run(test_suite)
            test_suite_summary = get_summary(result)

            name = test_suite.config.get("name")
            test_infos = TestCaseInfo.objects.filter(name=name).all()
            test_infos = list(test_infos)
            if test_infos:
                # 清除get_cache_case的缓存
                del_case_cache(test_infos[0].id)

            self.summary["success"] &= test_suite_summary["success"]
            test_suite_summary["name"] = name
            test_suite_summary["base_url"] = test_suite.config.get(
                "request", {}).get("base_url", "")
            test_suite_summary["output"] = test_suite.output
            print_output(test_suite_summary["output"])

            accumulate_stat(self.summary["stat"], test_suite_summary["stat"])
            accumulate_stat(self.summary["time"], test_suite_summary["time"])

            # 根据返回结果提取变量值
            if extract_list is not None and isinstance(extract_list, list):
                records = test_suite_summary.get('records')
                data_result = records[0].get('meta_data').get('response').get(
                    'json')

                if data_result is not None:
                    for extract in extract_list:
                        print(extract)
                        for key, value in extract.items():
                            try:
                                extract_value = {}
                                extract_value['content'] = data_result
                                fields = str(value).split('.')
                                extract_success = True
                                for field in fields:
                                    if isinstance(extract_value, dict):
                                        extract_value = extract_value.get(
                                            field)
                                    elif isinstance(extract_value, list):
                                        if extract_value:
                                            extract_value = extract_value[int(
                                                field)]
                                        else:
                                            extract_success = False
                                            # raise Exception('提取变量失败,', '路径:', value, '下的结果不存在,请检查')
                                if extract_success:
                                    extract_parameter[key] = extract_value
                            except AttributeError as e:
                                print('run运行错误:未提取到变量')
                            except Exception as e:
                                logger.log_error("出现错误.{0}".format(e))

            self.summary["details"].append(test_suite_summary)

        return self
Ejemplo n.º 10
0
def main_hrun():
    """ API test: parse command line options and run commands.
    """
    parser = argparse.ArgumentParser(description=__description__)
    parser.add_argument(
        '-V', '--version', dest='version', action='store_true',
        help="show version")
    parser.add_argument(
        'testset_paths', nargs='*',
        help="testset file path")
    parser.add_argument(
        '--no-html-report', action='store_true', default=False,
        help="do not generate html report.")
    parser.add_argument(
        '--html-report-name',
        help="specify html report name, only effective when generating html report.")
    parser.add_argument(
        '--html-report-template',
        help="specify html report template path.")
    parser.add_argument(
        '--log-level', default='INFO',
        help="Specify logging level, default is INFO.")
    parser.add_argument(
        '--log-file',
        help="Write logs to specified file path.")
    parser.add_argument(
        '--dot-env-path',
        help="Specify .env file path, which is useful for keeping production credentials.")
    parser.add_argument(
        '--failfast', action='store_true', default=False,
        help="Stop the test run on the first error or failure.")
    parser.add_argument(
        '--startproject',
        help="Specify new project name.")
    parser.add_argument(
        '--validate', nargs='*',
        help="Validate JSON testset format.")
    parser.add_argument(
        '--prettify', nargs='*',
        help="Prettify JSON testset format.")

    args = parser.parse_args()
    logger.setup_logger(args.log_level, args.log_file)

    if is_py2:
        logger.log_warning(get_python2_retire_msg())

    if args.version:
        logger.color_print("{}".format(__version__), "GREEN")
        exit(0)

    if args.validate:
        validate_json_file(args.validate)
        exit(0)
    if args.prettify:
        prettify_json_file(args.prettify)
        exit(0)

    project_name = args.startproject
    if project_name:
        project_path = os.path.join(os.getcwd(), project_name)
        create_scaffold(project_path)
        exit(0)

    runner = HttpRunner(failfast=args.failfast, dot_env_path=args.dot_env_path).run(args.testset_paths)

    if not args.no_html_report:
        runner.gen_html_report(
            html_report_name=args.html_report_name,
            html_report_template=args.html_report_template
        )

    summary = runner.summary
    print_output(summary["output"])
    return 0 if summary["success"] else 1