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

如何动态创建由其他fixture组成的pytest fixture列表

动态生成Pytest Fixture列表的解决方案

要实现动态填充fix_list而无需静态关联其他fixture,核心是使用pytest提供的request.getfixturevalue()方法——它可以主动触发指定fixture的执行并返回其结果,无需在fixture的函数签名中提前声明依赖。

按命名规则筛选的实现代码

import pytest

@pytest.fixture
def fix_foo():
    print("fix_foo")
    return "foo"

@pytest.fixture
def fix_fiz_foo():
   print("fix_fiz_foo")
   return "fiz_foo"

@pytest.fixture
def fix_other():
    print("fix_other")
    return "other"

@pytest.fixture
def fix_other2():
    print("fix_other2")
    return "other2"

def test_foo(fix_list):
    # 验证动态生成的列表内容
    assert len(fix_list) == 2
    assert "foo" in fix_list
    assert "fiz_foo" in fix_list

@pytest.fixture
def fix_list(request):
    # 按命名规则筛选以"_foo"结尾的fixture
    foo_fixture_names = [
        name for name in request._fixturemanager._arg2fixturedefs
        if name.endswith("_foo")
    ]
    
    # 动态获取每个fixture的结果并组成列表
    return [request.getfixturevalue(name) for name in foo_fixture_names]

为什么之前的尝试失败?

你之前直接访问fix_def.cached_result失败,是因为pytest的cached_result仅在fixture被当前测试上下文激活(无论是静态声明依赖,还是通过getfixturevalue动态触发)后才会存储结果。直接遍历fixture管理器的定义不会自动激活这些fixture,所以cached_result始终为None。而getfixturevalue()会主动触发fixture的执行流程,确保你能拿到实际的返回值。

扩展:按值类型/属性筛选

如果需要按fixture返回值的类型或属性筛选,可以先动态获取值再判断:

@pytest.fixture
def fix_list(request):
    all_fixture_names = list(request._fixturemanager._arg2fixturedefs.keys())
    filtered_values = []
    
    for name in all_fixture_names:
        try:
            # 尝试获取fixture值,跳过参数化或特殊fixture
            value = request.getfixturevalue(name)
            # 按类型筛选:仅保留字符串类型结果
            if isinstance(value, str):
                filtered_values.append(value)
            # 或按属性筛选:if hasattr(value, "target_attr"): filtered_values.append(value)
        except Exception:
            # 跳过无法正常获取的fixture
            continue
    
    return filtered_values

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 08:27:07