示例#1
0
class Main:
    def __init__(self):
        self.net = NetworkRequest()
        self.check = CheckResult()
        self.write = WriteReport()
        # self.msg = ''
        # self.code = 0

    def run(self):
        # 读json文件,取测试数据
        api_file = Tool.read_json("interface")
        test_data_file = Tool.read_json("test_data")
        expect_data_file = Tool.read_json("expect_data")

        # 获取接口的具体信息,循环读取某一接口
        api_list = api_file["api_list"]
        for api in api_list:
            url = api["url"]
            method = api["method"]
            need_login = api["need_login"]
            test_data = api["test_data"]
            expect_data_addr = api["expect_data"]
            expect_data = expect_data_file[expect_data_addr]
            print("期望结果", expect_data)
            params = test_data_file[test_data]

            if method == "get":
                result = self.net.get(url, params)
                result_dict = json.loads(result.content)
                print("实际结果", result_dict)
                print("开始比较实际结果和期望值")
                flag = self.check.comparison_result( expect_data, result_dict)
                self.write.write_report(url, params, expect_data, result_dict, flag)
示例#2
0
 def __init__(self):
     self.net = NetworkRequest()
     self.check = Check()
     self.write = WriteReport()
     self.interface = Tool.read_json('interface')
     self.api = Tool.read_json('api')
     self.token = self.net.request_token()
示例#3
0
 def __init__(self):
     self.net = ""
     self.check = Check()
     self.write = WriteReport()
     self.interface = Tool.read_json('interface')
     self.api = Tool.read_json('api')
     self.token = ""
示例#4
0
class Main:
    def __init__(self):
        self.net = NetworkRequest()
        self.check = Check()
        self.write = WriteReport()
        self.interface = Tool.read_json('interface')
        self.api = Tool.read_json('api')
        self.token = self.net.request_token()

    def run(self):
        for interface in self.interface['interfaces']:

            action_file = Tool.read_json(interface['addr'])

            actions = action_file['actions']  # 获取所有动作
            alldata = action_file['data']  # 获取所有测试数据

            for action in actions:

                interface = action["api"]  # 需要的接口
                dataname = action["data"]  # 需要的数据名字

                url, method, datas = self.get_one_api(self.api, interface)
                check, param, expect = self.get_one_data(dataname, alldata)

                param = self.data_processing(self.token, datas, param)
                self.request_method(url, method, check, param, expect)

    # 获取一个接口信息
    def get_one_api(self, interface_list, interface):
        url = interface_list[interface]["url"]
        method = interface_list[interface]["method"]
        datas = interface_list[interface]["data"]
        return url, method, datas

    # 获取一条接口信息
    def get_one_data(self, dataname, datas):
        data = datas[dataname]
        check = datas[dataname]["check"]
        param = datas[dataname]["input"]
        expect = datas[dataname]["output"]
        return check, param, expect

    # 根据参数的类型处理参数
    def data_processing(self, token, types, param):
        for item in types:
            name = item['name']
            type = item['type']
            must = item['must']
            if type == 1:
                pass
            elif type == 2:
                pass
            elif type == 3:
                param[name] = token
            elif type == 4:
                param[name] = Tool.hash_password(param[name], token)
        return param

    def request_method(self, url, method, check, param, expect_data):
        result_dict = {}
        if method == "get":
            result = self.net.get(url, param)
            result_dict = json.loads(result.content)
            print("实际结果", result_dict)
        if method == "post":
            result = self.net.post(url, param)
            result_dict = json.loads(result.content)
            print("实际结果", result_dict)
        if method == "delete":
            result = self.net.post(url, param)
            result_dict = json.loads(result.content)
            print("实际结果", result_dict)
        if method == "put":
            result = self.net.post(url, param)
            result_dict = json.loads(result.content)
            print("实际结果", result_dict)
        if check == 1:
            flag = self.check.comparison_result(expect_data, result_dict)
            self.write.write_report(url, param, expect_data, result_dict, flag)
        else:
            pass
示例#5
0
 def __init__(self):
     self.net = NetworkRequest()
     self.check = CheckResult()
     self.write = WriteReport()
 def __init__(self):
     self.net = NetworkRequest()
     # self.check = CheckResult()
     self.check = Check()
     self.write = WriteReport()
     self.token = self.net.request_token()
class Main:
    def __init__(self):
        self.net = NetworkRequest()
        # self.check = CheckResult()
        self.check = Check()
        self.write = WriteReport()
        self.token = self.net.request_token()

    def run(self):
        # 读json文件,取测试数据
        api_file = Tool.read_json("interface")
        test_data_file = Tool.read_json("test_data")
        expect_data_file = Tool.read_json("expect_data")

        # 获取接口的具体信息,循环读取某一接口
        api_list = api_file["api_list"]

        for api in api_list:
            print(api)
            url = api["url"]
            method = api["method"]
            need_login = api["need_login"]["type"]
            login_username = api["need_login"]["username"]
            login_password = api["need_login"]["password"]
            test_datas = api["test_data"]
            expect_data_addr = api["expect_data"]
            expect_datas = expect_data_file[expect_data_addr]
            params = test_data_file[test_datas]

            for index in range(len(params)):
                # print("测试和期望",params[index],expect_datas[index])
                param = params[index]
                expect_data = expect_datas[index]
                print("期望结果", expect_data)
                if '_token' in param.keys():
                    param["_token"] = self.token
                    print("测试数据", param)
                if need_login == 0:
                    print("执行不需要登录的")
                    if method == "get":
                        result = self.net.get(url, param)
                        result_dict = json.loads(result.content)
                        print("实际结果", result_dict)
                        # print("开始比较实际结果和期望值")
                        flag = self.check.comparison_result(
                            expect_data, result_dict)
                        self.write.write_report(url, param, expect_data,
                                                result_dict, flag)
                    elif method == "post":
                        result = self.net.post(url, param)
                        result_dict = json.loads(result.content)
                        print("实际结果", result_dict)
                        # print("开始比较实际结果和期望值")
                        flag = self.check.comparison_result(
                            expect_data, result_dict)
                        self.write.write_report(url, param, expect_data,
                                                result_dict, flag)
                    elif method == "delete":
                        # print("请求的params", param)
                        # print("执行需要登录的post")
                        result = self.net.post(url, param)
                        result_dict = json.loads(result.content)
                        print("实际结果", result_dict)
                        # print("开始比较实际结果和期望值")
                        flag = self.check.comparison_result(
                            expect_data, result_dict)
                        self.write.write_report(url, param, expect_data,
                                                result_dict, flag)
                    elif method == "put":
                        # print("请求的params", param)
                        # print("执行需要登录的post")
                        result = self.net.post(url, param)
                        result_dict = json.loads(result.content)
                        print("实际结果", result_dict)
                        # print("开始比较实际结果和期望值")
                        flag = self.check.comparison_result(
                            expect_data, result_dict)
                        self.write.write_report(url, param, expect_data,
                                                result_dict, flag)

                else:
                    print("执行登录")
                    parms = {}
                    hash_password = Tool.hash_password(login_password,
                                                       self.token)
                    parms["user_name"] = login_username
                    parms["_token"] = self.token
                    parms["password"] = hash_password
                    r_login = self.net.post("/auth/login", parms)
                    print("登录结果:", r_login.text)

                    if method == "get":
                        result = self.net.get(url, param)
                        result_dict = json.loads(result.content)
                        print("实际结果", result_dict)
                        # print("开始比较实际结果和期望值")
                        flag = self.check.comparison_result(
                            expect_data, result_dict)
                        self.write.write_report(url, param, expect_data,
                                                result_dict, flag)

                    elif method == "post":
                        # print("请求的params", param)
                        # print("执行需要登录的post")
                        result = self.net.post(url, param)
                        result_dict = json.loads(result.content)
                        print("实际结果", result_dict)
                        # print("开始比较实际结果和期望值")
                        flag = self.check.comparison_result(
                            expect_data, result_dict)
                        self.write.write_report(url, param, expect_data,
                                                result_dict, flag)

                    elif method == "delete":
                        # print("请求的params", param)
                        print("----执行需要登录的删除请求----")
                        result = self.net.post(url, param)
                        result_dict = json.loads(result.content)
                        print("实际结果:", result_dict)
                        # print("开始比较实际结果和期望值")
                        flag = self.check.comparison_result(
                            expect_data, result_dict)
                        self.write.write_report(url, param, expect_data,
                                                result_dict, flag)
                    elif method == "put":
                        # print("请求的params", param)
                        # print("执行需要登录的post")
                        result = self.net.post(url, param)
                        result_dict = json.loads(result.content)
                        print("实际结果", result_dict)
                        # print("开始比较实际结果和期望值")
                        flag = self.check.comparison_result(
                            expect_data, result_dict)
                        self.write.write_report(url, param, expect_data,
                                                result_dict, flag)
                result = self.net.post("/auth/logout", {})
                print('---------------')
示例#8
0
class Main:
    def __init__(self):
        self.net = ""
        self.check = Check()
        self.write = WriteReport()
        self.interface = Tool.read_json('interface')
        self.api = Tool.read_json('api')
        self.token = ""

    def run(self):

        for interface in self.interface['interfaces']:
            action_file = Tool.read_json(interface['addr'])
            interface_name = interface['name']

            actions = action_file['actions']  # 获取所有动作
            alldata = action_file['data']  # 获取所有测试数据

            for action_list in actions:
                self.net = NetworkRequest()
                self.token = self.net.request_token()

                for action in action_list["action"]:
                    interface = action["api"]  # 需要的接口
                    dataname = action["data"]  # 需要的数据名字

                    url, method, param_type_datas = self.get_one_api(
                        self.api, interface)
                    # check, param, expect = self.get_one_data(dataname, alldata)
                    check, test_datas = self.get_test_datas(dataname, alldata)
                    for test_data in test_datas:
                        print(test_data)
                        interface_description = test_data[
                            "Interface_description"]
                        print(interface_description)
                        param = test_data["input"]
                        expect = test_data["output"]
                        param = self.data_processing(self.token,
                                                     param_type_datas, param)
                        self.request_method(url, interface_name,
                                            interface_description, method,
                                            check, param, expect)

    # 获取一个接口信息
    def get_one_api(self, interface_list, interface):
        url = interface_list[interface]["url"]
        method = interface_list[interface]["method"]
        datas = interface_list[interface]["data"]
        return url, method, datas

    # 获取某接口测试数据
    def get_test_datas(self, dataname, datas):
        check = datas[dataname]["check"]
        test_datas = datas[dataname]["test_datas"]
        return check, test_datas

    # 根据参数的类型处理参数
    def data_processing(self, token, types, input_param):
        for item in types:
            name = item['name']
            type = item['type']
            must = item['must']
            if type == 1:
                pass
            elif type == 2:
                pass
            elif type == 3:
                input_param[name] = token
            elif type == 4:
                input_param[name] = Tool.hash_password(input_param[name],
                                                       token)
        return input_param

    def request_method(self, url, interface_name, interface_description,
                       method, check, param, expect_data):
        print('-------------------' + url)
        result_dict = {}
        if method == "get":
            result = self.net.get(url, param)
            result_dict = json.loads(result.content)
            print("请求参数", param)
            print("实际结果", result_dict)
        if method == "post":
            result = self.net.post(url, param)
            print(result.content)
            result_dict = json.loads(result.content)
            print("请求参数", param)
            print("实际结果", result_dict)
        if method == "delete":
            result = self.net.delete(url, param)
            result_dict = json.loads(result.content)
            print("请求参数", param)
            print("实际结果", result_dict)
        if method == "put":
            result = self.net.put(url, param)
            result_dict = json.loads(result.content)
            print("请求参数", param)
            print("实际结果", result_dict)
        if check == 1:
            flag = self.check.comparison_result_value(expect_data, result_dict)
            self.write.write_report(url, interface_name, interface_description,
                                    param, expect_data, result_dict, flag)
        elif check == 2:
            flag = self.check.comparison_result(expect_data, result_dict)
            self.write.write_report(url, interface_name, interface_description,
                                    param, expect_data, result_dict, flag)
        else:
            pass