为了提高复用性,我们在写测试用例的时候,会用到不同的fixture,
比如:最常见的登录操作,大部分的用例的前置条件都是登录
假设不同的用例想登录不同的测试账号,
那么登录fixture就不能把账号写死,
需要通过传参的方式来完成登录操作
#coding=utf-8
import pytest
#fixture传单个参数
@pytest.fixture()
def login(request):
name=request.param #request.param就代表参数化的data里的每一个参数
#print("账号是:{}".format(name))
return name
data=["vince","jerry"]
@pytest.mark.parametrize(
'login',
data,
indirect=True
)
def test_01(login):
print("测试账号是:{}".format(login))
if __name__ == '__main__':
pytest.main(["-s","test_param2.py"])
"""
注:
indirect=True 表示将参数'login'当做一个函数去执行(即参数名要和定义的fixture的函数名一致),而不是一个参数变量,并且将data当做参数传递给该函数
def test_01(login) ,这里的login是获取fixture返回的值
结果:
test_param2.py 测试账号是:vince
.测试账号是:jerry
.
"""
import pytest
@pytest.fixture()
def fixture_pre_post(request):
print("打开浏览器")
print(request.param)
yield
print("关闭浏览器")
class TestFeature():
@pytest.mark.parametrize('fixture_pre_post', ["aaaaa", ], indirect=True)
def test_01(self, fixture_pre_post):
print("test_01")
def test_02(self):
print("test_02")
#######运行结果########
============================= test session starts =============================
collecting ... collected 2 items
test_feature.py::TestFeature::test_01[aaaaa] 打开浏览器
aaaaa
PASSED [ 50%]test_01
关闭浏览器
test_feature.py::TestFeature::test_02 PASSED [100%]test_02
============================== 2 passed in 0.01s ==============================
fixture传多个参数
#coding=utf-8
import pytest
@pytest.fixture()
def logins(request):
param=request.param
#print("账号是:{}".format(name))
return param
data=[{"username":"user1","passwd":"123"},
{"username":"user2","passwd":"456"}
]
@pytest.mark.parametrize(
'logins',
data,
indirect=True
)
def test_01(logins):
print("测试用户名是:{0}———测试密码是:{1}".format(logins["username"],logins["passwd"]))
if __name__ == '__main__':
pytest.main(["-s","test_param3.py"])
"""
结果:
test_param3.py 测试用户名是:user1———测试密码是:123
.测试用户名是:user2———测试密码是:456
.
"""
多个fixtur只加一个装饰器
#coding=utf-8
import pytest
@pytest.fixture()
def input_user(request):
user=request.param
return user
@pytest.fixture()
def input_pwd(request):
pwd=request.param
return pwd
data=[("user1","123"),
("user2","456")
]
@pytest.mark.parametrize(
'input_user,input_pwd',
data,
indirect=True
)
def test_01(input_user,input_pwd):
print("测试用户名是:{0}———测试密码是:{1}".format(input_user,input_pwd))
if __name__ == '__main__':
pytest.main(["-s","test_param4.py"])
"""
结果:
test_param4.py 测试用户名是:user1———测试密码是:123
.测试用户名是:user2———测试密码是:456
.
"""
多个fixtur叠加装饰器
#coding=utf-8
import pytest
@pytest.fixture()
def input_user(request):
user=request.param
return user
@pytest.fixture()
def input_pwd(request):
pwd=request.param
return pwd
data=[("user1","123"),
("user2","456")
]
@pytest.mark.parametrize('input_user',data,indirect=True)
@pytest.mark.parametrize('input_pwd',data,indirect=True)
def test_01(input_user,input_pwd):
print("测试用户名是:{0}———测试密码是:{1}".format(input_user,input_pwd))
if __name__ == '__main__':
pytest.main(["-s","test_param5.py"])
"""
结果:
test_param5.py 测试用户名是:('user1', '123')———测试密码是:('user1', '123')
.测试用户名是:('user2', '456')———测试密码是:('user1', '123')
.测试用户名是:('user1', '123')———测试密码是:('user2', '456')
.测试用户名是:('user2', '456')———测试密码是:('user2', '456')
.
"""