1、name参数
# conftest.py
@pytest.fixture(scope="function",name='alias') # name参数即给这个fixture设置别名
def user():
user='aaa'
return user
# test_a.py
# 验证设置fixture别名,我们将别名传入用例即可。
def test_alias(alias):
print(alias) # 打印上面fixture的返回值
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-4.5.0, py-1.8.0, pluggy-0.13.1
rootdir: D:\code\py_dev_test\pytest框架\pytest_base\fixture登录
plugins: allure-pytest-2.8.6, base-url-1.4.2, html-1.19.0, metadata-1.8.0collected 1 item
test_a.py .aaa
[100%]
(2)params参数
import pytest
'''request是pytest内置的fixture'''
'''test_fixture的request内置fixture以及在fixture参数化'''
# 举例:登录不同的账号
@pytest.fixture(scope="function",params=['test01','test02']) # 使用params传参,参数列表为一个可迭代对象
def login(request):
user=request.param # 使用内置request接收参数, request为函数对象,使用request.param接收参数
print('用户%s分别先后登录'%user)
return user
def test_a(login):
print('%s登录成功'%login)
Testing started at 21:43 ...
D:\python36\python.exe "D:\pycharm\PyCharm Community Edition 2018.2.1\helpers\pycharm\_jb_pytest_runner.py" --path D:/code/py_dev_test/pytest框架/pytest_base/fixture登录/test_fixture_params.py
Launching pytest with arguments D:/code/py_dev_test/pytest框架/pytest_base/fixture登录/test_fixture_params.py in D:\code\py_dev_test\pytest框架\pytest_base\fixture登录
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-4.5.0, py-1.8.0, pluggy-0.13.1
rootdir: D:\code\py_dev_test\pytest框架\pytest_base\fixture登录
plugins: allure-pytest-2.8.6, base-url-1.4.2, html-1.19.0, metadata-1.8.0collected 2 items
test_fixture_params.py 用户test01分别先后登录
.test01登录成功
用户test02分别先后登录
.test02登录成功
[100%]
========================== 2 passed in 0.03 seconds ===========================
Process finished with exit code 0