prefer-monkeypatch-for-process-state-in-test
Test mutates process-wide state without a restoring test scope.
Why
The working directory and Python import registries are shared by every test in the process. A direct mutation can survive an assertion or setup failure, making later tests depend on execution order.
Fix
Establish restoration in setup with pytest's monkeypatch.chdir, monkeypatch.syspath_prepend, monkeypatch.setitem, or monkeypatch.delitem so the original state is recorded before the mutation. Alternatively, explicitly restore the original state with a matching try/finally. Adding monkeypatch operations only during teardown can undo intended cleanup: undoing a teardown deletion can reinstall a temporary sys.modules entry. Intentional cleanup with established ownership may use a local, reasoned SARJ446 suppression.
Examples
import sys
def test_plugin(fake_plugin): sys.modules["optional_plugin"] = fake_pluginimport sys
def test_plugin(monkeypatch, fake_plugin): monkeypatch.setitem(sys.modules, "optional_plugin", fake_plugin)