[New-bugs-announce] [issue35330] When using mock to wrap an existing object, side_effect requires return_value

Noam Yorav-Raphael report at bugs.python.org
Tue Nov 27 12:26:39 EST 2018


New submission from Noam Yorav-Raphael <noamraph at gmail.com>:

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
1
>>> m.func.return_value = 3
>>> m.func()
func2 called
2

----------
components: Library (Lib)
messages: 330540
nosy: noamraph
priority: normal
severity: normal
status: open
title: When using mock to wrap an existing object, side_effect requires return_value
type: behavior

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35330>
_______________________________________


More information about the New-bugs-announce mailing list