Pytest进阶 -- fixture 参数化

Fixture 在自动化中的应用 -参数化

 

场景:

测试离不开数据,为了数据灵活,⼀般数据都是通过参数传的

解决:

fixture 通过固定参数 request 传递

步骤:

在 fixture 中增加@pytest.fixture(params=[1, 2, 3, ‘linda’])

在⽅法参数写 request,方法体里面使用 request.param 接收参数

 

案例:

conftest.py

@pytest.fixture(params=[[1,2],"app","web"]) #定义一个带参数的fixture方法
def para(request):
    print(f"output:{request.param}") #只能使用request.param来返回数据
    yield request.param    #第一次返回[1,2],第二次["app"]以此类推

test_demo.py

def test_demo_01(para):

    if type(para) == list:
        print("demo para is list",para[0])
    else:
        print(f"demo1 case return:{para}")

输出:

output:[1, 2]
PASSED                                 [ 33%]demo para is list 1
output:app
PASSED                                   [ 66%]demo1 case return:app
output:web
PASSED                                   [100%]demo1 case return:web

 

posted @   lms21  阅读(252)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示