Esempio n. 1
0
class write_Return_Value():
    # operation_Excel实例化
    Exel = operation_Excel()
    # 实例化文件写入读取工具
    of = operation_File()

    # 获取返回值并写入文件及Excel(tcp:测试用例类,test_return_data:返回值,sheet:当前的sheet,sheet_test_col:首行名称所对应的坐标)
    def operation_Return_Parameter(self, tcp, test_return_data, sheet,
                                   sheet_test_col):
        # 将返回值放入测试用例对象中
        tcp.test_return_data = json.loads(test_return_data.text)
        # 将返回值写入返回值文件中
        self.of.write_File(file_name="return_Data_Json.json",
                           write_let=tcp.test_return_data)
        # 将返回值写入Excel中
        self.Exel.write_Excel(sheet=sheet,
                              row=tcp.row,
                              col=sheet_test_col["返回参数"],
                              content=tcp.test_return_data)

    # 如果请求接口抛出异常,就将异常写入Excel中
    def over_Return_Parameter(self, tcp, test_return_data, sheet,
                              sheet_test_col):
        # 将返回值放入测试用例对象中
        tcp.test_return_data = str(test_return_data)
        # 将返回值写入返回值文件中
        self.of.write_File(file_name="return_Data_Json.json",
                           write_let=tcp.test_return_data)
        # 将返回值写入Excel中
        self.Exel.write_Excel(sheet=sheet,
                              row=tcp.row,
                              col=sheet_test_col["返回参数"],
                              content=tcp.test_return_data)
Esempio n. 2
0
class c_assert():
    # operation_Excel实例化
    Exel = operation_Excel()
    #实例化文件写入读取工具
    of = operation_File()
    #实例化json转行工具
    oj = operation_Json()

    # 对比传入的断言,判断断言,并将断言内容,断言结果写入Excel中(tcp:测试用例类,test_return_data:返回值,sheet:当前的sheet,sheet_test_col:首行名称所对应的坐标)
    def judge_assert(self, tcp, test_return_data, sheet, sheet_test_col):
        # 取出断言文件中的内容
        assert_Json = self.of.read_File(file_name="assert_Json.json")
        if assert_Json == "":
            return None
        # 断言内容转换为JSON格式
        assert_Json = self.oj.read_Json(test_Json=assert_Json, Json_name="断言")
        # 将返回参数转换为JSON格式
        # print("返回值:" + test_return_data.text)
        # test_return_data1 = json.loads(test_return_data.text)
        # 将返回值装进字典集合中
        test_return_assert = {}
        is_succeed = "成功"
        # 循环断言的每个值,并作出判断
        for assert_key in assert_Json:
            tcp.find_key = assert_key
            self.oj.assert_Json(r=tcp.test_return_data, model_find=tcp)
            # 将返回的断言对应打的值放入字典中
            test_return_assert[assert_key] = tcp.All_value[0]
            # print("123:" + assert_Json[assert_key])
            # print("123:" + tcp.All_value[0])
            if assert_Json[assert_key] != tcp.All_value[0]:
                is_succeed = "失败"
        # 将返回断言值放入测试用例对象中
        tcp.test_return_assert = test_return_assert
        # 将返回的断言值写入返回断言值文件中
        self.of.write_File(file_name="return_Assert_Json.json",
                           write_let=test_return_assert)
        # 将返回值写入Excel中
        self.Exel.write_Excel(sheet=sheet,
                              row=tcp.row,
                              col=sheet_test_col["断言的获取值"],
                              content=str(test_return_assert))
        # 将断言结果写入Excel中
        self.Exel.write_Excel(sheet=sheet,
                              row=tcp.row,
                              col=sheet_test_col["断言结果"],
                              content=is_succeed)
class list_Assert():
    # operation_Excel实例化
    Exel = operation_Excel()
    # 实例化文件写入读取工具
    of = operation_File()
    #实例化json转行工具
    oj = operation_Json()

    # 对比传入的断言,判断断言,并将断言内容,断言结果写入Excel中(tcp:测试用例类,test_return_data:返回值,sheet:当前的sheet,sheet_test_col:首行名称所对应的坐标)
    def list_judge_assert(self, tcp, sheet, sheet_test_col):
        # # 取出模糊查询的数据
        # dim_find = list_assert_dick["dim_find"]
        # # 取出精确查询的数据
        # precise_find = list_assert_dick["precise_find"]
        # # 取出数量
        # num = list_assert_dick["num"]
        # 取出列表断言中的值,并做处理
        list_assert_dick = self.list_assert_data_out()
        #层数
        tier = list_assert_dick["tier"]
        # 取出列表名
        data_name = list_assert_dick["列表名"]
        # 获取列表
        tcp.All_value = []
        tcp.find_key = data_name
        self.oj.assert_Json(r=tcp.test_return_data, model_find=tcp)
        if len(tcp.All_value) < (tier - 1):
            self.Exel.write_Excel(sheet=sheet,
                                  row=tcp.row,
                                  col=sheet_test_col["列表断言结果"],
                                  content="失败")
            return
        #取出列表
        list_data = tcp.All_value[tier - 1]
        #判断成功还是失败
        is_succeed = self.juder(list_data=list_data,
                                list_assert_dick=list_assert_dick)
        #结果写入
        self.write_Excel(tcp=tcp,
                         list_data=list_data,
                         is_succeed=is_succeed,
                         sheet=sheet,
                         sheet_test_col=sheet_test_col)

    def write_Excel(self, tcp, list_data, is_succeed, sheet, sheet_test_col):
        # 将返回断言值放入测试用例对象中
        tcp.test_return_list_assert = list_data
        # 将返回的断言值写入返回断言值文件中
        self.of.write_File(file_name="return_List_Assert_Json.json",
                           write_let=list_data)
        # 将列表返回值写入Excel中
        self.Exel.write_Excel(sheet=sheet,
                              row=tcp.row,
                              col=sheet_test_col["列表断言获取值"],
                              content=str(list_data))
        # 将断言结果写入Excel中
        self.Exel.write_Excel(sheet=sheet,
                              row=tcp.row,
                              col=sheet_test_col["列表断言结果"],
                              content=is_succeed)

    # 判断断言成功还是失败
    def juder(self, list_data, list_assert_dick):
        is_succeed = "失败"
        # 取出模糊查询的数据
        dim_find = list_assert_dick["dim_find"]
        # 取出精确查询的数据
        precise_find = list_assert_dick["precise_find"]
        # 取出列表名
        data_name = list_assert_dick["列表名"]
        # 取出数量
        num = list_assert_dick["num"]
        # 层数
        tier = list_assert_dick["tier"]
        #交并
        intersection_or_union = list_assert_dick["交并"]
        jp = self.juder_precise(list_data=list_data, precise_find=precise_find)
        jd = self.juder_dim(list_data=list_data, dim_find=dim_find)
        jn = self.juder_num(list_data=list_data, num=num)
        if jn:
            return is_succeed
        if self.juder_key(self,
                          list_data=list_data,
                          precise_find=precise_find,
                          dim_find=dim_find):
            return is_succeed
        if intersection_or_union == "交":
            if jp and jd:
                is_succeed = "成功"
                return is_succeed
        elif intersection_or_union == "并":
            if self.juder_union(list_data=list_data,
                                precise_find=precise_find,
                                dim_find=dim_find):
                is_succeed = "成功"
                return is_succeed

        return is_succeed

    #key值判断
    def juder_key(self, list_data, precise_find, dim_find):
        precise_dim = []
        for pf in precise_find:
            precise_dim.append(pf)
        for df in dim_find:
            precise_dim.append(df)

        for pd in precise_dim:
            is_succeed = False
            for ld in list_data:
                for l in ld:
                    if pd == l:
                        is_succeed = True
                        break
                if is_succeed == False:
                    return is_succeed
        return True

    #并集判断
    def juder_union(self, list_data, precise_find, dim_find):
        for ld in list_data:
            for pf in precise_find:
                if precise_find[pf] != ld[pf]:
                    return False
            for df in dim_find:
                if precise_find[df] not in precise_find[df]:
                    return False
        return True

    #精确判断成功还是失败
    def juder_precise(self, list_data, precise_find):
        if precise_find == {}:
            return True
        for pf in precise_find:
            for ld in list_data:
                if ld[pf] != precise_find[pf]:
                    return False
        return True

    # 模糊判断成功还是失败
    def juder_dim(self, list_data, dim_find):
        if dim_find == {}:
            return True
        for pf in dim_find:
            for ld in list_data:
                if ld[pf] not in dim_find[pf]:
                    return False
        return True

    #数量判断成功还是失败
    def juder_num(self, list_data, num):
        if num == -1:
            return True
        if len(list_data) != num:
            return False
        return True

    # 取出列表断言中的值,并做处理(return:list_assert_dick:num(num),交并(交并),dim_find(模糊查询),precise_find(精确查询))
    def list_assert_data_out(self):
        # 取出列表断言文件中的内容
        list_assert_Json = self.of.read_File(file_name="list_assert_Json.json")
        # 断言内容转换为JSON格式
        list_assert_Json = self.oj.read_Json(test_Json=list_assert_Json,
                                             Json_name="断言列表")

        # 列表查询的dick调整后的dick
        list_assert_dick = {}

        # 获取num”,“交并”,“模糊查询”的值
        list_assert_dick["num"] = list_assert_Json["num"]
        list_assert_dick["交并"] = list_assert_Json["交并"]
        list_assert_dick["列表名"] = list_assert_Json["列表名"]
        list_assert_dick["tier"] = list_assert_Json["tier"]
        # 需要模糊查询的值
        fuzzy_query = list_assert_Json["模糊查询"]
        # 将需要模糊查询的值转换为list
        fuzzy_query_list = fuzzy_query.split(",")
        # 获取需要查询的值
        dim_find = {}  # 模糊查询
        precise_find = {}  # 精确查询
        # 将模糊查询的值和精确查询的值分开
        for laj in list_assert_Json:
            if laj != "num" and laj != "交并" and laj != "模糊查询" and laj != "列表名" and laj != "tier":
                i = 0
                for fql in fuzzy_query_list:
                    if fql == laj:
                        dim_find[laj] = list_assert_Json[laj]
                        break
                    i = i + 1
                    if i == len(fuzzy_query_list):
                        precise_find[laj] = list_assert_Json[laj]
        # 将模糊查询和精确查询装入dick中
        list_assert_dick["dim_find"] = dim_find
        list_assert_dick["precise_find"] = precise_find
        return list_assert_dick
class realize_File():

    # operation_Excel实例化
    Exel = operation_Excel()
    #实例化文件写入读取工具
    of = operation_File()
    #实例化json转行工具
    oj = operation_Json()
    #实例化接口请求工具
    tr = test_request()
    #返回值写入类
    wrv = write_return_value.write_Return_Value()
    #普通断言
    ca = common_assert.c_assert()
    #业务处理类
    bd = business_dispose.business_Dispose()
    #列表断言处理
    la = list_assert.list_Assert()

    #操作测试用例,并返回值
    def Test_Case_Integration(self, test_Case_File):
        #将文件存入,并获取文件路径
        file_path_name = file_handing().stored_File(file_obj=test_Case_File)
        file_path = file_path_name["file_path"]
        file_name = file_path_name["file_name"]
        abbreviation = file_path_name["abbreviation"]
        #临时地址
        # file_path = "C:/Users/lx/Desktop/接口自动化/InterfaceAutomationTest/realize_package/File_data/A2BF350697F411EA89F0005056C00008接口自动化测试用例.xlsx"
        #打开Excel文件
        Excle_File = self.Exel.open_Excel(file_path=file_path)
        # 获取"是否执行"sheet页的用例条数
        Y_sheet_list = self.sheet_execute(Excle_File=Excle_File)
        # print(Y_sheet_list[0])
        #获取当前执行的sheet页的用例
        self.test_case_sheet(Excle_File=Excle_File, Y_sheet_list=Y_sheet_list)
        #保存文件
        self.Exel.save_File(Excle_File=Excle_File, file_path=file_path)
        #关闭文件
        # self.Exel.clos_Excel(Excle_File=Excle_File)
        #将文件转移到定期清理的文件夹
        file_new_path = self.of.copy(old_file_paht=file_path,
                                     file_name=file_name)
        #需要返回的文件信息
        return_file = {
            "file": self.of.file_iterator(file_name=file_name),
            "abbreviation": abbreviation,
            "file_name": file_name,
            "file_path": file_path,
            "file_new_path": file_new_path
        }
        #删除文件
        self.of.remove_file(file_name=file_name)
        return return_file

    #"是否执行"sheet页
    def sheet_execute(self, Excle_File):
        #获取“是否执行”的sheet文件
        sheet = self.Exel.get_sheet(Excle_File=Excle_File, sheet_name="是否执行")
        #获取文件的最大行和最大列
        row_col = self.Exel.all_Information(sheet=sheet)

        #获取首行所有名称
        Excel_list_row = self.Exel.get_fiest_line(
            sheet=sheet, max_col=row_col["max_columns"])

        #找出“执行”的列
        for i in Excel_list_row:
            if i.first_line_name == "执行":
                sheet_zx_col = i.col
        #获取"执行"的一列数据
        Excel_list_col = self.Exel.get_All_row(sheet=sheet,
                                               max_row=row_col["max_row"],
                                               col=sheet_zx_col)

        Y_sheet_list = []
        #获取”Y“所对应的sheet名称
        for i in Excel_list_col:
            if i.first_line_name == "Y":
                self.Exel.read_one_cell(sheet=sheet, row=i.row,
                                        col=i.col).value
                Y_sheet_list.append(
                    self.Exel.read_one_cell(sheet=sheet,
                                            row=i.row,
                                            col=i.col - 1).value)
        #将所有“执行”为“Y”的sheet名字返回
        return Y_sheet_list

    #测试用例sheet执行
    def test_case_sheet(self, Excle_File, Y_sheet_list):

        for Ysl in Y_sheet_list:
            # 循环所有的”执行“为”Y“的sheet
            inerease_row = 2
            # test_Case_Property集合
            tcp_list = []
            # 获取测试用例的sheet文件
            sheet = self.Exel.get_sheet(Excle_File=Excle_File, sheet_name=Ysl)
            # 获取文件的最大行和最大列
            row_col = self.Exel.all_Information(sheet=sheet)
            Excel_list_row = self.Exel.get_fiest_line(
                sheet=sheet, max_col=row_col["max_columns"])

            # 找出当前用例中入参所对应的列
            sheet_test_col = self.bd.get_first_line_r_c(
                Excel_list_row=Excel_list_row)

            #循环所有的测试用例并放入tcp中
            while inerease_row <= row_col["max_row"]:
                # test_Case_Propert实例化
                tcp = test_Case_Property()
                # 测试编号
                tcp.test_id = None
                # url
                tcp.test_url = None
                # 请求方式
                tcp.test_request_way = None
                # header请求头
                tcp.test_header = None
                # param请求参数
                tcp.test_param = None
                # 入参数据
                tcp.test_data = None
                # cookie
                tcp.test_cookie = None
                # 返参数据
                tcp.test_return_data = None
                # 断言
                tcp.test_assert = None
                # 返回断言
                tcp.test_return_assert = None
                # 断言结果
                tcp.test_assert_result = None
                # 列表断言
                tcp.test_list_assert = None
                # 列表断言结果
                tcp.test_list_assert_result = None
                #列表断言获取值
                tcp.test_return_list_assert = None
                # 列表查询时列表的个数
                tcp.list_num = 0
                # 需要查询的key
                tcp.find_key = ""
                # 将需要查询的value封装成集合
                tcp.All_value = []
                # 该条测试用例的行
                tcp.row = 0

                #将当前行数放入测试用例中
                tcp.row = inerease_row
                #传值
                self.bd.test_Case_Property_by_value(
                    sheet=sheet,
                    inerease_row=inerease_row,
                    sheet_test_col=sheet_test_col,
                    tcp=tcp)
                tcp_list.append(tcp)
                inerease_row = inerease_row + 1
            self.calling_Interface(tcp_list=tcp_list,
                                   sheet=sheet,
                                   sheet_test_col=sheet_test_col)

    #执行测试用例,调用接口
    def calling_Interface(self, tcp_list, sheet, sheet_test_col):
        for tcp in tcp_list:
            #将值存入文件中
            self.bd.in_parameter_write(tcp)
            #获取文件里面的值
            all_in_parameter = self.bd.get_File()
            #调用接口请求方法
            test_return_data = self.tr.test_post_get_deleter(
                url=tcp.test_url,
                request_way=tcp.test_request_way,
                cookies=all_in_parameter["cookie_Json"],
                header=all_in_parameter["header_Json"],
                data=all_in_parameter["data_Json"],
                params=all_in_parameter["param_Json"])
            return_data_type = isinstance(test_return_data,
                                          requests.models.Response)
            if return_data_type == False:
                self.wrv.over_Return_Parameter(
                    tcp=tcp,
                    test_return_data=test_return_data,
                    sheet=sheet,
                    sheet_test_col=sheet_test_col)
                continue
            # 获取返回值并写入文件及Excel
            self.wrv.operation_Return_Parameter(
                tcp=tcp,
                test_return_data=test_return_data,
                sheet=sheet,
                sheet_test_col=sheet_test_col)
            # 对比传入的断言,判断断言,并将断言内容,断言结果写入Excel中
            self.ca.judge_assert(tcp=tcp,
                                 test_return_data=test_return_data,
                                 sheet=sheet,
                                 sheet_test_col=sheet_test_col)
            #列表断言
            self.la.list_judge_assert(tcp=tcp,
                                      sheet=sheet,
                                      sheet_test_col=sheet_test_col)

    #定时删除文件
    def remover_File(self):
        self.of.remover_scratch_File()
class business_Dispose():
    # operation_Excel实例化
    Exel = operation_Excel()
    #实例化文件写入读取工具
    of = operation_File()
    #实例化json转行工具
    oj = operation_Json()
    #将入参写入文件
    def in_parameter_write(self,tcp):
        self.of.write_File(file_name="header_Json.json",write_let=tcp.test_header)
        self.of.write_File(file_name="param_Json.json", write_let=tcp.test_param)
        self.of.write_File(file_name="data_Json.json",write_let=tcp.test_data)
        self.of.write_File(file_name="cookie_Json.json",write_let=tcp.test_cookie)
        self.of.write_File(file_name="assert_Json.json",write_let=tcp.test_assert)
        self.of.write_File(file_name="list_assert_Json.json", write_let=tcp.test_list_assert)
        #列表断言,暂时不实现
        # of.write_File(file_name="cookie_Json.json",write_let=tcp.test_list_assert)


    #test_Case_Property传值(sheet:sheet页面,inerease_row:行,sheet_test_col:列的集合(dick))
    def test_Case_Property_by_value(self,sheet,inerease_row,sheet_test_col,tcp):
        tcp.test_id = self.Exel.read_one_cell(sheet=sheet, row=inerease_row, col=sheet_test_col["用例编号(不可为空,不可重复)"]).value
        tcp.test_url = self.Exel.read_one_cell(sheet=sheet, row=inerease_row, col=sheet_test_col["url"]).value
        tcp.test_request_way = self.Exel.read_one_cell(sheet=sheet, row=inerease_row, col=sheet_test_col["请求方式"]).value
        tcp.test_header = self.Exel.read_one_cell(sheet=sheet, row=inerease_row, col=sheet_test_col["header请求头"]).value
        tcp.test_param = self.Exel.read_one_cell(sheet=sheet, row=inerease_row, col=sheet_test_col["param请求参数"]).value
        tcp.test_data = self.Exel.read_one_cell(sheet=sheet, row=inerease_row, col=sheet_test_col["入参数据"]).value
        tcp.test_cookie = self.Exel.read_one_cell(sheet=sheet, row=inerease_row, col=sheet_test_col["cookie"]).value
        tcp.test_assert = self.Exel.read_one_cell(sheet=sheet, row=inerease_row, col=sheet_test_col["断言"]).value
        tcp.test_list_assert = self.Exel.read_one_cell(sheet=sheet, row=inerease_row, col=sheet_test_col["列表断言"]).value



    #获取文件
    def get_File(self):
        #入参文件值的集合
        all_in_parameter = {}
        #获取需要的文件,并读取文件的值
        header_Json = self.of.read_File(file_name="header_Json.json")
        param_Json = self.of.read_File(file_name="param_Json.json")
        data_Json = self.of.read_File(file_name="data_Json.json")
        cookie_Json = self.of.read_File(file_name="cookie_Json.json")
        assert_Json = self.of.read_File(file_name="assert_Json.json")

        #如果文件里面有内容,就转为json,如果没有,就不转换
        if header_Json != "" and header_Json != None :
            header_Json = self.oj.read_Json(Json_name="header",test_Json=header_Json)
        if param_Json != "" and param_Json != None :
            param_Json = self.oj.read_Json(Json_name="param",test_Json=param_Json)
        if data_Json != "" and data_Json != None :
            data_Json = self.oj.read_Json(Json_name="data",test_Json=data_Json)
        if cookie_Json != "" and cookie_Json != None :
            cookie_Json = self.oj.read_Json(Json_name="cookie",test_Json=cookie_Json)
        if assert_Json != "" and assert_Json != None :
            assert_Json = self.oj.read_Json(Json_name="assert",test_Json=assert_Json)
        #将转换后的数据存入集合
        all_in_parameter["header_Json"] = header_Json
        all_in_parameter["param_Json"] = param_Json
        all_in_parameter["data_Json"] = data_Json
        all_in_parameter["cookie_Json"] = cookie_Json
        all_in_parameter["assert_Json"] = assert_Json


        return all_in_parameter


    # 获取首行需要的坐标值
    def get_first_line_r_c(self, Excel_list_row):
        # 入参数据取出
        need_find_name = []
        need_find_name.append("用例编号(不可为空,不可重复)")
        need_find_name.append("url")
        need_find_name.append("请求方式")
        need_find_name.append("header请求头")
        need_find_name.append("param请求参数")
        need_find_name.append("入参数据")
        need_find_name.append("cookie")
        need_find_name.append("断言")
        need_find_name.append("列表断言")
        # 返参数据取出
        need_find_name.append("返回参数")
        need_find_name.append("断言的获取值")
        need_find_name.append("断言结果")
        need_find_name.append("列表断言结果")
        need_find_name.append("列表断言获取值")

        # 找出当前用例中入参所对应的列
        sheet_test_col = {}
        for k in need_find_name:
            for i in Excel_list_row:
                if i.first_line_name == k:
                    sheet_test_col[k] = i.col

        return sheet_test_col