Exemplo n.º 1
0
def get_case_info(request):
    """
    获取接口数据
    :param request:
    :return:
    """
    if request.method == "POST":
        case_id = request.POST.get("caseId", "")
        if case_id == "":
            return common.response_failed("用例id为空!")

        case_obj = TestCase.objects.get(pk=case_id)
        module_obj = Module.objects.get(id=case_obj.module_id)
        module_name = module_obj.name

        project_name = Project.objects.get(id=module_obj.project_name_id).name

        case_info = {
            "modulename": module_name,
            "projectName": project_name,
            "name": case_obj.name,
            "url": case_obj.url,
            "reqMethod": case_obj.req_method,
            "reqType": case_obj.req_type,
            "reqHeader": case_obj.req_header,
            "reqParameter": case_obj.req_parameter,
            "assertText": case_obj.response_assert,
        }

        return common.response_succeed(data=case_info)
    else:
        return common.response_failed("请求方法错误!")
Exemplo n.º 2
0
def get_task_info(request):
    if request.method == "POST":
        tid = request.POST.get("taskId", "")
        if tid == "":
            return common.response_failed("任务id不能为空")

        task_obj = TestTask.objects.get(id=tid)
        task_info = {
            "id": task_obj.id,
            "name": task_obj.name,
            "describe": task_obj.describe
        }

        cases_id = task_obj.cases.split(",")
        print(cases_id)

        cases_list = return_cases_list()
        print(cases_list)

        for i in range(len(cases_list)):
            for cid in cases_id:
                if int(cid) == int(cases_list[i]["id"]):
                    cases_list[i]["status"] = True
                    break
            else:
                cases_list[i]["status"] = False
        task_info["cases"] = cases_list

        return common.response_succeed(message="获取成功!", data=task_info)
    else:
        return common.response_failed("请求方法错误")
Exemplo n.º 3
0
def api_debug(request):
    """
    HTTP接口调试
    :param request:
    :return: 接口调用结果
    """
    if request.method == "POST":
        url = request.POST.get("req_url", "")
        method = request.POST.get("req_method", "")
        type_ = request.POST.get("req_type", "")
        parameter = request.POST.get("req_parameter", "")

        if url == "" or method == "" or type_ == "":
            return common.response_failed("必传参数为空")

        payload = json.loads(parameter.replace("'", "\""))

        if method == "get":
            if type_ == "from":
                r = requests.get(url, params=payload)
            else:
                return common.response_failed("参数类型错误")

        if method == "post":
            if type_ == "from":
                r = requests.post(url, data=payload)
            elif type_ == "json":
                r = requests.post(url, json=payload)
            else:
                return common.response_failed("参数类型错误")

        return common.response_succeed(data=r.text)
    else:
        return common.response_failed("请求方法错误")
Exemplo n.º 4
0
def delete_task(request):
    if request.method == "POST":
        id = request.POST.get("id", "")
        if id == "":
            return common.response_failed("删除失败,ID不存在")
        TestTask.objects.filter(id=id).delete()
        return common.response_succeed(message="删除成功")
    else:
        return common.response_failed("请求方法错误!")
Exemplo n.º 5
0
def delete_task(request):
    if request.method == "POST":
        tid = request.POST.get("task_id", "")
        if tid == "":
            return common.response_failed("任务id不能为空")

        task_obj = TestTask.objects.get(id=tid)
        task_obj.delete()
        return common.response_succeed(message="删除成功!")
    else:
        return common.response_failed("请求方法错误")
Exemplo n.º 6
0
def save_task(request):
    if request.method == "POST":
        name = request.POST.get("task_name", "")
        describe = request.POST.get("task_describe", "")
        cases = request.POST.get("task_cases", "")
        if name == '':
            return common.response_failed("名称不能为空!")
        if cases == '':
            return common.response_failed("用例不能为空!")
        task = TestTask.objects.create(name = name,describe=describe,cases=cases,status=0)
        if task is not None:
            return common.response_succeed("保存成功!")

    else:
        return common.response_failed("请求方法错误!")
Exemplo n.º 7
0
def save_case(request):
    """
    保存接口测试用例
    :param request:
    :return:
    """
    if request.method == "POST":
        name = request.POST.get("name", "")
        url = request.POST.get("req_url", "")
        method = request.POST.get("req_method", "")
        parameter = request.POST.get("req_parameter", "")
        req_type = request.POST.get("req_type", "")
        header = request.POST.get("header", "")
        module_name = request.POST.get("module", "")
        assert_text = request.POST.get("assert_text", "")

        if url == "" or method == "" or req_type == "" or module_name == "" or assert_text == "":
            return common.response_failed("必传参数为空")

        if parameter == "":
            parameter = "{}"
        if header == "":
            header = "{}"

        module_obj = Module.objects.get(name=module_name)
        case = TestCase.objects.create(name=name,
                                       module=module_obj,
                                       url=url,
                                       req_method=method,
                                       req_header=header,
                                       req_type=req_type,
                                       req_parameter=parameter,
                                       respose_assert=assert_text)
        if case is not None:
            return common.response_succeed("保存成功!")
Exemplo n.º 8
0
def get_project_list(request):
    """
    获取项目模块列表
    :param request:
    :return: 项目接口列表
    """
    if request.method == "GET":
        project_list = Project.objects.all()
        data_list = []
        for project in project_list:
            project_dict = {
                "name": project.name
            }
            module_list = Module.objects.filter(project_id=project.id)
            if len(module_list) != 0:
                module_name = []
                for module in module_list:
                    module_name.append(module.name)

                project_dict["moduleList"] = module_name
                data_list.append(project_dict)

        return common.response_succeed(data=data_list)

    else:
        return common.response_failed("请求方法错误")
Exemplo n.º 9
0
def api_assert(request):
    """对测试用例的断言进行验证"""
    if request.method == "POST":
        result_text = request.POST.get("result", "")
        assert_text = request.POST.get("assert", "")

        if result_text == "" or assert_text == "":
            return common.response_failed("验证的数据不能为空")

        try:
            assert assert_text in result_text
        except AssertionError:
            return common.response_failed("验证失败!")
        else:
            return common.response_succeed("验证成功!")
    else:
        return common.response_failed("请求方法错误")
Exemplo n.º 10
0
def update_case(request):
    if request.method == 'POST':
        case_id = request.POST.get("cid", "")
        name = request.POST.get("name", "")
        url = request.POST.get("req_url", "")
        method = request.POST.get("req_method", "")
        parameter = request.POST.get("req_parameter", "")
        type_ = request.POST.get("req_type", "")
        header = request.POST.get("header", "")
        module_name = request.POST.get("module", "")
        project_name = request.POST.get("project", "")
        assert_text = request.POST.get("assert_text", "")
        if validators.url(url) != True:
            return common.response_failed("请检查URL!")
        if method not in ['post','get']:
            return common.response_failed("请检查请求方法!")
        if header != '':
            try:
                header_dict = json.loads(header.replace("'", "\""))
                if type(header_dict) != dict:
                    raise TypeError
            except Exception:
                return common.response_failed("请检查header格式!")
        else:
            header = "{}"

        if parameter != '':
            try:
                payload = json.loads(parameter.replace("'", "\""))
                if type(payload) != dict:
                    raise TypeError
            except json.decoder.JSONDecodeError:
                return common.response_failed("请检查参数的格式!")
        else:
            parameter = "{}"

        project = Project.objects.get(name = project_name)
        module_obj = Module.objects.get(name = module_name,project__id=project.id)
        try:
            TestCase.objects.filter(id=case_id).update(name = name,module=module_obj,url=url,req_method=method
                                ,req_type=type_,req_header=header,req_parameter=parameter,responses_assert=assert_text)
        except Exception as e:
            return common.response_failed("保存失败!")
        return common.response_succeed("保存成功!")
    else:
        return HttpResponse("404")
Exemplo n.º 11
0
def update_case(request):
    """
    更新接口测试用例
    :param request:
    :return:
    """
    if request.method == "POST":
        case_id = request.POST.get("cid", "")
        name = request.POST.get("name", "")
        url = request.POST.get("req_url", "")
        method = request.POST.get("req_method", "")
        parameter = request.POST.get("req_parameter", "")
        req_type = request.POST.get("req_type", "")
        header = request.POST.get("header", "")
        module_name = request.POST.get("module", "")
        project_name = request.POST.get("project","")
        assert_text = request.POST.get("assert_text", "")
        print("接口id", case_id)

        if url == "" or method == "" or req_type == "" or module_name == "" or assert_text == "":
            return common.response_failed("必传参数为空")

        if parameter == "":
            parameter = "{}"

        if header == "":
            header = "{}"

        module_obj = Module.objects.filter(project__name=project_name).filter(name=module_name)[0]
        case_obj = TestCase.objects.filter(id=case_id).update(
                module=module_obj,
                name=name,
                url=url,
                req_method=method,
                req_header=header,
                req_type=req_type,
                req_parameter=parameter,
                resp_assert=assert_text
            )
        if case_obj == 1:
            return common.response_succeed("更新成功!")
        else:
            return common.response_failed("更新失败!")
    else:
        return common.response_failed("请求方法错误")
Exemplo n.º 12
0
def save_task_data(request):
    if request.method == "POST":
        name = request.POST.get("task_name", "")
        describe = request.POST.get("task_describe", "")
        cases = request.POST.get("task_cases", "")

        if name == "":
            return common.response_failed("任务的名称不能为空")

        # 保存数据库
        task = TestTask.objects.create(name=name,
                                       describe=describe,
                                       cases=cases)
        if task is None:
            return common.response_failed("创建失败")

        return common.response_succeed(message="创建任务成功!")
    else:
        return common.response_failed("请求方法错误")
Exemplo n.º 13
0
def task_result(request):
    if request.method == "POST":
        rid = request.POST.get("result_id", "")
        result_obj = TestResult.objects.get(id=rid)
        data = {
            "result": result_obj.result,
        }
        return common.response_succeed(message="获取成功!", data=data)
    else:
        return common.response_failed("请求方法错误")
Exemplo n.º 14
0
def run_task(request):
    if request.method == "POST":
        tid = request.POST.get("task_id", "")
        if tid == "":
            return common.response_failed("任务ID不能为空")

        task_list = TestTask.objects.all()
        runing_task = 0
        for task in task_list:
            if task.status == 1:
                runing_task = 1
                break
        if runing_task == 1:
            return common.response_failed("当前有任务正在执行...")
        else:
            TaskThread(tid).new_run()
            return common.response_succeed(message="已执行")
    else:
        return common.response_failed("请求方法错误")
Exemplo n.º 15
0
def get_case_list(request):
    """
    获取测试用例列表
    :param request:
    :return:
    """
    if request.method == "GET":
        cases_list = return_cases_list()
        return common.response_succeed(data=cases_list)
    else:
        return common.response_failed("请求方法错误")
Exemplo n.º 16
0
def run_task(request):
    '''运行任务i'''
    if request.method =="POST":
        id = request.POST.get("id","")

        if id =="":
            return common.response_failed("任务id不能为空")

        task_obj = TestTask.objects.all()
        runing_task = 0
        for i in task_obj:
            if i.status == 1:
                runing_task = 1
                break
        if runing_task == 1:
            return common.response_failed("当前有任务正在执行...")
        else:
            TaskThread(id).run_new()
            return common.response_succeed(message="已执行")
    else:
        return common.response_failed("请求方法错误")
Exemplo n.º 17
0
def api_debug(request):
    """
    HTTP接口调试
    :param request:
    :return: 接口调用结果
    """
    if request.method == "POST":
        url = request.POST.get("req_url", "")
        method = request.POST.get("req_method", "")
        type = request.POST.get("req_type", "")
        parameter = request.POST.get("req_parameter", "")
        header = request.POST.get("req_header", "")

        if url == "" or method == "" or type == "":
            return common.response_failed("必传参数为空!")

        try:
            header_dict = json.loads(header.replace("'", "\""))
            payload = json.loads(parameter.replace("'", "\""))
        except json.decoder.JSONDecodeError:
            return common.response_failed("请检查header或参数的格式!")

        if method == "get":
            if type == "from":
                r = requests.get(url, params=parameter)
            else:
                return common.response_failed("参数类型错误!")

        if method == "post":
            if type == "from":
                r = requests.post(url, data=parameter)
            elif type == "json":
                r = requests.post(url, json=parameter)
            else:
                return common.response_failed("参数类型错误!")

        return common.response_succeed(data=r.text)
    else:
        return common.response_failed("请求方法错误!")
Exemplo n.º 18
0
def task_result(request):
    '''任务结果'''
    if request.method == "POST":
        id = request.POST.get("id", "")

        result_obj = TestResult.objects.get(id=id)

        data={
           "result":result_obj.result
        }
        return common.response_succeed(message="获取数据成功!",data=data)
    else:
        return common.response_failed(message="请求方法错误!")
Exemplo n.º 19
0
def updata_task(request):

    if request.method == "POST":
        tid = request.POST.get("task_id", "")
        name = request.POST.get("task_name", "")
        describe = request.POST.get("task_describe", "")
        cases = request.POST.get("task_cases", "")
        if tid == "":
            return common.response_failed("任务id不能为空")

        # 去掉最后一个字符
        if cases[-1] == ",":
            cases = cases[:-1]

        task_obj = TestTask.objects.get(id=tid)
        task_obj.name = name
        task_obj.describe = describe
        task_obj.cases = cases
        task_obj.save()

        return common.response_succeed(message="保存成功!")
    else:
        return common.response_failed("请求方法错误")
Exemplo n.º 20
0
def get_task_info(request):
    if request.method == "POST":
        task_id = request.POST.get('taskId','')
        if task_id == '':
            return common.response_failed("task id Null.")
        task_obj = TestTask.objects.get(pk=task_id)
        task_info = {
            "task_name":task_obj.name,
            "task_describe":task_obj.describe,
            "task_cases" : task_obj.cases,
        }
        return common.response_succeed(data=task_info)
    else:
        return HttpResponse("404")
Exemplo n.º 21
0
def get_case_list(request):
    """获取项目用例列表"""
    if request.method == "GET":
        project_list = Project.objects.all()
        case_list = []
        for project in project_list:
            module_list = Model.objects.filter(project_id=project.id)
            for module in module_list:
                cases = TestCase.objects.filter(module_id=module.id)
                for case in cases:
                    case_info = project.title + "->" + module.name + "->" + case.name
                    case_dict = {"id": case.id, "name": case_info}
                    case_list.append(case_dict)
        return common.response_succeed(data=case_list)

    else:
        return common.response_failed("请求方法错误")
Exemplo n.º 22
0
def debug_case(request):
    if request.method == 'POST':
        url = request.POST.get("req_url",'')
        method = request.POST.get("req_method",'')
        type_ = request.POST.get("req_type",'')
        header = request.POST.get("req_header",'')
        parameter = request.POST.get("req_parameter",'')
        if validators.url(url) != True:
            return common.response_failed("请检查URL!")
        if method not in ['post','get']:
            return common.response_failed("请检查请求方法!")
        if header != '':
            try:
                header_dict = json.loads(header.replace("'", "\""))
                if type(header_dict) != dict:
                    raise TypeError
            except Exception:
                return common.response_failed("请检查header格式!")
        else:
            header_dict = None

        if parameter != '':
            try:
                payload = json.loads(parameter.replace("'", "\""))
                if type(payload) != dict:
                    raise TypeError
            except json.decoder.JSONDecodeError:
                return common.response_failed("请检查参数的格式!")
        else:
            payload = None

        if method == "get":
            if type_ == "from":
                r = requests.get(url, headers=header_dict, params=payload)
            else:
                return common.response_failed("参数类型错误")

        if method == "post":
            if type_ == "from":
                r = requests.post(url, headers=header_dict, data=payload)
            elif type_ == "json":
                r = requests.post(url, headers=header_dict, json=payload)
            else:
                return common.response_failed("参数类型错误")
        print(r.text)
        return common.response_succeed(data=r.text)
    else:
        return common.response_failed("请求方法错误")
Exemplo n.º 23
0
def get_case_list(request):
    #项目——》模块——》用例
    if request.method == "GET":
        task_cases = request.GET.get("task_case", "")
        cases_list = []
        projects = Project.objects.all()
        for project in projects:
            modules = Module.objects.filter(project_id = project.id)
            for module in modules:
                cases = TestCase.objects.filter(module_id = module.id)
                for case in cases:
                    case_info = project.name + "--->" + module.name + "--->" + case.name
                    case_dict = {
                        "id":case.id,
                        "name":case_info
                    }
                    cases_list.append(case_dict)
        data = {"cases_list":cases_list,"check_cases":task_cases}
        return common.response_succeed(data=data)
    else:
        return common.response_failed("请求方法错误!")
Exemplo n.º 24
0
def get_case_list(request):
    """
    获取测试用例列表
    :param request:
    :return:
    """
    if request.method == "GET":
        cases_list = []

        projects = Project.objects.all()
        for project in projects:
            modules = Module.objects.filter(project_id=project.id)
            for module in modules:
                cases = TestCase.objects.filter(module_id=module.id)
                for case in cases:
                    case_info = project.name + " -> " + module.name + " -> " + case.name
                    case_dict = {'id': case.id, 'name': case_info}
                    cases_list.append(case_dict)
        return common.response_succeed(data=cases_list)

    else:
        return common.response_failed("请求方法错误")
Exemplo n.º 25
0
            header = "{}"

        module_obj = Module.objects.get(name=module_name)
        case = TestCase.objects.create(name=name,
                                       module=module_obj,
                                       url=url,
                                       req_method=method,
                                       req_header=header,
                                       req_type=req_type,
                                       req_parameter=parameter,
                                       respose_assert=assert_text)
        if case is not None:
            return common.response_succeed("保存成功!")

    else:
        return common.response_failed("请求方法错误!")


def api_assert(request):
    """
    对测试用例的断言进行验证
    :param request:
    :return:
    """
    if request.method == "POST":
        result_text = request.POST.get("result", "")
        assert_text = request.POST.get("assert", "")

        if result_text == "" or assert_text == "":
            return common.response_failed("验证的数据不能为空!")