pytest之fixture

什么是fixture 

        fixture 是 pytest 提供的一种功能强大的机制,用于在测试执行之前进行设置和在测试执行之后进行清理。它们可以提高测试代码的复用性、模块化和自动化程度,使测试代码更加简洁和易于维护。通过使用 @pytest.fixture 装饰器,可以定义各种类型的 fixture,并在测试函数中使用它们来简化测试环境的管理。

fixture语法

import pytest


# 定义一个fixture函数
@pytest.fixture
def test_fixture():
    # 在调用位置之前执行的
    print('======成功调用fixture======')
    yield
    # 调用后执行的操作,写yield后
    print('=======清楚fixture========')


class Test_C:
    def test_C001(self, test_fixture):
        print('======这是测试用例1=========')

# 执行结果
# collected 1 item  test_001.py
# ======成功调用fixture======
# ======这是测试用例1=========
# .=======清楚fixture========

执行顺序

由执行结果可以看出,fixture会在作用范围(图中调用fixture的test_C001)之前先执行 yield之前的代码,然后再执行作用范围的代码,最后执行 yield之后的代码。

作用范围scope 

pytest fixture 有四种不同的作用范围(scope)

函数级别

函数级别的fixture ,初始化、清除 会再每个函数或者方法执行前面各调用一次

fixture 的 scope 缺省值 ,就是 function , 所以也可以不指定。

类级别

函数级别的fixture ,初始化、清除 会再每个函数或者方法执行前面各调用一次

使用方法:@pytest.fixture(scope='class'),scope参数指定为class。

import pytest


# 定义一个fixture函数
@pytest.fixture(scope='class')
def class_fixture():
    # 在调用位置之前执行的
    print('======成功调用class_fixture======')
    yield
    # 调用后执行的操作,写yield后
    print('=======清楚class_fixture========')


class Test_C:
    def test_C001(self,class_fixture):
        print('======这是测试用例1=========')
    def test_C002(self,class_fixture):
        print('======这是测试用例2=========')

# 执行结果
# collected 1 item  test_001.py
# ======成功调用fixture======
# ======这是测试用例1=========
# .=======清楚fixture========

就更改了scope='class',其余不变,可以从执行结果发现,在整个类所有用例执行前后分别 执行1次 。 

模块级别

函数级别的fixture ,初始化、清除 会再每个模块执行前面各调用一次

使用方法:@pytest.fixture(scope='module'),scope参数指定为module

import pytest


# 定义一个fixture函数
@pytest.fixture(scope='module')
def module_fixture():
    # 在调用位置之前执行的
    print('======成功调用module_fixture======')
    yield
    # 调用后执行的操作,写yield后
    print('=======清楚module_fixture========')


class Test_C:
    def test_C001(self,module_fixture):
        print('======这是测试用例C1=========')
    def test_C002(self,class_fixture):
        print('======这是测试用例C2=========')
class Test_D:
    def test_C001(self,module_fixture):
        print('======这是测试用例D1=========')
    def test_C002(self,class_fixture):
        print('======这是测试用例D2=========')

# 执行结果
# collected 4 item  test_001.py
# ======这是测试用例1=========
# ======这是测试用例2=========
# ======这是测试用例D1=========
# ======这是测试用例D2=========
# =======清楚class_fixture========


 目录级别

目录级别的 初始化清除,就是针对整个目录执行的初始化、清除。

目录级别的初始化、清除 在 整个目录 所有用例 执行前后 分别 只执行一次

使用方法:@pytest.fixture(scope='package'),scope参数指定为package。

自动调用fixture

一般fixture都要在用例函数的参数中声明,才会进行调用,否则不会进行调用。

若是不行声明,直接进行使用,可以指定fixture的参数 autouse 的值为 true

 一般用在fixture没有返回值,而用例需要调用的情况。

import pytest


# 定义一个fixture函数
@pytest.fixture(autouse=True)
def test_fixture():
    # 在调用位置之前执行的
    print('======成功调用test_fixturee======')
    yield
    # 调用后执行的操作,写yield后
    print('=======清楚test_fixture========')


class Test_C:
    def test_C001(self):
        print('======这是测试用例C1=========')

    def test_C002(self):
        print('======这是测试用例C2=========')


class Test_D:
    def test_C001(self):
        print('======这是测试用例D1=========')

    def test_C002(self):
        print('======这是测试用例D2=========')

# 执行结果
# collected 4 item  test_001.py
# ======成功调用module_fixture======
# ======这是测试用例C1=========
# .=======清楚module_fixture========
# ======成功调用module_fixture======
# ======这是测试用例C2=========
# .=======清楚module_fixture========
# ======成功调用module_fixture======
# ======这是测试用例D1=========
# .=======清楚module_fixture========
# ======成功调用module_fixture======
# ======这是测试用例D2=========
# .=======清楚module_fixture========

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值