从0开始python学习-38.pytest的parametrize结合yaml实现数据驱动

目录

[1. 数据驱动](#1. 数据驱动)

[2. 定制化allure报告](#2. 定制化allure报告)


1. 数据驱动

yaml文件中写入

python 复制代码
-
  data: 1
  ces: 2

- test:
  data: 3

数据驱动:@pytest.mark.parametrize("参数名","参数值(可以是list或tuple)")

注意:这里参数值只能是列表或者元组,由于yaml的特性,最好写成列表,如果写成了字典则无法读取

python 复制代码
@pytest.mark.parametrize("caseinfo",YamlUtil("./common/test_api.yaml").read_yaml())
def test_b(self,caseinfo):
    print(caseinfo)
    print(caseinfo["data"])

打印结果:

注意:这里有多个case的情况,每个case下的key值最好一致

2. 定制化allure报告

python 复制代码
-
  title: 用例1

- test:
  title: 用例2

定制化title,定制其他的同理即可

python 复制代码
@allure.epic('测试报告')
@allure.feature('测试模块')
@pytest.mark.parametrize("caseinfo",YamlUtil("./common/test_api.yaml").read_yaml())
def test_b(self,caseinfo):
    allure.dynamic.title(caseinfo["title"])
    print(caseinfo["data"])

allure报告:

相关推荐
宸津-代码粉碎机14 分钟前
微服务线上踩坑复盘:接口超时、负载倾斜隐形问题根治方案(生产级配置)
java·大数据·人工智能·python·spring
速易达网络22 分钟前
Python + Tkinter 实现基于 TCP/IP 的局域网聊天工具
python·信息与通信
多多鼠23 分钟前
System Prompt 的“版本漂移”问题:从变更管理到 A/B 测试体系
开发语言·网络·人工智能·python·langchain
昔我往昔29 分钟前
pytest日志问题排查记录
python·pytest
WiChP30 分钟前
【V0.1B16】从零开始的2D游戏引擎开发之路
开发语言·算法·游戏引擎
Python自动化直播38 分钟前
用 Python 爬虫给 AI 直播间做数据反馈机制
人工智能·python·ai·直播
传奇开心果编程1 小时前
【Go入门练中学】第2课:条件与循环
后端·学习·golang
小小de风呀1 小时前
de风——【从零开始学C++】(二十)红黑树封装map和set
c++·学习
孙启超1 小时前
【AI开发之Rust】第 8 课:泛型、trait 与 trait 对象
开发语言·后端·rust
努力学习的代码小白1 小时前
源码学习04
python