June 2017
Beginner to intermediate
274 pages
6h 49m
English
What do we do, though, when the code we're testing reaches out to the system automatically and accesses something we want to replace with a mock object? For example, what if the code we're testing calls time.time? This is where the unittest.mockpatch function comes into play.
The patch function is a context manager and it could temporarily replace nearly any object in any package or module with a mock object. Once the with block exits, the real object is restored to its position, as shown in the following code example:

Read now
Unlock full access