params实现参数化:
1、如何把值传到fixture?是通过在fixture函数的参数里面加入request来接收参数,然后通过request.param来取值
#读取数据的方法
def read_yaml():
return ['zhangsan','lisi','wangwu']
@pytest.fixture(scope="function",autouse=False,params=read_yaml())
def exe_database_sql(request):
print(request.param)
print("执行SQL查询")
yield
print("关闭数据库连接")
执行结果:
2、如何把值传到函数里面?是通过fixture函数的yield返回值,然后就把这个值传递到测试用例函数中。
@pytest.fixture(scope="function",autouse=False,params=read_yaml())
def exe_database_sql(request):
print("执行SQL查询")
yield request.param
print("关闭数据库连接")
执行结果:
如果参数值是字典,需要在测试用例函数中进行强制转换