python data_error ai_generated true

值错误:未收集到任何测试。确保测试文件以 'test_' 开头或以 '_test.py' 结尾。

ValueError: No tests were collected. Ensure test files start with 'test_' or end with '_test.py'.

ID: python/valueerror-no-tests-collected

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

版本兼容性

版本状态引入弃用备注
3.8 active
3.9 active
3.10 active

根因分析

Pytest 或 unittest 找不到测试文件,因为它们不符合默认命名约定。

English

Pytest or unittest cannot find test files because they do not follow the default naming conventions.

generic

解决方案

  1. 95% 成功率 Rename test files to match pattern
    mv my_tests.py test_my_tests.py
  2. 90% 成功率 Use pytest --collect-only to debug discovery
    pytest --collect-only -v

无效尝试

常见但无效的做法:

  1. Creating test files with arbitrary names 80% 失败

    Pytest only discovers files matching patterns like test_*.py or *_test.py.

  2. Running pytest from wrong directory 60% 失败

    If tests are in a subdirectory, pytest must be run from the root or with --rootdir.