python
data_error
ai_generated
true
AssertionError: assert 'original' == 'patched'
ID: python/pytest-monkeypatch-not-reverting
80%Fix Rate
84%Confidence
0Evidence
2024-11-15First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.6 | active | — | — | — |
Root Cause
monkeypatch fixture在测试后未正确恢复原始值,导致后续测试受到影响。
generic中文
monkeypatch未正确恢复,导致测试间状态污染。
Workarounds
-
95% success
def test_example(monkeypatch): monkeypatch.setattr(module, 'attr', 'patched') # 测试结束后自动恢复 -
90% success
with ExitStack() as stack: stack.enter_context(patch('module.attr', 'patched'))
Dead Ends
Common approaches that don't work:
-
80% fail
容易遗漏,且与monkeypatch机制冲突。
-
70% fail
pytest的monkeypatch自动处理,手动恢复可能重复。