从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
相关推荐
秋邱几秒前
AR + 离线 AI 实战:YOLOv9+TensorFlow Lite 实现移动端垃圾分类识别
开发语言·前端·数据库·人工智能·python·html
GesLuck2 分钟前
Function函数
开发语言·物联网
程序员杰哥3 分钟前
UI自动化测试框架:PO 模式+数据驱动
自动化测试·软件测试·python·selenium·测试工具·ui·测试用例
Swift社区11 分钟前
在 Swift 中使用 Image Playground 生成 AI 图像:完整实战指南
开发语言·人工智能·swift
走在路上的菜鸟12 分钟前
Android学Dart学习笔记第十一节 分支
android·笔记·学习·flutter
AI视觉网奇15 分钟前
视频选帧截取
python·opencv·音视频
liu****20 分钟前
11.字符函数和字符串函数(一)
linux·运维·c语言·开发语言·数据结构·算法
hmbbcsm21 分钟前
练习python题目小记(七)
开发语言·python
aini_lovee22 分钟前
基于UERD算法的JPEG图像隐写MATLAB实现
开发语言·算法·matlab
郑州光合科技余经理29 分钟前
技术架构:跑腿配送系统海外版源码全解析
java·开发语言·前端·数据库·架构·uni-app·php