python
config_error
ai_generated
true
ScopeMismatch: 你尝试在 session 作用域的请求对象中访问 function 作用域的 fixture db
ScopeMismatch: You tried to access the function scoped fixture db with a session scoped request object
ID: python/pytest-scope-mismatch
80%修复率
87%置信度
0证据数
2024-09-12首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
根因分析
session 作用域的 fixture 依赖了 function 作用域的 fixture,这是不允许的,因为 session fixture 的生命周期长于 function fixture。
English
A session-scoped fixture depends on a function-scoped fixture, which is not allowed because the session fixture outlives the function fixture.
解决方案
-
90% 成功率
Promote the dependency to a compatible scope: @pytest.fixture(scope='session') def db(): ... -
88% 成功率
Or demote the dependent fixture to function scope: @pytest.fixture(scope='function') def client(db): ...
无效尝试
常见但无效的做法:
-
60% 失败
Defaults to function scope, which is the same problem.
-
70% 失败
Does not change scope compatibility.