Toggling Python’s unit test mock patchers

I have recently found out about handy start and stop patch methods in Python’s mock library (or unittest.mock since Python 3.3). I started using it immediately and one of the first improvements I could think of was the ability to toggle the patcher off/on for a single test case using a decorator.

If you are not familiar with start and stop methods you can start out reading mock documentation (or for 3.3+). If you can’t think of a use case, it’s probably not for you. :) Otherwise, you can find both the code and the example usage below.

Read more…