Chapter 8: Monkeypatching
Alfredo Deza
The Python testing community has had quite a few mocking and patching libraries. Perhaps the most popular one is mock which is now part of the standard library. The mock library is very flexible and has lots of different helpers meant to ease testing. When Python 3 was not available, it wasn’t surprising to see projects declare mock as a dependency, but since Python 3.3 it is available in Python as the unittest.mock module.
Pytest has the monkeypatching fixture that can also be used, and it does offer a few ways to patch objects properly. Explaining patching can be tricky, especially for a dynamic language like Python, but in short, it allows us to (safely) replace code at runtime with other code that ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access