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

如何让pytest插件中的folder_structure fixture支持用户覆盖?

pytest插件fixture覆盖问题排查与实现方案

问题场景

我正在开发一款基于Google Earth Engine API的pytest插件,核心功能是在开发者账号中创建和清理目录树。为了支持用户自定义目录结构,我在插件的plugin.py中定义了一个session级别的fixture folder_structure,默认返回空字典:

# plugin.py

@pytest.fixture(scope="session")
def folder_structure():
    return {} 

按照pytest插件文档的说明,conftest.py中的fixture优先级高于插件内的fixture,因此我期望用户能通过在自身项目的conftest.py中定义同名fixture来覆盖默认结构。用户侧的conftest.py代码如下:

# conftest.py

@pytest.fixture(scope="session")
def folder_structure():
    return {"toto": {
        "tutu": "<a GEE API object>"
        } 
    }

但运行以下测试时断言失败,说明用户定义的fixture并未生效:

def test_folder_structure(folder_structure):
    assert len(folder_structure) > 0

疑问与需求

  1. 我对pytest fixture优先级的理解是否存在错误?
  2. 有没有其他可靠的方法实现用户自定义覆盖该fixture的需求?

内容的提问来源于stack exchange,提问作者Pierrick Rambaud

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 16:04:56