#使用多个fixture
#如果用例需要用到多个fixture的返回数据,fixture也可以return一个元组、list或字典,然后从里面取出对应数据。
import pytest
@pytest.fixture()
def user():
print("获取用户名")
a ="nuo"
b="123456"
return (a,b)
def test_1(user):
u=user[0]
p=user[1]
print("测试账号:%s,密码“%s"%(u,p))
assert u =="nuo"
if __name__ == '__main__':
pytest.main(["-s","fixtures.py"])
#
# #当然也可以分开定义成多个fixture,然后test_用例传多个fixture参数
import pytest
@pytest.fixture()
def user():
print("获取用户名")
a="nuo"
return a
@pytest.fixture()
def psw():
print("获取密码")
b="123456"
return b
def test_1(user,psw):
'''传多个fixture'''
print("测试账号:%s,测试密码:%s"%(user,psw))
assert user == "nuo"
if __name__ == '__main__':
pytest.main(["-s","fixtures.py"])
# #fixture与fixture互相调用
@pytest.fixture()
def first():
print("获取用户名")
a ="nuo"
return a
@pytest.fixture()
def second(first):
'''pswd调用user fixture'''
a=first
b="1234556"
return (a,b)
def test_1(second):
'''用例传fixture'''
print("测试账号:%s,密码:%s"%(second[0],second[1]))
assert second[0]=="nuo"
if __name__ == '__main__':
pytest.main(["-s","fixtures.py"])
'''fixture的scope=session 跨.py文件调用'''
def test_2(first):
'''用例传fixture'''
print("测试账号:%s"%first)
assert first == "nuo"
if __name__ == '__main__':
pytest.main(["-s","fixtures.py"])