python config_error ai_generated true

收集错误:无法收集 'test_foo',因为它不是函数或类

CollectionError: cannot collect 'test_foo' because it is not a function or class

ID: python/pytest-collection-error

其他格式: JSON · Markdown 中文 · English
80%修复率
83%置信度
0证据数
2024-04-27首次发现

版本兼容性

版本状态引入弃用备注
7.4.0 active

根因分析

pytest 试图收集一个名为 test_foo 的变量或对象,而不是测试函数

English

pytest 试图收集一个名为 test_foo 的变量或对象,而不是测试函数

generic

解决方案

  1. 95% 成功率
    my_foo = 123
  2. 80% 成功率
    collect_ignore = ['test_foo']

无效尝试

常见但无效的做法:

  1. 40% 失败

    如果变量在模块级别,pytest 可能仍会尝试收集

  2. 70% 失败

    只适用于类,不适用于变量