如何让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
疑问与需求
- 我对pytest fixture优先级的理解是否存在错误?
- 有没有其他可靠的方法实现用户自定义覆盖该fixture的需求?
内容的提问来源于stack exchange,提问作者Pierrick Rambaud
相关产品推荐
相关产品推荐

