# PermissionError: [Errno 13] 权限被拒绝: '/tmp/pytest-of-user/pytest-123/test_0/'

- **ID:** `python/pytest-tmpdir-permission-denied`
- **领域:** python
- **类别:** system_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

pytest 的 tmpdir fixture 无法创建或访问临时目录，因为权限不足，通常发生在容器等受限环境中。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |
| 3.10 | active | — | — |
| 3.11 | active | — | — |
| 3.12 | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   Set a custom temp directory with write permissions: `pytest --basetemp=/home/user/tmp`
   ```
2. **** (85% 成功率)
   ```
   Use the `tmp_path` fixture instead of `tmpdir` (returns pathlib.Path) and ensure the parent directory is writable.
   ```

## 无效尝试

- **** — This is a security risk and may not be allowed in containerized environments; also, the issue might be with specific subdirectories. (80% 失败率)
- **** — Running tests as root is dangerous and can mask permission issues that would occur in production. (90% 失败率)
