fixture的name以及params参数

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值