예제 #1
0
    def go_on_run(self):
        res = None
        pass_count = []
        fail_count = []
        no_run_count = []
        rows_count = self.data.get_case_lines()

        # 每次执行用例之前将log日志文件清空数据
        log_file = '../log/log.txt'
        with open(log_file, 'w') as f:
            f.seek(0, 0)  # 加上f.seek(0),把文件定位到position 0;没有这句的话,文件是定位到数据最后,truncate也是从最后这里删除
            f.truncate()  # 清空数据

        # 循环执行每行用例
        for i in range(1, rows_count):
            try:
                is_run = self.data.get_is_run(i)
                if is_run:
                    url = self.data.get_request_url(i)
                    method = self.data.get_request_method(i)
                    # 获取请求参数
                    data = self.data.get_data_value(i)
                    # 获取excel文件中header关键字
                    header_key = self.data.get_request_header(i)
                    # 获取json文件中header_key对应的头文件数据
                    header = self.data.get_header_value(i)
                    expect = self.data.get_expect_data(i)
                    depend_case = self.data.is_depend(i)

                    if depend_case != None:
                        self.depend_data = DependentData(depend_case)
                        # 获取依赖字段的响应数据
                        depend_response_data = self.depend_data.get_value_for_key(i)
                        # 获取请求依赖的key
                        depend_key = self.data.get_depend_field(i)
                        # 将依赖case的响应返回中某个字段的value赋值给该接口请求中某个参数
                        data[depend_key] = depend_response_data

                    # cookie相关的没有跑通,代码逻辑是正常的,但是模拟登陆返回一直是非法请求
                    if header_key == 'write_Cookies':
                        res = self.run_method.run_main(method, url, data, header, params=data)
                        op_header = OperationHeader(res)
                        op_header.write_cookie()

                    elif header_key == 'get_Cookies':
                        op_json = OperationJson('../dataconfig/cookie.json')
                        cookie = op_json.get_data('apsid')
                        cookies = {'apsid': cookie}
                        res = self.run_method.run_main(method, url, data, header=cookies, params=data)

                    else:
                        res = self.run_method.run_main(method, url, data, header, params=data)

                    '''
                    get请求参数是params:request.get(url='',params={}),post请求数据是data:request.post(url='',data={})
                    excel文件中没有区分直接用请求数据表示,则data = self.data.get_data_value(i)拿到的数据,post请求就是data=data,get请就是params=data
                    '''

                    # excel中拿到的expect数据是str类型,但是返回的res是dict类型,两者数据比较必须都是字符类型
                    if self.com_util.is_contain(expect, json.dumps(res)):
                        self.data.write_result(i, 'pass')
                        pass_count.append(i)
                    else:
                        # 返回的res是dict类型,要将res数据写入excel中,需将dict类型转换成str类型
                        self.data.write_result(i, json.dumps(res))
                        with open(log_file, 'a', encoding='utf-8') as f:
                            f.write("\n第%s条用例实际结果与预期结果不一致:\n" % i)
                            f.write("Expected:%s\n  Actual:%s\n" % (expect, res))
                        fail_count.append(i)

                else:
                    no_run_count.append(i)

            except Exception as e:
                # 将异常写入excel的测试结果中
                self.data.write_result(i, str(e))
                # 将报错写入指定路径的日志文件里
                with open(log_file, 'a', encoding='utf-8') as f:
                    f.write("\n第%s条用例报错:\n" % i)
                initLogging(log_file, e)
                fail_count.append(i)

        self.send_mail.send_main(pass_count, fail_count, no_run_count)
예제 #2
0
    def base_run(self):
        res = None
        pass_count = []
        fail_count = []
        no_run_count = []
        rows_count = self.data.get_case_rows()

        log_file = "../log/interface-demo03.log"  # 每次执行用例前,将log日志文件清空数据
        with open(log_file, "w") as lf:
            lf.seek(
                0, 0
            )  # 加上lf.seek(0),把文件定位到position 0;若没有这句话,文件是定位到数据最后,truncate也是从最后这里删除。
            lf.truncate()

        if self.excel.sheet_name == 0:
            self.tool.write_mobile()  # 向get_mobile.json 中写入手机号码
            # self.__mobile__ = self.tool.phone_code_generator()

        for i in range(1, rows_count):
            try:
                is_run = self.data.get_is_run(i)
                if is_run:
                    url = self.data.get_request_url(i)
                    method = self.data.get_request_method(i)
                    # data = self.data.get_request_data(i)  # 获取excel 中的 data
                    data = self.data.get_request_excel_data(
                        i)  # 直接读取 excel 中的 data

                    # 获取依赖单元格
                    depend_case = self.data.is_depend(i)
                    if depend_case != None:
                        self.depend_data = DependentData(depend_case)
                        depend_response_data = self.depend_data.get_value_for_key(
                            i)  # 获取 依赖字段的 响应数据
                        # depend_key = self.data.get_depend_field(i)  # 获取 请求依赖的 key
                        # data[depend_key] = depend_response_data  # 将依赖case的响应返回中某个字段的value赋值给该接口请求中某个参数

                    if data.find("${mobile}") != -1:
                        # data = data.replace("${mobile}", self.__mobile__)
                        data = data.replace("${mobile}",
                                            self.tool.get_mobile())
                    elif data.find("${idCardNumber}") & data.find(
                            "${name}") != -1:
                        data = data.replace("${idCardNumber}",
                                            self.tool.getRandomIdCard())
                        name = "雨点" + self.tool.num_chinese(
                            self.tool.get_mobile()[-4:])
                        data = data.replace("${name}", name)
                    elif data.find("${businessLicenseNumber}") != -1:
                        data = data.replace("${businessLicenseNumber}",
                                            self.tool.license_no())
                    elif data.find("${write_borrowerCode}") != -1:
                        self.use_mysql.select_borrowercode()
                        use_json = UseJson("../data_config/borrowerCode.json")
                        borrowerCode = use_json.get_data("borrowerCode")
                        data = data.replace("${write_borrowerCode}",
                                            borrowerCode)
                    elif data.find("${borrowerCode}") != -1:
                        use_json = UseJson("../data_config/borrowerCode.json")
                        borrowerCode = use_json.get_data("borrowerCode")
                        data = data.replace("${borrowerCode}", borrowerCode)
                    elif data.find("${creditApplyCode}") != -1:
                        use_json = UseJson(
                            "../data_config/credit_apply_code.json")
                        code = use_json.get_data("code")
                        # code = use_json.read_data()
                        data = data.replace("${creditApplyCode}", code)
                        now = time.strftime("%Y-%m-%d 08:%M:%S")
                        data = data.replace("${nowdate}", now)
                    elif data.find("${nowdate}") != -1:
                        now = time.strftime("%Y-%m-%d 08:%M:%S")
                        data = data.replace("${nowdate}", now)

                    # data = self.data.get_request_excel_data_wrapper(i, self.mobile)  # 直接读取 excel 中的 data
                    # data = self.data.get_data_values(i)
                    # data = self.data.get_data_values_wrapper(i, self.mobile)
                    header = self.data.get_request_header(
                        i)  # 获取 excel 中的 header 关键字
                    # header_json = self.data.get_header_value(i)  # 获取 json 中的 header_key 对应的头文件数据
                    expect_data = self.data.get_expect_data(i)
                    expect = json.loads(expect_data)

                    # # 获取依赖单元格
                    # depend_case = self.data.is_depend(i)
                    # if depend_case != None:
                    #     self.depend_data = DependentData(depend_case)
                    #     depend_response_data = self.depend_data.get_value_for_key(i)  # 获取 依赖字段的 响应数据
                    #     # depend_key = self.data.get_depend_field(i)  # 获取 请求依赖的 key
                    #     # data[depend_key] = depend_response_data  # 将依赖case的响应返回中某个字段的value赋值给该接口请求中某个参数

                    if header == '{"Content-Type":"application/json"}':
                        header1 = eval(header)
                        # header1 = {"Content-Type": "application/json"}
                        # data = json.dumps(data)
                        # res = self.run_method.run_main(method, url, data, header=header_json, params=data)
                        res = self.run_method.run_main(method,
                                                       url,
                                                       data,
                                                       header=header1,
                                                       params=data)
                    elif header == '{"Content-Type": "application/json", "Product": "31G"}':
                        header1 = eval(header)
                        res = self.run_method.run_main(method,
                                                       url,
                                                       data,
                                                       header=header1,
                                                       params=data)
                    elif header == '{"Content-Type":"multipart/form-data"}':
                        # data = {"imageType": "ID_CARD_FRONT_IMAGE"}
                        data = eval(data)  # 把 str 转成 dict
                        with open("H:/wahh.jpg", "rb") as f:  # 打开上传文件
                            r = f.read()
                        files = {"file": ("wahh.jpg", r, "image/jpeg")}
                        use_json = UseJson("../data_config/config_header.json")
                        header_json = use_json.read_data()
                        # header_json = {  # "Content-Type": "multipart/form-data",  # 不要画蛇添足写这一句
                        #     "Cookie": "SESSION=NGExN2Y0MzUtNjVhNy00MDRkLWIyZjItMGFjZWVlMDFiZjM5"}
                        response = requests.post(url=url,
                                                 data=data,
                                                 files=files,
                                                 headers=header_json)  # 获取返回请求
                        res = response.text
                        # res = self.run_method.run_main(method, url, data, header=header_json, params=data)
                    elif header == 'get_cookie':
                        use_json = UseJson("../data_config/config_header.json")
                        cookie_value = use_json.get_data("Cookie")
                        # a = json.dumps(cookie1)
                        # cookie = re.findall(r'{(.*?)}', a)
                        # cookie_value = cookie1['Cookie']
                        header_json = {
                            "Content-Type": "application/json",
                            "Cookie": cookie_value
                        }
                        res = self.run_method.run_main(method,
                                                       url,
                                                       data,
                                                       header=header_json,
                                                       params=data)
                    elif header == 'get_cookie,31G':
                        use_json = UseJson("../data_config/config_header.json")
                        cookie_value = use_json.get_data("Cookie")
                        # a = json.dumps(cookie1)
                        # cookie = re.findall(r'{(.*?)}', a)
                        # cookie_value = cookie1['Cookie']
                        header_json = {
                            "Content-Type": "application/json",
                            "Product": "31G",
                            "Cookie": cookie_value
                        }
                        res = self.run_method.run_main(method,
                                                       url,
                                                       data,
                                                       header=header_json,
                                                       params=data)
                    elif header == "write_Cookie1":
                        # res = self.run_method.run_main(method, url, data, header=header_json, params=data)
                        use_header = UseHeader(res)
                        use_header.write_cookie()
                    elif header == "get_Cookie1":
                        use_json = UseJson("../data_config/cookie.json")
                        cookie = use_json.get_data("apsid")
                        cookies = {"apsid": cookie}
                        res = self.run_method.run_main(method,
                                                       url,
                                                       data=data,
                                                       header=cookies,
                                                       params=data)
                    else:
                        res = self.run_method.run_main(method,
                                                       url,
                                                       data,
                                                       header,
                                                       params=data)

                    if self.tool.is_contain(expect, res):
                        self.data.write_actual(
                            i, json.dumps(res,
                                          ensure_ascii=False))  # 返回值为中文时,不乱码
                        self.data.write_result(i, "PASS")
                        pass_count.append(i)
                    else:
                        self.data.write_actual(
                            i, json.dumps(res, ensure_ascii=False))
                        self.data.write_result(i, "FAIL")
                        with open(log_file, "a", encoding="utf-8") as lf:
                            lf.write("\n第{}条用例实际结果与期望结果不一致:\n".format(i))
                            lf.write("期望结果:{}\n实际结果:{}\n".format(expect, res))
                        fail_count.append(i)
                else:
                    no_run_count.append(i)

            except Exception as e:
                self.data.write_result(i, str(e))
                with open(log_file, "a", encoding="utf-8") as lf:
                    lf.write("\n第%s条用例报错:\n" % i)
                initLogging(log_file, e)
                fail_count.append(i)
예제 #3
0
    def base_run(self):
        res = None
        pass_count = []
        fail_count = []
        no_run_count = []
        rows_count = self.data.get_case_rows()

        log_file = "../log/interface-demo02.log"  # 每次执行用例前,将log日志文件清空数据
        with open(log_file, "w") as lf:
            lf.seek(
                0, 0
            )  # 加上lf.seek(0),把文件定位到position 0;若没有这句话,文件是定位到数据最后,truncate也是从最后这里删除。
            lf.truncate()

        self.tool.write_mobile()  # 向get_mobile.json 中写入手机号码

        for i in range(1, rows_count):
            try:
                is_run = self.data.get_is_run(i)
                if is_run:
                    url = self.data.get_request_url(i)
                    method = self.data.get_request_method(i)
                    data = self.data.get_request_excel_data(
                        i)  # 直接读取 excel 中的 data
                    if data.find("${mobile}") != -1:
                        data = data.replace("${mobile}",
                                            self.tool.get_mobile())
                    elif data.find("${idCardNumber}") & data.find(
                            "${name}") != -1:
                        data = data.replace("${idCardNumber}",
                                            self.tool.getRandomIdCard())
                        name = "雨点" + self.tool.num_chinese(
                            self.tool.get_mobile()[-4:])
                        data = data.replace("${name}", name)
                    elif data.find("${businessLicenseNumber}") != -1:
                        data = data.replace("${businessLicenseNumber}",
                                            self.tool.license_no())

                    header = self.data.get_request_header(
                        i)  # 获取 excel 中的 header 关键字
                    expect_data = self.data.get_expect_data(i)
                    expect = json.loads(expect_data)

                    depend_case = self.data.is_depend(i)

                    if depend_case != None:
                        self.depend_data = DependentData(depend_case)
                        depend_response_data = self.depend_data.get_value_for_key(
                            i)  # 获取 依赖字段的 响应数据

                    if header == '{"Content-Type":"application/json"}':
                        header_json = eval(header)
                        res = self.run_method.run_main(method,
                                                       url,
                                                       data,
                                                       header=header_json,
                                                       params=data)
                    elif header == '{"Content-Type":"multipart/form-data"}':
                        data = eval(data)  # 把 str 转成 dict
                        with open("H:/wahh.jpg", "rb") as f:  # 打开上传文件
                            r = f.read()
                        files = {"file": ("wahh.jpg", r, "image/jpeg")}
                        use_json = UseJson("../data_config/config_header.json")
                        header_json = use_json.read_data()
                        # header_json = {  # "Content-Type": "multipart/form-data",  # 不要画蛇添足写这一句
                        #     "Cookie": "SESSION=NGExN2Y0MzUtNjVhNy00MDRkLWIyZjItMGFjZWVlMDFiZjM5"}
                        response = requests.post(url=url,
                                                 data=data,
                                                 files=files,
                                                 headers=header_json)  # 获取返回请求
                        res = response.text
                    elif header == 'get_cookie':
                        use_json = UseJson("../data_config/config_header.json")
                        cookie_value = use_json.get_data("Cookie")
                        header_json = {
                            "Content-Type": "application/json",
                            "Cookie": cookie_value
                        }
                        res = self.run_method.run_main(method,
                                                       url,
                                                       data,
                                                       header=header_json,
                                                       params=data)
                    elif header == "write_Cookie1":
                        use_header = UseHeader(res)
                        use_header.write_cookie()
                    elif header == "get_Cookie1":
                        use_json = UseJson("../data_config/config_cookie.json")
                        cookie = use_json.get_data("apsid")
                        cookies = {"apsid": cookie}
                        res = self.run_method.run_main(method,
                                                       url,
                                                       data=data,
                                                       header=cookies,
                                                       params=data)
                    else:
                        res = self.run_method.run_main(method,
                                                       url,
                                                       data,
                                                       header,
                                                       params=data)

                    if self.tool.is_contain(expect, res):
                        self.data.write_result(i, "pass")
                        pass_count.append(i)
                    else:
                        self.data.write_result(i, json.dumps(res))
                        with open(log_file, "a", encoding="utf-8") as lf:
                            lf.write("\n第%s条用例实际结果与期望结果不一致:\n" % i)
                            lf.write("期望结果:%s\n实际结果:%s\n" % (expect, res))
                        fail_count.append(i)
                else:
                    no_run_count.append(i)

            except Exception as e:
                self.data.write_result(i, str(e))
                with open(log_file, "a", encoding="utf-8") as lf:
                    lf.write("\n第%s条用例报错:\n" % i)
                initLogging(log_file, e)
                fail_count.append(i)