Author noamraph
Recipients noamraph
Date 2018-11-27.17:26:39
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
When using mock to wrap an existing object, and using side_effect to set a function to wrap a method, I would expect the wrapper function to be called instead of the wrapped function, and its return value to be returned. Instead, both the wrapper function and the wrapped functions are being called, and the return value of the wrapped function is returned.

If, in addition to side_effect, return_value is set, the return_value is ignored, but my expected behavior actually happens: only the wrapper function is called, and its return value is returned.

Python 3.7.0 (default, Aug 22 2018, 20:50:05)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from unittest import mock
>>> class MyClass(object):
...     def func(self):
...         print('func called')
...         return 1
>>> c = MyClass()
>>> m = mock.Mock(wraps=c)
>>> def func2():
...     print('func2 called')
...     return 2
>>> m.func.side_effect = func2
>>> m.func()
func2 called
func called
>>> m.func.return_value = 3
>>> m.func()
func2 called
Date User Action Args
2018-11-27 17:26:39noamraphsetrecipients: + noamraph
2018-11-27 17:26:39noamraphsetmessageid: <>
2018-11-27 17:26:39noamraphlinkissue35330 messages
2018-11-27 17:26:39noamraphcreate