pytest之yaml格式测试用例读写封装

pytest之yaml格式测试用例读写封装

pytest之parametrize()实现数据驱动

@pytest.mark.parametrize("参数名",列表数据)

参数名:用来接收每一项数据,并作为测试用例的参数。

列表数据:一组测试数据。

python 复制代码
import pytest

@pytest.mark.parametrize("参数名", [列表数据])
def test_example(参数名):
    # 在这里编写测试用例,可以使用参数名作为测试数据
    pass
  • 第一种用法:列表
python 复制代码
class TestApi(CommonUtil):
    @pytest.mark.parametrize('caseinfo', ['百里','汤姆','一瑶'])
    def test_01_get_token(self,caseinfo):
        print("获取统一接口鉴权码"+caseinfo)
  • 第二种用法:字典列表
python 复制代码
class TestApi(CommonUtil):

    @pytest.mark.parametrize('arg1,arg2', [['name', '百里'], ['age', '18']])
    def test_01_get_token(self, arg1, arg2):
        print("获取统一接口鉴权码" + str(arg1)+ " "+ str(arg2))

YAML格式测试用例读/写/清除/封装

结构类型

python 复制代码
# 读取yaml
import os

import yaml


# 获取项目的根目录
def get_obj_path():
    # 获取当前文件的目录路径,并使用字符串 'common_01' 进行分割,返回分割后的第一个部分,即 'common_01' 之前的路径。
    return os.path.dirname(__file__).split('common_01')[0]


def read_yaml(yaml_path):
    # 使用 with 语句打开文件,并使用模式 'r' 以只读方式打开文件
    with open(get_obj_path()+yaml_path, mode='r', encoding='utf-8') as f:
        # 使用 yaml 模块的 load 函数从文件中读取数据,并使用 FullLoader 加载器
        value = yaml.load(stream=f, Loader=yaml.FullLoader)
        # 返回从文件中读取的数据
        return value


if __name__ == '__main__':
    print(read_yaml('testcases/user_manage/get_token.yaml'))

Maps类型

Map为字典,一对key:value键值对

键:(空格)值。

python 复制代码
# 示例 YAML 文件
person:
  name: John
  age: 30
  city: New York

数组类型

使用'-'表示列表。

python 复制代码
# Example YAML file: get_token.yaml
tokens:
  - token1
  - token2
  - token3

pytest+parametrize+yaml

test_api.py

python 复制代码
import pytest
import requests

from common_01.common_util import CommonUtil
from common_01.yaml_util import read_yaml


class TestApi(CommonUtil):
    session = requests.session()

    # 定义一个参数化测试函数
    @pytest.mark.parametrize('caseinfo', read_yaml('testcases/user_manage/get_token.yaml'))
    def test_01_get_token(self, caseinfo):
        # 打印测试用例信息
        print("获取统一接口鉴权码: ")
        print(caseinfo)
        # 打印请求信息
        name = caseinfo['name']
        method = caseinfo['request']['method']
        url = caseinfo['request']['url']
        data = caseinfo['request']['data']
        print(caseinfo['validate'])
        # 发送 HTTP 请求
        res = self.session.request(method=method, url=url, params=data)
        # 打印返回结果
        print(res.json())
        #断言
        result =res.json()
        assert 'access_token' in result

get_token.yaml

python 复制代码
-
  name: 获取统一的接口鉴权
  request:
    method: get
    url: https://api.weixin.qq.com/cgi-bin/token
    data:
      grant_type: client_credential
      appid: wx6b11b3efd1cdc290
      secret: 106a9c6157c4db5f6029918738f9529d
  validate: None
相关推荐
(:满天星:)8 小时前
第31篇:块设备与字符设备管理深度解析(基于OpenEuler 24.03)
linux·运维·服务器·网络·centos
小陶来咯8 小时前
【仿muduo库实现并发服务器】Acceptor模块
运维·服务器
爱莉希雅&&&8 小时前
shell编程之awk命令详解
linux·服务器·git
笑稀了的野生俊8 小时前
在服务器中下载 HuggingFace 模型:终极指南
linux·服务器·python·bash·gpu算力
小扎仙森10 小时前
关于服务器宝塔转移wordperss子比主题问题
运维·服务器
小小小糖果人10 小时前
Linux云计算基础篇(5)
linux·运维·服务器
KENYCHEN奉孝10 小时前
Rust征服字节跳动:高并发服务器实战
服务器·开发语言·rust
开开心心就好11 小时前
免费PDF处理软件,支持多种操作
运维·服务器·前端·spring boot·智能手机·pdf·电脑
IC 见路不走12 小时前
LeetCode 第91题:解码方法
linux·运维·服务器
汀沿河12 小时前
8.1 prefix Tunning与Prompt Tunning模型微调方法
linux·运维·服务器·人工智能