python
data_error
ai_generated
true
断言错误:断言'original'等于'patched'
AssertionError: assert 'original' == 'patched'
ID: python/pytest-monkeypatch-not-reverting
80%修复率
84%置信度
0证据数
2024-11-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.6 | active | — | — | — |
根因分析
monkeypatch未正确恢复,导致测试间状态污染。
English
monkeypatch fixture在测试后未正确恢复原始值,导致后续测试受到影响。
解决方案
-
95% 成功率
def test_example(monkeypatch): monkeypatch.setattr(module, 'attr', 'patched') # 测试结束后自动恢复 -
90% 成功率
with ExitStack() as stack: stack.enter_context(patch('module.attr', 'patched'))
无效尝试
常见但无效的做法:
-
80% 失败
容易遗漏,且与monkeypatch机制冲突。
-
70% 失败
pytest的monkeypatch自动处理,手动恢复可能重复。