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
80%Fix Rate
84%Confidence
0Evidence
2024-05-12First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 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
-
95% success Rename test files to match pattern
mv my_tests.py test_my_tests.py
-
90% success Use pytest --collect-only to debug discovery
pytest --collect-only -v
Dead Ends
Common approaches that don't work:
-
Creating test files with arbitrary names
80% fail
Pytest only discovers files matching patterns like test_*.py or *_test.py.
-
Running pytest from wrong directory
60% fail
If tests are in a subdirectory, pytest must be run from the root or with --rootdir.