python
system_error
ai_generated
true
权限错误:[Errno 13] 权限被拒绝:'/tmp/pytest-of-user/pytest-123/test_xxx0/tmp_file.txt'
PermissionError: [Errno 13] Permission denied: '/tmp/pytest-of-user/pytest-123/test_xxx0/tmp_file.txt'
ID: python/pytest-tmp-path-permission
80%修复率
82%置信度
0证据数
2024-08-12首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 7.4.0 | active | — | — | — |
| 8.0.0 | active | — | — | — |
根因分析
测试尝试写入 tmp_path 目录,但该目录权限受限,或测试以非特权用户运行。
English
测试尝试写入 tmp_path 目录,但该目录权限受限,或测试以非特权用户运行。
解决方案
-
90% 成功率
在测试中打印 tmp_path 并确保目录存在且可写,必要时使用 `tmp_path.mkdir(parents=True, exist_ok=True)`。
-
85% 成功率
使用 `tmp_path_factory.mktemp('test_dir')` 创建自定义目录,确保权限正确。
无效尝试
常见但无效的做法:
-
70% 失败
修改系统目录权限可能影响其他进程,且不安全。
-
60% 失败
绝对路径可能指向不可写位置,且测试不可移植。