当前位置: 代码迷 >> 综合 >> 17.pytest_ fixture修饰的函数作为参数
  详细解决方案

17.pytest_ fixture修饰的函数作为参数

热度:33   发布时间:2023-09-19 01:31:57.0

fixture修饰的函数作为参数

  1. 如果用例需要用到多个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的返回值;

  1. 当然也可以分开定义成多个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互相调用

fixturefixture直接也能互相调用的

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"])