pytest的fixture

fixture的作用类似于setup/teardown。
fixture命名不以test开头,用于区分用例。
在函数前使用装饰器@pytest.fixture(),括号中有个scope的参数,用于控制fixture的范围。默认取值为function,控制范围的排序为:session > module > class > function。取值范围如下所示:
在这里插入图片描述
代码如下:定义了一个init方法,作为参数传入test_1和test_02函数,在test_1函数中对init的返回值做了断言

import pytest


@pytest.fixture()
def init():
    print('init...')
    return 1


def test_1(init):
    print('test1')
    assert init == 1


def test_2(init):
    print('test2')

输出如下:

======================================================================== test session starts ========================================================================
platform win32 -- Python 3.7.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\python3.7.6\python.exe
cachedir: .pytest_cache
rootdir: D:\pythonProject\my_selenium_project\testcases\pytest, configfile: pytest.ini
collected 2 items                                                                                                                                                    

test_fixture.py::test_1 init...
test1
PASSED
test_fixture.py::test_2 init...
test2
PASSED

========================================================================= 2 passed in 0.03s =========================================================================
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

永远不要矫情

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值