# PermissionError：[Errno 13] 权限被拒绝：'/tmp/pytest-of-root/pytest-0/test_x0/out.txt'

- **ID:** `python/pytest-tmp-path-permissions`
- **领域:** python
- **类别:** system_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在一个环境中以 root 运行的测试留下了由 root 拥有的 /tmp/pytest-of-root；随后以其他用户运行无法写入同一个 tmp_path 基础目录。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 7.x | active | — | — |
| 8.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   # pytest.ini
[pytest]
addopts = --basetemp=/home/ci/pytest-tmp
# ensure /home/ci/pytest-tmp exists and is writable
   ```
2. **** (90% 成功率)
   ```
   sudo rm -rf /tmp/pytest-of-root
pytest tests/
   ```

## 无效尝试

- **** — Security risk and the next root run recreates restrictive perms. (70% 失败率)
- **** — May still collide if /var/tmp/pytest-of-root exists from a root run. (60% 失败率)
- **** — Loses pytest cleanup guarantees and can leave garbage. (75% 失败率)
