# 作用域不匹配：您尝试使用'session'作用域的请求对象访问'function'作用域的fixture 'db'

- **ID:** `python/pytest-fixture-scope-conflict`
- **领域:** python
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

pytest中，高作用域fixture（如session）请求低作用域fixture（如function）时，pytest无法保证低作用域fixture的生命周期，从而报错。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.6 | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   在fixture定义中修改scope参数，例如：@pytest.fixture(scope='session') def db(): ... 改为 @pytest.fixture(scope='function') def db(): ...
   ```
2. **** (70% 成功率)
   ```
   def session_fixture(request):
    db = request.getfixturevalue('db')
    return db
   ```

## 无效尝试

- **** — 如果fixture本身需要每个测试独立状态，改为session会导致状态污染，且仍然无法解决根本的依赖关系错误。 (60% 失败率)
- **** — autouse只影响自动应用，不改变作用域依赖，错误依旧存在。 (90% 失败率)
