python
data_error
ai_generated
true
FileNotFoundError: [Errno 2] 没有那个文件或目录: 'test_data.csv'
FileNotFoundError: [Errno 2] No such file or directory: 'test_data.csv'
ID: python/unittest-testloader-filenotfounderror
80%修复率
85%置信度
0证据数
2024-05-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.8 | active | — | — | — |
| 3.9 | active | — | — | — |
| 3.10 | active | — | — | — |
| 3.11 | active | — | — | — |
| 3.12 | active | — | — | — |
根因分析
一个 unittest TestCase 尝试使用相对路径加载数据文件,但当前工作目录不是测试数据目录。
English
A unittest TestCase tries to load a data file using a relative path, but the current working directory is not the test data directory.
解决方案
-
95% 成功率
Use an absolute path derived from the test file location: `import os; data_path = os.path.join(os.path.dirname(__file__), 'test_data.csv')`
-
90% 成功率
Place test data in a fixed directory and configure the test runner to always start from the project root.
无效尝试
常见但无效的做法:
-
80% 失败
This is inefficient and clutters the project; the file might still be missed if the path resolution is inconsistent.
-
70% 失败
This can affect other tests running in parallel and cause race conditions or side effects.