Exemple #1
0
def auto_run_testsuite_pk(**kwargs):
    """
    :param pk: int 用例组主键
    :param config: int 运行环境
    :param project_id: int 项目id
    :return:
    """

    pk = kwargs.get('pk')
    run_type = kwargs.get('run_type')
    project_id = kwargs.get('project_id')

    name = models.Case.objects.get(pk=pk).name

    # 通过主键获取单个用例
    test_list = models.CaseStep.objects. \
        filter(case__id=pk).order_by("step").values("body")

    # 把用例加入列表
    testcase_list = []
    for content in test_list:
        body = eval(content["body"])

        if "base_url" in body["request"].keys():
            config = eval(models.Config.objects.get(name=body["name"], project__id=project_id).body)
            continue
        testcase_list.append(body)

    summary = debug_api(testcase_list, project_id, name=name, config=config, save=False)

    save_summary(f'{name}_'+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), summary, project_id, type=3)

    ding_message = DingMessage(run_type)
    ding_message.send_ding_msg(summary)
Exemple #2
0
def schedule_debug_suite(*args, **kwargs):
    """定时任务
    """

    project = kwargs["project"]
    suite = []
    test_sets = []
    config_list = []
    for pk in args:
        try:
            name = models.Case.objects.get(id=pk).name
            suite.append({"name": name, "id": pk})
        except ObjectDoesNotExist:
            pass

    for content in suite:
        test_list = models.CaseStep.objects. \
            filter(case__id=content["id"]).order_by("step").values("body")

        testcase_list = []
        config = None
        for content in test_list:
            body = eval(content["body"])
            if "base_url" in body["request"].keys():
                config = eval(
                    models.Config.objects.get(name=body["name"],
                                              project__id=project).body)
                continue
            testcase_list.append(body)
        config_list.append(config)
        test_sets.append(testcase_list)

    summary = debug_suite(test_sets, project, suite, config_list, save=False)
    save_summary("", summary, project, type=3)
Exemple #3
0
def schedule_debug_suite(*args, **kwargs):
    """定时任务
    """

    project = kwargs["project"]
    suite = []
    test_sets = []
    config_list = []
    for pk in args:
        try:
            name = models.Case.objects.get(id=pk).name
            suite.append({"name": name, "id": pk})
        except ObjectDoesNotExist:
            pass

    for content in suite:
        test_list = models.CaseStep.objects. \
            filter(case__id=content["id"]).order_by("step").values("body")

        testcase_list = []
        config = None
        for content in test_list:
            body = eval(content["body"])
            if "base_url" in body["request"].keys():
                config = eval(
                    models.Config.objects.get(name=body["name"],
                                              project__id=project).body)
                continue
            testcase_list.append(body)
        config_list.append(config)
        test_sets.append(testcase_list)

    summary, _ = debug_suite(test_sets,
                             project,
                             suite,
                             config_list,
                             save=False)
    task_name = kwargs["task_name"]

    if kwargs.get('run_type') == 'deploy':
        task_name = '部署_' + task_name
        report_type = 4
    else:
        report_type = 3

    save_summary(task_name,
                 summary,
                 project,
                 type=report_type,
                 user=kwargs.get('user', ''))

    strategy = kwargs["strategy"]
    if strategy == '始终发送' or (strategy == '仅失败发送'
                              and summary['stat']['failures'] > 0):
        # ding_message = DingMessage(run_type)
        # ding_message.send_ding_msg(summary, report_name=task_name)
        webhook = kwargs.get("webhook", "")
        if webhook:
            summary["task_name"] = task_name
            lark_message.send_message(summary=summary, webhook=webhook)
Exemple #4
0
def async_debug_test(test_case, project, name, report_name, config, test_data):
    """异步执行testcase
    """
    summary = debug_api(test_case,
                        project,
                        name=name,
                        config=config,
                        save=False,
                        test_data=test_data)
    save_summary(report_name, summary, project)
Exemple #5
0
def async_debug_api(test_case,
                    project,
                    name=None,
                    config=None,
                    save=False,
                    log_file=None):
    """异步执行api
    """
    summary = debug_api(test_case,
                        project,
                        name=f'批量运行{len(test_case)}条API',
                        config=config,
                        save=save,
                        log_file=log_file)

    save_summary(name, summary, project, api_type=1)
Exemple #6
0
def schedule_debug_suite(*args, **kwargs):
    """定时任务
    """
    project = int(kwargs["project"])

    sample_summary = []
    if not args:
        raise ValueError('任务列表为空,请检查')
    for cases in args:
        case_kwargs = cases.get('kwargs', '')
        test_list = models.CaseStep.objects.filter(
            case__id=cases["id"]).order_by("step").values("body")
        if not test_list:
            raise ValueError('用例缺失,请假查')
        report_name = cases["name"]
        case_name = cases["name"]
        test_case = []
        config = None
        temp_config = []
        test_data = None
        temp_baseurl = ''
        g_host_info = ''
        if case_kwargs:
            report_name = case_kwargs["testCaseName"]
            if case_kwargs.get("excelTreeData", []):
                test_data = tuple(case_kwargs["excelTreeData"])
            if case_kwargs["hostInfo"] and case_kwargs["hostInfo"] != "请选择":
                g_host_info = case_kwargs["hostInfo"]
                host = models.HostIP.objects.get(name=g_host_info,
                                                 project__id=project)
                _host_info = json.loads(host.hostInfo)
                temp_config.extend(_host_info["variables"])
                temp_baseurl = host.base_url if host.base_url else ''

        for content in test_list:
            body = eval(content["body"])
            if "base_url" in body["request"].keys():
                config = eval(
                    models.Config.objects.get(name=body["name"],
                                              project__id=project).body)
                continue
            test_case.append(parse_host(g_host_info, body))

        if config and g_host_info not in ["请选择", '']:
            config["variables"].extend(temp_config)
            if temp_baseurl:
                config["request"]["base_url"] = temp_baseurl
        if not config and g_host_info not in ["请选择", '']:
            config = {
                "variables": temp_config,
                "request": {
                    "base_url": temp_baseurl
                }
            }

        summary = debug_api(test_case,
                            project,
                            name=case_name,
                            config=parse_host(g_host_info, config),
                            save=False,
                            test_data=test_data)
        summary["name"] = report_name
        sample_summary.append(summary)

    if sample_summary:
        summary_report = get_summary_report(sample_summary)
        save_summary(kwargs["task_name"], summary_report, project, type=3)
        is_send_email = control_email(sample_summary, kwargs)
        if is_send_email:
            sensitive_keys = kwargs.get('sensitive_keys', [])
            runresult = parser_runresult(sample_summary, sensitive_keys)

            peoject_name = models.Project.objects.get(id=project).name
            subject_name = peoject_name + ' - ' + kwargs["task_name"]
            if runresult["fail_task"] > 0:
                subject_name += " - 失败:" + ",".join(
                    [err_msg["proj"] for err_msg in runresult["error_list"]])
            else:
                subject_name += " - 成功!"
            html_conetnt = prepare_email_content(runresult, subject_name)
            send_file_path = prepare_email_file(summary_report)
            send_status = send_result_email(subject_name,
                                            kwargs["receiver"],
                                            kwargs["mail_cc"],
                                            send_html_content=html_conetnt,
                                            send_file_path=send_file_path)
            if send_status:
                print('邮件发送成功')
            else:
                print('邮件发送失败')
Exemple #7
0
def async_debug_suite(suite, project, obj, report, config):
    """异步执行suite
    """
    summary = debug_suite(suite, project, obj, config=config, save=False)
    save_summary(report, summary, project)
Exemple #8
0
def async_debug_api(api, project, name, config=None):
    """异步执行api
    """
    summary = debug_api(api, project, config=config, save=False)
    save_summary(name, summary, project)
Exemple #9
0
def schedule_debug_suite(*args, **kwargs):
    """定时任务
    """
    project = int(kwargs["project"])

    sample_summary = []
    for cases in args:
        case_kwargs = cases.get('kwargs', '')
        test_list = models.CaseStep.objects.filter(
            case__id=cases["id"]).order_by("step").values("body")
        report_name = cases["name"]
        case_name = cases["name"]
        test_case = []
        config = None
        temp_config = []
        test_data = None
        temp_baseurl = ''
        g_host_info = ''
        if case_kwargs:
            report_name = case_kwargs["testCaseName"]
            if case_kwargs["currentTestDataExcel"] != '请选择' and case_kwargs[
                    "currentTestDataSheet"]:
                test_data = (case_kwargs["currentTestDataExcel"],
                             case_kwargs["currentTestDataSheet"])
            if case_kwargs["hostInfo"] != "请选择":
                g_host_info = case_kwargs["hostInfo"]
                host = models.HostIP.objects.get(name=g_host_info,
                                                 project__id=project)
                _host_info = json.loads(host.hostInfo)
                temp_config.extend(_host_info["variables"])
                temp_baseurl = host.base_url if host.base_url else ''

        for content in test_list:
            body = eval(content["body"])
            if "base_url" in body["request"].keys():
                config = eval(
                    models.Config.objects.get(name=body["name"],
                                              project__id=project).body)
                continue
            test_case.append(parse_host(host, body))

        if config and g_host_info not in ["请选择", '']:
            config["variables"].extend(temp_config)
            if temp_baseurl:
                config["request"]["base_url"] = temp_baseurl
        if not config and g_host_info not in ["请选择", '']:
            config = {
                "variables": temp_config,
                "request": {
                    "base_url": temp_baseurl
                }
            }

        summary = debug_api(test_case,
                            project,
                            name=case_name,
                            config=parse_host(host, config),
                            save=False,
                            test_data=test_data)
        save_summary(report_name, summary, project, type=3)
        if kwargs["strategy"] != '从不发送':
            summary["name"] = report_name
            sample_summary.append(summary)

    if sample_summary:
        runresult = parser_runresult(sample_summary)
        is_send_email = control_email(runresult, kwargs)
        if is_send_email:
            peoject_name = models.Project.objects.get(id=project).name
            subject_name = peoject_name + kwargs["task_name"]
            html_conetnt = prepare_email_content(runresult, subject_name)
            send_file_path = prepare_email_file(sample_summary)
            print(send_file_path[0])
            send_status = send_result_email(subject_name,
                                            kwargs["receiver"],
                                            kwargs["mail_cc"],
                                            send_html_content=html_conetnt,
                                            send_file_path=send_file_path)
            if send_status:
                print('邮件发送成功')
            else:
                print('邮件发送失败')
 def runBackTestCase(self, name):
     runner = HttpRunner(failfast=False)
     runner.run(self.needRunCase, mapping=self.__mapping)
     save_summary(name, runner.summary, self.__project)
     '''self.summary = parse_summary(runner.summary)
 def runBackAPI(self, name):
     runner = HttpRunner(failfast=False)
     runner.run(self.casePath, mapping=self.__mapping)
     save_summary(name, runner.summary, self.__project)
Exemple #12
0
def async_debug_suite(suite, project, obj, report, config):
    """异步执行suite
    """
    logger.info("async_debug_suite start!!!")
    summary = debug_suite(suite, project, obj, config=config, save=False)
    save_summary(report, summary, project)
Exemple #13
0
def async_debug_api(api, project, name, config=None):
    """异步执行api
    """
    logger.info("async_debug_api start!!!")
    summary = debug_api(api, project, config=config, save=False)
    save_summary(name, summary, project)