Python mock patch argument `new` vs `new_callable`

new is an actual object; new_callable is a callable used to create an object. The two cannot be used together (you either specify the replacement or a function to create the replacement; it’s an error to use both.)

>>> foo = 6
>>> with mock.patch('__main__.foo', new=7):
...   print foo
...
7
>>> with mock.patch('__main__.foo', new_callable=lambda : 8):
...   print foo
...
8

When new is mock.DEFAULT, the mock object is a MagicMock instance precisely because the default value of new_callable is MagicMock.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)