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
相关推荐
Sinclair5 小时前
简单几步,安卓手机秒变服务器,安装 CMS 程序
android·服务器
Rockbean1 天前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek
茶杯梦轩1 天前
CompletableFuture 在 项目实战 中 创建异步任务 的核心优势及使用场景
服务器·后端·面试
海天鹰2 天前
【免费】PHP主机=域名+解析+主机
服务器
不是二师兄的八戒2 天前
Linux服务器挂载OSS存储的完整实践指南
linux·运维·服务器
芝士雪豹只抽瑞克五2 天前
Nginx 高性能Web服务器笔记
服务器·nginx
失重外太空啦2 天前
Tomcat
java·服务器·tomcat
Henry Zhu1232 天前
数据库:并发控制基本概念
服务器·数据库
茶杯梦轩2 天前
从零起步学习并发编程 || 第九章:Future 类详解及CompletableFuture 类在项目实战中的应用
服务器·后端·面试
ZeroNews内网穿透2 天前
谷歌封杀OpenClaw背后:本地部署或是出路
运维·服务器·数据库·安全