fixture传参的request的使用
- 为了提高复用性,我们在写测试用例的时候,会用到不同的fixture,比如:最常见的登录操作,大部分的用例的前置条件都是登录
- 假设不同的用例想登录不同的测试账号,那么登录fixture就不能把账号写死,需要通过传参的方式来完成登录操作
传入单个参数
import pytest
@pytest.fixture()
def login(request):
name = request.param
print(f'账号是{name}')
return name
data = ["test1","test2"]
ids = [f'login_test_name is {name}' for name in data]
@pytest.mark.parametrize('login',data,ids=ids,indirect=True)
def test_name(login):
print(f'测试用例的登录账号是{login}')
注:
- 添加 indirect=True 参数是为了把 login 当成一个函数去执行,而不是一个参数,并且将data当做参数传入函数
- def test_name(login) ,这里的login是获取fixture返回的值
传入多个参数
import pytest
@pytest.fixture()
def logins(request):
param = request.param
print(f"账号是:{param['username']},密码是:{param['pwd']}")
return param
data = [
{"username": "name1", "pwd": "pwd1"},
{"username": "name2", "pwd": "pwd2"},
]
@pytest.mark.parametrize("logins", data, indirect=True)
def test_name_pwd(logins):
print(f"账号是:{logins['username']},密码是:{logins['pwd']}")
注:
如果需要传多个参数,需要通过字典去传
多个fixture(只加一个装饰器)----常用的
@pytest.fixture(scope='module')
def input_user(request):
user = request.param
print(f'账号是{user}')
return user
@pytest.fixture(scope='module')
def input_pwd(request):
pwd = request.param
print(f'密码是{pwd}')
return pwd
data = [
("name1", "pwd1"),
("name2", "pwd2")
]
@pytest.mark.parametrize("input_user,input_pwd", data, indirect=True)
def test_case1(input_user,input_pwd):
print(f'账号是{input_user},密码是{input_pwd}')
多个fixture(叠加装饰器)
# 多个fixture
@pytest.fixture(scope="function")
def input_user(request):
user = request.param
print("登录账户:%s" % user)
return user
@pytest.fixture(scope="function")
def input_psw(request):
psw = request.param
print("登录密码:%s" % psw)
return psw
name = ["name1", "name2"]
pwd = ["pwd1", "pwd2"]
@pytest.mark.parametrize("input_user", name, indirect=True)
@pytest.mark.parametrize("input_psw", pwd, indirect=True)
def test_more_fixture(input_user, input_psw):
print("fixture返回的内容:", input_user, input_psw)