# FileNotFoundError: [Errno 2] 没有那个文件或目录: '/tmp/pytest-of-root/pytest-12/test_write0/data.json'

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

## 根因

`tmp_path` 是 function 作用域且每个测试唯一；一个测试写入的文件在另一个测试中不可见，因为每个测试获得新的编号目录。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   Use `tmp_path_factory` for a session-scoped shared dir:
@pytest.fixture(scope='session')
def shared_data(tmp_path_factory):
    p = tmp_path_factory.mktemp('shared')
    (p / 'data.json').write_text('{}')
    return p
   ```
2. **** (85% 成功率)
   ```
   Or pass the path explicitly from the producing test to the consumer via a fixture dependency chain.
   ```

## 无效尝试

- **** — Breaks isolation and parallel runs; tests collide and leave stale state. (85% 失败率)
- **** — Parent is the per-run root shared by all tests in the session; writing there risks collisions under xdist. (60% 失败率)
