1、前言
为了提高复用性,我们在写测试用例的时候,会用到不同的fixture。比如:最常见的登录操作,大部分的用例的前置条件都是登录 假设不同的用例登录的账号是不一样的,那么我们要如何向fixture传参,完成使用不同账号登录的操作
2、案例一:传单个参数
indirect=True
参数是可以把下方代码中的login当成函数去执行,而不是一个参数,并且将data当作参数传入函数代码import pytest
@pytest. fixture ( )
def login ( request) :
name = request. param
print ( f"===账号是: { name} ===" )
return name
data = [ "test01" , "test02" , "test_03" ]
ids = [ f"login_test_name: { name} " for name in data]
@pytest. mark. parametrize ( "login" , data, ids= ids, indirect= True )
def test_name ( login) :
print ( f"测试用例登录的账号是: { login} " )
结果collecting .. . collected 3 items
test_01.py::test_name[ login_test_name: test01]
== = 账号是: test01 == =
PASSED [ 33 %]
测试用例登录的账号是: test01
test_01.py::test_name[ login_test_name: test02]
== = 账号是: test02 == =
PASSED [ 66 %]
测试用例登录的账号是: test02
test_01.py::test_name[ login_test_name: test_03]
== = 账号是: test_03 == =
PASSED [ 100 %]
测试用例登录的账号是: test_03
3、案例二:传入多个参数
代码@pytest. fixture ( )
def login2 ( request) :
param = request. param
print ( f"账号是: { param[ 'username' ] } ,密码是: { param[ 'passwd' ] } " )
return param
data2 = [
{ "username" : "test01" , "passwd" : "123456" } ,
{ "username" : "test02" , "passwd" : "123456" }
]
@pytest. mark. parametrize ( "login2" , data2, indirect= True )
def test_name_pwd ( login2) :
print ( f"账号密码为: { login2} " )
结果collecting .. . collected 2 items
test_01.py::test_name_pwd[ login20]
账号是: test01,密码是: 123456
PASSED [ 50 %]
账号密码为: { 'username' : 'test01' , 'passwd' : '123456' }
test_01.py::test_name_pwd[ login21]
账号是: test02,密码是: 123456
PASSED [ 100 %]
账号密码为: { 'username' : 'test02' , 'passwd' : '123456' }
3、案例三:多个fixture(只加一个装饰器)
代码@pytest. fixture ( )
def input_user ( request) :
user = request. param
print ( f"登录账号: { user} " )
return user
@pytest. fixture ( )
def input_passwd ( request) :
passwd = request. param
print ( f"登录密码: { passwd} " )
return passwd
data4 = [
( "test01" , "123456" ) ,
( "test02" , "123456" )
]
@pytest. mark. parametrize ( "input_user, input_passwd" , data4, indirect= True )
def test_more_fixture ( input_user, input_passwd) :
print ( f"fixture返回内容: { input_user} , { input_passwd} " )
结果collecting .. . collected 2 items
test_01.py::test_more_fixture[ test01-123456]
登录账号: test01
登录密码: 123456
PASSED [ 50 %]
fixture返回内容: test01, 123456
test_01.py::test_more_fixture[ test02-123456]
登录账号: test02
登录密码: 123456
PASSED [ 100 %]
fixture返回内容: test02, 123456
4、案例四:多个fixture(叠加装饰器)
代码@pytest. fixture ( )
def input_passwd ( request) :
passwd = request. param
print ( f"登录密码: { passwd} " )
return passwd
name = [ "test01" , "test02" ]
passwd = [ "123456" , "123qwe" ]
@pytest. mark. parametrize ( "input_user" , name, indirect= True )
@pytest. mark. parametrize ( "input_passwd" , passwd, indirect= True )
def test_more_fixture ( input_user, input_passwd) :
print ( f"fixture返回内容: { input_user} , { input_passwd} " )
结果:总共2x2 = 4条用例collecting .. . collected 4 items
test_01.py::test_more_fixture[ 123456 -test01]
登录账号: test01
登录密码: 123456
PASSED [ 25 %]
fixture返回内容: test01, 123456
test_01.py::test_more_fixture[ 123456 -test02]
登录账号: test02
登录密码: 123456
PASSED [ 50 %]
fixture返回内容: test02, 123456
test_01.py::test_more_fixture[ 123qwe-test01]
登录账号: test01
登录密码: 123qwe
PASSED [ 75 %]
fixture返回内容: test01, 123qwe
test_01.py::test_more_fixture[ 123qwe-test02]
登录账号: test02
登录密码: 123qwe
PASSED [ 100 %]
fixture返回内容: test02, 123qwe
参考文章:https://www.cnblogs.com/poloyy/p/12685948.html