python data_error ai_generated true

AssertionError: assert 'original' == 'patched'

ID: python/pytest-monkeypatch-not-reverting

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2024-11-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.6 active

Root Cause

monkeypatch fixture在测试后未正确恢复原始值,导致后续测试受到影响。

generic

中文

monkeypatch未正确恢复,导致测试间状态污染。

Workarounds

  1. 95% success
    def test_example(monkeypatch):
        monkeypatch.setattr(module, 'attr', 'patched')
        # 测试结束后自动恢复
  2. 90% success
    with ExitStack() as stack:
        stack.enter_context(patch('module.attr', 'patched'))

Dead Ends

Common approaches that don't work:

  1. 80% fail

    容易遗漏,且与monkeypatch机制冲突。

  2. 70% fail

    pytest的monkeypatch自动处理,手动恢复可能重复。