Pytest精通指南(06)Fixture scope作用域详解

46 篇文章 1 订阅
28 篇文章 1 订阅


请添加图片描述

前言

从前文中,我们已经知道固件(fixture)的概念、原理、作用域,并且举出很多自定义的前后夹具;

本文不再赘述这些,将继续深入讲解固件(fixture)的作用域(scope)参数

Scope 作用域

固件(fixture) 的作用域决定了其被调用的频率和生命周期;

主要分为两部分:

  • 写在测试用例函数文件中

  • 写在conftest.py文件中conftest.pypytest特有且固定的py模块

写在测试用例函数文件

表示被@pytest.fixture标记的函数的作用域

属性描述
function默认值, 对于每个测试用例(函数或方法),fixture 会在其执行前被调用一次
class对于每个测试类,fixture 会在所有测试用例执行前被调用一次
module对于每个Python模块,fixture 会在该模块中的所有测试用例执行前被调用一次
package对于整个Python包,fixture会在该包中的所有测试用例执行前被调用一次
session对于整个测试,会话中的所有模块、包和测试函数中只调用一次

写在conftest.py文件

如果fixture写在conftest.py文件中,作用则略有不同;主要体现在测试用例的可见性重用性

属性描述
function每一个测试文件中的所有测试用例执行前都会执行一次conftest文件中的fixture
class每一个测试文件中的测试类执行前都会执行一次conftest文件中的fixture
module每一个测试文件执行前都会执行一次conftest文件中的fixture
package当前目录下所有测试py文件执行前执行一次conftest文件中的fixture
session所有测试py文件执行前执行一次conftest文件中的fixture

作用域总结

  • scope参数的默认值function
  • 作用域执行顺序遵循session > package > module > class > function
  • 模块和类中的fixture优先级:当模块和类中有同名的 fixture 时,类的 fixture 会优先被使用。这是因为类的 fixture 是局部的,而模块的 fixture 是全局的。

验证默认作用域


import pytest


@pytest.fixture
def setup_teardown_per_test():
    print("\n前置")
    yield
    print("\n后置")


def test_example1(setup_teardown_per_test):
    assert 1 == 1


def test_example2(setup_teardown_per_test):
    assert 2 == 2


class TestCase01:

    def test_02_a(self):
        print("正在执行 test_02_a 函数...")

    def test_02_b(self):
        print("正在执行 test_02_b 函数...")

请添加图片描述

验证执行顺序遵循

import pytest


@pytest.fixture(autouse=True, scope="function")
def fun():
    print("---fixture : function-前")
    yield
    print("---fixture : function-后")


@pytest.fixture(autouse=True, scope="class")
def fun2():
    print("---fixture : class-前")
    yield
    print("---fixture : class-后")


@pytest.fixture(autouse=True, scope="module")
def fun3():
    print("---fixture : module-前")
    yield
    print("---fixture : module-后")


@pytest.fixture(autouse=True, scope="package")
def fun4():
    print("---fixture : package-前")
    yield
    print("---fixture : package-后")


@pytest.fixture(autouse=True, scope="session")
def fun5():
    print("---fixture : session-前")
    yield
    print("---fixture : session-后")


def test_a():
    print("--------------test_a")


def test_b():
    print("--------------test_b")


class Test01Scope:
    def test_c(self):
        print("--------------test_c")

    def test_d(self):
        print("--------------test_d")

请添加图片描述

验证类中的fixture作用域

import pytest


def test_a():
    print("--------------test_a")


def test_b():
    print("--------------test_b")


class Test01Scope:
    def test_c(self):
        print("--------------test_c")

    def test_d(self):
        print("--------------test_d")

    @pytest.fixture(autouse=True, scope="function")
    def fun(self):
        print("---fixture : function-前")
        yield
        print("---fixture : function-后")

    @pytest.fixture(autouse=True, scope="class")
    def fun2(self):
        print("---fixture : class-前")
        yield
        print("---fixture : class-后")

    @pytest.fixture(autouse=True, scope="module")
    def fun3(self):
        print("---fixture : module-前")
        yield
        print("---fixture : module-后")

    @pytest.fixture(autouse=True, scope="package")
    def fun4(self):
        print("---fixture : package-前")
        yield
        print("---fixture : package-后")

    @pytest.fixture(autouse=True, scope="session")
    def fun5(self):
        print("---fixture : session-前")
        yield
        print("---fixture : session-后")

请添加图片描述

验证重名fixture作用域

import pytest
 
def test_a():
    print("--------------test_a")
 
def test_b():
    print("--------------test_b")
 
 
@pytest.fixture(autouse=True, scope="function")
def fun():
    print("---fixture : function-前")
    yield
    print("---fixture : function-后")
 
 
@pytest.fixture(autouse=True, scope="class")
def fun2():
    print("---fixture : class-前")
    yield
    print("---fixture : class-后")
 
 
@pytest.fixture(autouse=True, scope="module")
def fun3():
    print("---fixture : module-前")
    yield
    print("---fixture : module-后")
 
 
@pytest.fixture(autouse=True, scope="package")
def fun4():
    print("---fixture : package-前")
    yield
    print("---fixture : package-后")
 
 
@pytest.fixture(autouse=True, scope="session")
def fun5():
    print("---fixture : session-前")
    yield
    print("---fixture : session-后")
 
class Test01Scope:
    def test_c(self):
        print("--------------test_c")
 
    def test_d(self):
        print("--------------test_d")
 
    @pytest.fixture(autouse=True, scope="function")
    def fun(self):
        print("---fixture : function-前(类中)")
        yield
        print("---fixture : function-后(类中)")
 
    @pytest.fixture(autouse=True, scope="class")
    def fun2(self):
        print("---fixture : class-前(类中)")
        yield
        print("---fixture : class-后(类中)")
 
    @pytest.fixture(autouse=True, scope="module")
    def fun3(self):
        print("---fixture : module-前(类中)")
        yield
        print("---fixture : module-后(类中)")
 
    @pytest.fixture(autouse=True, scope="package")
    def fun4(self):
        print("---fixture : package-前(类中)")
        yield
        print("---fixture : package-后(类中)")
 
    @pytest.fixture(autouse=True, scope="session")
    def fun5(self):
        print("---fixture : session-前(类中)")
        yield
        print("---fixture : session-后(类中)")

请添加图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

需要休息的KK.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值