如果用例需要用到多个fixture的返回数据,fixture也可以return一个元组、list或字典,然后从里面取出对应数据。
import pytest
@pytest.fixture()
def user():
print("获取用户名")
a = "hjt"
b = "123456"
return (a, b)
def test_1(user):
u = user[0]
p = user[1]
print("测试账号:%s, 密码:%s" % (u, p))
assert u == "hjt"
分开定义成多个fixture,然后test_用例传多个fixture参数
@pytest.fixture()
def user():
print("获取用户名")
a = "hjt"
return a
@pytest.fixture()
def psw():
print("获取密码")
b = "123456"
return b
def test_1(user, psw):
‘‘‘传多个fixture‘‘‘
print("测试账号:%s, 密码:%s" % (user, psw))
assert user == "hjt"
多个fixture和parametrize组合使用
# 测试账号数据
test_user = ["admin1", "admin2"]
test_psw = ["11111", "22222"]
@pytest.fixture(scope="module")
def input_user(request):
user = request.param
print("登录账户:%s" % user)
return user
@pytest.fixture(scope="module")
def input_psw(request):
psw = request.param
print("登录密码:%s" % psw)
return psw
@pytest.mark.parametrize("input_user", test_user, indirect=True)
@pytest.mark.parametrize("input_psw", test_psw, indirect=True)
def test_login(input_user, input_psw):
"""登录用例"""
a = input_user
b = input_psw
print("测试数据a-> %s, b-> %s" % (a, b))
assert b
fixture和fixture互相调用
@pytest.fixture()
def first():
print("获取用户名")
a = "hjt"
return a
@pytest.fixture()
def sencond(first):
‘‘‘psw调用user fixture‘‘‘
a = first
b = "123456"
return (a, b)
def test_1(sencond):
‘‘‘用例传fixture‘‘‘
print("测试账号:%s, 密码:%s" % (sencond[0], sencond[1]))
assert sencond[0] == "hjt"
原文:https://www.cnblogs.com/juntaohuang/p/14496462.html