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 ✅(无条件) 适合必须清理的场景
相关推荐
鲸渔4 天前
【AI Agent系列课程23-Python 基础23】测试入门:为函数写第一个 pytest 用例
开发语言·python·pytest·ai编程
2601_967980117 天前
python pytest怎样测试代码
python·pytest·测试·文档·断言
2601_967980117 天前
python pytest如何编写测试
python·测试用例·pytest·函数·assert
2601_967980117 天前
Python pytest如何实现参数化测试
测试用例·pytest·python测试框架·参数化测试·ids参数
测试界的小码农24 天前
【让 AI 生成的测试用例变成 pytest API 自动化脚本】
人工智能·测试用例·pytest
2601_967987371 个月前
Python在自动化测试中有哪些应用?
python自动化测试·selenium·appium·pytest·性能测试
Madison-No71 个月前
pytest框架
pytest
凛铄linshuo1 个月前
分析业务流程什么时候适合断言?
python·pytest
凛铄linshuo1 个月前
自动化测试:pytest第一个实例学习
学习·pytest
Helix2501 个月前
Python单元测试实战:pytest框架从入门到企业级测试体系搭建指南
python·单元测试·实战·mock·pytest·测试覆盖率·fixture