August 2018
Intermediate to advanced
366 pages
10h 14m
English
mock.patch replaces, within the context, the specified object or class with a Mock instance.
Mock will do nothing when called, but will track their arguments and will allow you to check that they were called as expected.
So with mock.patch, we replace print with Mock and we keep a reference to Mock as mprint:
with mock.patch('builtins.print') as mprint:
print_division(4, 2)
This allows us to check that print was called with the expected arguments, through Mock, later on:
mprint.assert_called_with(2)