python data_error ai_generated true

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

ID: python/valueerror-no-tests-collected

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2024-05-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active — — —
3.9 active — — —
3.10 active — — —

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. Creating test files with arbitrary names 80% fail

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

  2. Running pytest from wrong directory 60% fail

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