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 @ 2022-05-14 10:42  lms21  阅读(262)  评论(0)    收藏  举报