# test_foo 拆卸时出错
RuntimeError: 在 throw() 之后生成器没有停止

- **ID:** `python/pytest-session-fixture-finalizer-error`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

基于 yield 的 fixture 在 yield 之后的 teardown 中引发异常，生成器未捕获，pytest 的 finalizer 机制报告生成器仍在运行。

## 版本兼容性

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

## 解决方案

1. **** (92% 成功率)
   ```
   @pytest.fixture
def client():
    c = connect()
    try:
        yield c
    finally:
        try:
            c.close()
        except Exception as e:
            log.warning('close failed: %s', e)
   ```
2. **** (90% 成功率)
   ```
   @pytest.fixture
def client(request):
    c = connect()
    request.addfinalizer(c.close)
    return c
   ```

## 无效尝试

- **** — The error occurs during fixture teardown, outside the test body, so the test's try/except never sees it. (80% 失败率)
- **** — Loses teardown entirely; resources are never released and the error is replaced by a leak. (70% 失败率)
