python+requests+pytest 接口自动化框架(六)

目录

一、规范YAML测试用例

1、一级关键字必须要包含:name,request,validate

2、在request下必需包含:method,url

3、传参方式:

二、接口关联改进(直接在YAML文件重就可以实现)

今日内容:接口自动化框架封装之规范YAML测试用例&接口关联封装改进&基础路径封装改进

源码: python+requests+pytest接口自动化框架(6)-Python文档类资源-CSDN下载

一、规范YAML测试用例

1、一级关键字必须要包含:name,request,validate

2、在request下必需包含:method,url

3、传参方式:

  • get请求,那么必须通过params传参
  • post请求 传json格式,需要使用json传参 传表单格式,需要使用data传参
  • 文件上传:使用files传参
    # 规范YAML测试用例
    def standard_yaml(self, caseinfo):
        caseinfo_key = caseinfo.keys()
        # 判断一级关键字是否包括有:name,request,valiedate
        if "name" in caseinfo_key and "request" in caseinfo_key and "validate" in caseinfo_key:
            # 判断request下面是否包含:method,url
            request_keys = caseinfo['request'].keys()
            if "method" in request_keys and "url" in request_keys:
                print("YAML基本架构检查通过")
                # pop() 方法删除字典给定键 key 及对应的值
                name = caseinfo.pop("name")
                method = caseinfo['request'].pop("method")
                url = caseinfo['request'].pop("url")
                # 发送请求
                res = self.send_request(method, url, **caseinfo['request'])
                return res
            else:
                print("在request下必需包含:method,url")
        else:
            print("一级关键字必须要包含:name,request,validate")

二、接口关联改进(直接在YAML文件重就可以实现)

多种提取方式(提取多个值):

正则表达式和jsonpath表达式提取

extract:

access_token:'”access_token”:”(.*?)”‘ #正则

expires_in:$.expires_in #jsonpath

if "extract" in caseinfo.keys():
    for key, value in caseinfo["extract"].items():
        if "(.*?)" in value or "(.+?)" in value: #正则表达式
            zz_value = re.search(value, result_text)
            if zz_value:
                extract_value = {key: zz_value.group(1)}
                write_extract_yaml(extract_value)
        else:  # jsonpath
            try:
                resturn_json = res.json()
                js_value = jsonpath.jsonpath(resturn_json, value)
                if js_value:
                    extract_value = {key: js_value[0]}
                    write_extract_yaml(extract_value)
            except Exception as e:
                print("extract返回的结果不是JSON格式,不能使用jsonpath提取")

取值: ${access_token}

python+requests+pytest 接口自动化框架(六)
    # 替换值的方法
    # Qa1(替换url,params,data,jsom,headers)
    # Qa2(string,int,float,list,dict)
    def replace_value(self, data):
        if data:
            # 保存数据类型
            data_type = type(data)
            # 判断数据类型转换成str
            if isinstance(data, dict) or isinstance(data, list):
                str_data = json.dumps(data)
            else:
                str_data = str(data)
            for cs in range(1, str_data.count('${') + 1):
                # 替换
                if "${" in str_data and "}" in str_data:
                    start_index = str_data.index("${")
                    end_index = str_data.index("}", start_index)
                    old_value = str_data[start_index:end_index + 1]
                    new_value = read_extract_yaml(old_value[2:-1])
                    str_data  = str_data.replace(old_value,new_value)
            # 还原数据类型
            if isinstance(data, dict) or isinstance(data, list):
                data = json.loads(str_data)
            else:
                data = data_type(str_data)
        return data
    # 统一请求封装
    def send_request(self, method, url, **kwargs):
        # 基础路径拼接
        url = self.base_url + self.replace_value(url)
        #请求头和参数替换
        for key,value in kwargs.items():
            if key in ['params','data','json','headers']:
                kwargs[key]=self.replace_value(value)
            elif key=="files":
                for file_key,file_path in value.items():
                    value[file_key]=open(file_path,'rb')

        #请求
        rep = RequestsUtil.session.request(method, url, **kwargs)
        print(rep.text)
        return rep

python+requests+pytest 接口自动化框架(六)

python+requests+pytest 接口自动化框架(六)

Original: https://blog.csdn.net/weixin_41121249/article/details/124491525
Author: 天草柑橘
Title: python+requests+pytest 接口自动化框架(六)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/773408/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球