# AssertionError: assert <MagicMock> == '/tmp/real'
# first test passes, second fails when run together

- **ID:** `python/pytest-monkeypatch-not-undone`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

A test mutates a global (env var, module attribute, dict) without using monkeypatch or addfinalizer, so the change leaks into subsequent tests.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 7.x | active | — | — |
| 8.x | active | — | — |

## Workarounds

1. **** (98% success)
   ```
   def test_x(monkeypatch):
    monkeypatch.setenv('APP_MODE', 'test')
    monkeypatch.setattr('myapp.config.DEBUG', True)
    run()
   ```
2. **** (90% success)
   ```
   @pytest.fixture
def temp_registry():
    old = myapp.registry.copy()
    yield myapp.registry
    myapp.registry.clear()
    myapp.registry.update(old)
   ```

## Dead Ends

- **** — Fragile and unpredictable; pytest ordering depends on collection and xdist distribution. (90% fail)
- **** — If the test fails before finally, or uses yield fixtures incorrectly, restoration still may not happen. (60% fail)
- **** — Only helps if pytest-randomly is the cause; the underlying leak persists. (85% fail)
