pytest.fixture详解

scope分为session、package、module、class、function级别,其中function也是包含测试类的测试方法的,package指在当前contest.py的package下只执行一次,如果想在某个package执行前后做操作,可以在那个package里加contest.py。yield就相当于执行测试函数的位置,详细解释如下:
1.首先新建conftest.py

import pytest


@pytest.fixture(scope='session', autouse=True)
def realize_session():
    print('session之前')
    yield
    print('session之后')


@pytest.fixture(scope='package', autouse=True)
def realize_package():
    print('package之前')
    yield
    print('package之后')


@pytest.fixture(scope='module', autouse=True)
def realize_module():
    print('module之前')
    yield
    print('module之后')


@pytest.fixture(scope='class', autouse=True)
def realize_class():
    print('class之前')
    yield
    print('class之后')


@pytest.fixture(scope='function', autouse=True)
def realize_function():
    print('function之前')
    yield
    print('function之后')


@pytest.fixture(scope='function', name='aa')
def realize_auto_flase():
    print('autouse=false-function之前')
    yield
    print('autouse=false-function之后')

2.然后新建测试文件

import pytest

class Test_pytest_class():

    def test_buy_now(self):
        print('test_buy_now')

    def test_buy_now2(self):
        print('test_buy_now2')

    def test_add_cart(self):
        print('test_add_cart')

    #测试自定义调用fixture函数方式1
    @pytest.mark.usefixtures('aa')
    def test_learn(self):
        print('装饰器方式调用fixtures函数')
    #测试自定义调用fixture函数方式1
    def test_learn2(self,aa):
        print('传参方式调用fixtures函数')

3.接下来看执行结果

test_pytest_class.py::Test_pytest_class::test_buy_now session之前
package之前
module之前
class之前
function之前
PASSED             [ 20%]test_buy_now
function之后

test_pytest_class.py::Test_pytest_class::test_buy_now2 function之前
PASSED            [ 40%]test_buy_now2
function之后

test_pytest_class.py::Test_pytest_class::test_add_cart function之前
PASSED            [ 60%]test_add_cart
function之后

test_pytest_class.py::Test_pytest_class::test_learn function之前
autouse=false-function之前
PASSED               [ 80%]装饰器方式调用fixtures函数
autouse=false-function之后
function之后

test_pytest_class.py::Test_pytest_class::test_learn2 function之前
autouse=false-function之前
PASSED              [100%]传参方式调用fixtures函数
autouse=false-function之后
function之后
class之后
module之后
package之后
session之后
  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pytest.fixture装饰器是pytest测试框架中的一个重要特性,用于定义测试用例中的共享资源或者测试环境的初始化和清理操作。通过使用fixture装饰器,我们可以在测试用例中方便地使用这些共享资源。 fixture装饰器可以应用在函数、类或者模块级别上,它的作用是将被装饰的函数或者方法转变为一个fixture对象。fixture对象可以在测试用例中作为参数进行调用,pytest会自动根据参数名匹配相应的fixture对象,并将其传递给测试用例。 fixture装饰器可以接受一些参数来定制其行为,例如scope参数用于指定fixture的作用域,autouse参数用于指定是否自动使用fixture等。 下面是一些常见的fixture用法: 1. 无参数fixture: ```python import pytest @pytest.fixture def setup(): # 初始化操作 yield # 清理操作 def test_example(setup): # 使用setup fixture assert 1 + 1 == 2 ``` 2. 带参数fixture: ```python import pytest @pytest.fixture def setup(request): # 初始化操作 def teardown(): # 清理操作 request.addfinalizer(teardown) def test_example(setup): # 使用setup fixture assert 1 + 1 == 2 ``` 3. fixture作用域: ```python import pytest @pytest.fixture(scope="module") def setup_module(): # 模块级别的初始化操作 yield # 模块级别的清理操作 @pytest.fixture(scope="function") def setup_function(): # 函数级别的初始化操作 yield # 函数级别的清理操作 def test_example(setup_module, setup_function): # 使用setup_module和setup_function fixture assert 1 + 1 == 2 ``` 通过使用fixture装饰器,我们可以更加灵活地管理测试用例中的共享资源和测试环境的初始化和清理操作,提高测试用例的可维护性和可重复性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值