从0开始python学习-50.pytest之多接口用例封装

  1. yaml用例设计--一个yaml中多个用例,且互相存在关联关系
python 复制代码
- # 第一个用例
  request:
    method: post
    url: http://192.168.0.1:8010/api
    json:
      accounts: admin
      pwd: 123
      type: username
    headers:
      Content-Type: application/json
      
- # 第二个用例
  request:
    method: post
    url: http://192.168.0.1:8010/api/order/index
    headers:
      Content-Type: application/json
  1. 设计多接口用例读取封装
python 复制代码
def read_testcase(yaml_path):
    with open(yaml_path,encoding="utf-8") as f:
        case_list = yaml.safe_load(f)
        if len(case_list) >=2: # list长度大于等于2,则说明是多个接口用例,则yaml读取出来的格式为:[{},{}]
            return [case_list] # 将返回格式修改为[[{},{}]]
        else:
            return case_list # 单接口的用例格式,直接返回即可:[{}]
  1. 将读取caseinfo的方法进行list格式的兼容设计
python 复制代码
def create_testcase(yaml_path):
    @pytest.mark.parametrize('caseinfo', read_testcase(yaml_path))
    def func(self,caseinfo):
        global case_obj
        if isinstance(caseinfo,list): # 多接口用例
            for case in caseinfo:
                case_obj = verify_yaml(case)
                stand_case_flow(case_obj)
        else: # 单接口用例
            # 校验yaml中的数据
            case_obj = verify_yaml(caseinfo)
            # 用例的标准化流程
            stand_case_flow(case_obj)
    return func
相关推荐
databook13 小时前
Manim实现闪光轨迹特效
后端·python·动效
Juchecar14 小时前
解惑:NumPy 中 ndarray.ndim 到底是什么?
python
用户83562907805115 小时前
Python 删除 Excel 工作表中的空白行列
后端·python
Json_15 小时前
使用python-fastApi框架开发一个学校宿舍管理系统-前后端分离项目
后端·python·fastapi
数据智能老司机21 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机1 天前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i1 天前
drf初步梳理
python·django
每日AI新事件1 天前
python的异步函数
python