You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在外部函数中获取conftest钩子定义的assertion_count变量?

问题:pytest钩子统计断言通过数,外部函数无法获取正确值

我用pytest的pytest_assertion_pass钩子统计通过的断言数量,将计数变量assertion_count定义在conftest.py中。在conftest内部打印该计数时功能正常,但在外部测试函数中访问这个变量时,尽管计数器已更新,却无法获取正确数值。

现有代码

conftest.py

assertion_count = 0

def pytest_assertion_pass(item, lineno, orig, expl):
    global assertion_count
    assertion_count += 1

外部测试函数

def external_test_function():
    print(f"Total Assertions Passed: {assertion_count}")

我曾尝试使用fixtures方案,但仍未解决问题。希望能得到帮助,实现外部方法对assertion_count的正确访问。

内容的提问来源于stack exchange,提问作者Tilak Mallikarjun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 18:22:41