# ScopeMismatch: 你尝试从 'function' 作用域的 fixture 'user_fixture' 访问 'session' 作用域的 fixture 'db_connection'

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

## 根因

一个具有较窄作用域的 fixture 尝试依赖一个具有较宽作用域的 fixture，这在 pytest 中是不允许的。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   Restructure fixtures so that the broader-scoped fixture is used directly in tests, not via a narrower fixture.
   ```
2. **** (85% 成功率)
   ```
   Change the scope of the dependent fixture to 'session' if appropriate: `@pytest.fixture(scope='session')
def user_fixture(db_connection):`
   ```

## 无效尝试

- **** — This may cause resource leaks or unintended sharing of state across tests, leading to test pollution. (75% 失败率)
- **** — This bypasses the scope check but still violates the intended dependency hierarchy, potentially causing runtime errors. (80% 失败率)
