pytest后置处理方式

在 pytest 中,确保后置清理代码一定会执行(无论测试通过、失败还是发生异常),推荐使用以下两种最稳健的方式:


1. 使用 yield 的 fixture(推荐)

在 fixture 的 yield 之后编写清理代码,pytest 会在测试结束后自动运行这些代码,即使测试断言失败或抛出异常。

复制代码
import pytest

@pytest.fixture
def db_connection():
    # 前置:建立连接
    conn = create_connection()
    yield conn
    # 后置:一定会执行(除非 fixture 前置阶段就挂了)
    conn.close()

def test_query(db_connection):
    assert db_connection.query("SELECT 1") == 1

⚠️ 注意:如果 yield 之前的代码(建立连接)抛出了异常,那么 yield 之后的清理代码不会执行,因为 fixture 本身没有成功初始化。这是合理的------资源从未被成功获取,自然无需释放。


2. 使用 request.addfinalizer

功能与 yield 类似,但以注册回调函数的方式显式声明清理逻辑。

复制代码
import pytest

@pytest.fixture
def db_connection(request):
    conn = create_connection()
    def cleanup():
        conn.close()
    request.addfinalizer(cleanup)   # 注册后置函数
    return conn

同样,addfinalizer 注册成功后的清理函数一定会执行。


3. 在测试类或模块中使用传统 teardown 方法

适用于经典的 xUnit 风格,pytest 完全支持:

  • 方法级def teardown_method(self, method):

  • 类级def teardown_class(cls):

  • 模块级def teardown_module(module):

复制代码
class TestDatabase:
    def setup_method(self):
        self.conn = create_connection()

    def teardown_method(self):
        self.conn.close()   # 每个测试方法结束后一定会执行

    def test_query(self):
        assert self.conn.query("SELECT 1") == 1

4. 如果需要"无论如何(即使 fixture 设置失败)都执行清理"

如果存在必须执行的全局资源回收(例如临时文件、进程等),可以在 fixture 中使用 try/finally

复制代码
@pytest.fixture
def temp_file():
    f = None
    try:
        f = open("/tmp/test.txt", "w")
        yield f
    finally:
        if f:
            f.close()
        # 或者无条件删除文件

但这种情况较少见,因为通常设置失败时资源尚未分配,无需清理。

方式 是否保证后置执行 备注
yield fixture ✅(前置成功时) 最推荐,代码简洁
request.addfinalizer ✅(前置成功时) 与 yield 等价
teardown_* 方法 ✅(对应作用域) 适用于传统风格
try/finally ✅(无条件) 适合必须清理的场景
相关推荐
爆更小哇1 天前
Python自动化测试:pytest新手快速入门指南
python·测试工具·自动化·pytest
qq_452396232 天前
第一篇:基座选型 —— 为什么 Pytest 是自动化的终点?
自动化·pytest
时光不写代码2 天前
修复 pytest-asyncio 事件循环冲突:完整解决方案
python·pytest·fastapi
上天_去_做颗惺星 EVE_BLUE2 天前
接口自动化测试全流程:pytest 用例收集、并行执行、Allure 报告合并与上传
python·pytest
踏着七彩祥云的小丑3 天前
pytest——Mark标记
开发语言·python·pytest
lifewange4 天前
pytest-类中测试方法、多文件批量执行
开发语言·python·pytest
我的xiaodoujiao6 天前
API 接口自动化测试详细图文教程学习系列9--Requests模块
python·学习·测试工具·pytest
我的xiaodoujiao6 天前
API 接口自动化测试详细图文教程学习系列10--Requests模块2--举例说明
python·学习·测试工具·pytest
超梦梦梦梦8 天前
playwright&pytest
pytest