python
config_error
ai_generated
true
ScopeMismatch:您尝试使用会话作用域的请求对象访问函数作用域的 fixture monkeypatch,涉及工厂
ScopeMismatch: You tried to access the function scoped fixture monkeypatch with a session scoped request object, involved factories
ID: python/pytest-monkeypatch-scope-session
80%修复率
88%置信度
0证据数
2024-04-26首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
根因分析
会话作用域的 fixture 请求了 monkeypatch(函数作用域),违反了 pytest 的作用域排序规则。
English
A session-scoped fixture requested monkeypatch (which is function-scoped), violating pytest's scope ordering rules.
解决方案
-
95% 成功率
@pytest.fixture(scope="session") def config(): return {"url": "http://x"} @pytest.fixture def patched(config, monkeypatch): monkeypatch.setitem(config, "url", "http://test") return config -
90% 成功率
@pytest.fixture(scope="session") def patched(): with pytest.MonkeyPatch.context() as mp: mp.setenv("X", "1") yield
无效尝试
常见但无效的做法:
-
60% 失败
May break expensive setup assumptions and hides the real scope design issue.
-
90% 失败
Does not change scope mismatch behavior.
-
85% 失败
Markers do not alter fixture scope rules.