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

运行pytest --collect-only时如何忽略fixtures避免重复测试条目?

解决方法

有几种实用的方式可以实现你要的效果:

  • 命令行输出过滤(无需改代码)
    直接对收集的测试结果做文本处理,提取无参数化后缀的原始测试名并去重,用这条命令就行:

    pytest --collect-only -q | awk -F'[' '{print $1}' | uniq > list_of_tests.txt
    

    原理是把每个测试条目按[分割,只保留前面的原始测试名,再通过uniq去重,最终写入文件的就是每个测试只显示一次无后缀的条目。

  • 修改测试配置(更精准)
    如果不想依赖命令行工具,可在项目根目录的conftest.py里添加一个收集钩子,手动过滤参数化的测试项:

    def pytest_collection_modifyitems(items):
        # 记录已经保留过的原始测试名
        existing_tests = set()
        filtered_items = []
        for item in items:
            # 提取不带参数化后缀的原始测试路径+名称
            base_test_id = item.nodeid.split("[")[0]
            if base_test_id not in existing_tests:
                existing_tests.add(base_test_id)
                filtered_items.append(item)
        # 替换原收集列表为过滤后的结果
        items[:] = filtered_items
    

    这样运行pytest --collect-only -q > list_of_tests.txt时,就只会收集每个测试的原始条目,不会展开参数化的67个条目。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 05:43:15