monkeypatch

Replacing a name inside a module while a test runs, such as swapping a clock function, and restoring it afterwards.

In more detail

pytest's monkeypatch fixture and unittest.mock.patch both do this. Patch the name where your module looks it up, not where the library defines it, or the real object keeps running. Prefer passing the dependency in when the design allows.

Related terms

Taught in

References