fixture修饰的函数作为参数
- 如果用例需要用到多个
fixture
的返回数据,fixture
也可以return一个元组、list或字典
,然后从里面取出对应数据。
import pytest
#使用fixture的返回值,返回元祖
@pytest.fixture()
def user():u = "wwj"pwd = "1234"return (u,pwd)def test_01(user):u1 = user[0]p1 = user[1]print(f"用户是:{u1},密码是:{p1}")assert u1 == "wwj"if __name__ == '__main__':pytest.main(["-s","test_fix02.py"])
上面被fixture
修饰的user函数名作为参数给test_01使用,
可以直接使用user
的返回值;
- 当然也可以分开定义成多个
fixture
,然后test_用例传多个fixture
参数
mport pytest
@pytest.fixture()
def user():return ("u1","u2","u3")@pytest.fixture()
def pwd():return ("p1","p2","p3")
def test_01(user,pwd):for i in user:for j in pwd:print(f"用户名是:{i},密码是{j}")assert "u" in iif __name__ == '__main__':pytest.main(["-s","test_fix03.fix"])
分别将两个被fixture修饰的函数作为参数传入
测试用例
fixture与fixture互相调用
fixture
与fixture
直接也能互相调用的
import pytest
@pytest.fixture()
def user():u = "wwj"return u@pytest.fixture()
#user穿参给pwd
def pwd(user):#此处需要调用user函数u = userp = "12345"#返回user和pwdreturn (u,p)def test_01(pwd):print(f"用户名是:{pwd[0]},密码是:{pwd[1]}")if __name__ == '__main__':pytest.main(["-s","test_fix04.fix"])