python
config_error
ai_generated
true
ScopeMismatch: 你尝试从 'function' 作用域的 fixture 'user_fixture' 访问 'session' 作用域的 fixture 'db_connection'
ScopeMismatch: You tried to access the 'session' scoped fixture 'db_connection' from a 'function' scoped fixture 'user_fixture'
ID: python/pytest-fixture-scope-mismatch
80%修复率
85%置信度
0证据数
2024-06-05首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.8 | active | — | — | — |
| 3.9 | active | — | — | — |
| 3.10 | active | — | — | — |
| 3.11 | active | — | — | — |
| 3.12 | active | — | — | — |
根因分析
一个具有较窄作用域的 fixture 尝试依赖一个具有较宽作用域的 fixture,这在 pytest 中是不允许的。
English
A fixture with a narrower scope tries to depend on a fixture with a broader scope, which is not allowed in pytest.
解决方案
-
90% 成功率
Restructure fixtures so that the broader-scoped fixture is used directly in tests, not via a narrower fixture.
-
85% 成功率
Change the scope of the dependent fixture to 'session' if appropriate: `@pytest.fixture(scope='session') def user_fixture(db_connection):`
无效尝试
常见但无效的做法:
-
75% 失败
This may cause resource leaks or unintended sharing of state across tests, leading to test pollution.
-
80% 失败
This bypasses the scope check but still violates the intended dependency hierarchy, potentially causing runtime errors.