从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报告:

相关推荐
wengqidaifeng1 小时前
C++从菜鸟到强手:1.基础入门
开发语言·c++
我喜欢山,也喜欢海1 小时前
Java和go在并发上的表现为什么不一样
java·python·golang
hhb_6181 小时前
PHP 8.x 核心特性与工程化开发实践指南
开发语言·php
geovindu2 小时前
go: Flyweight Pattern
开发语言·设计模式·golang·享元模式
Wenzar_2 小时前
**零信任架构下的微服务权限控制:用Go实现基于JWT的动态访问策略**在现代云原生环境中,
java·python·微服务·云原生·架构
不是起点的终点2 小时前
【实战】Python 一键生成数据库说明文档(对接阿里云百炼 AI,输出 Word 格式)
数据库·python·阿里云
xyq20243 小时前
TypeScript中的String类型详解
开发语言
2301_813599554 小时前
Go语言怎么做秒杀系统_Go语言秒杀系统实战教程【实用】
jvm·数据库·python
--fancy8 小时前
股票预测情感分析研究案例分析
python
shughui8 小时前
PyCharm 完整教程(旧版本卸载+旧/新版本下载安装+基础使用,2026最新版附安装包)
ide·python·pycharm