Mocking python function based on input arguments

后端 未结 8 535
无人及你
无人及你 2020-12-22 21:15

We have been using Mock for python for a while.

Now, we have a situation in which we want to mock a function

def foo(self, my_param):
    #do someth         


        
8条回答
  •  猫巷女王i
    2020-12-22 22:02

    If side_effect is a function then whatever that function returns is what calls to the mock return. The side_effect function is called with the same arguments as the mock. This allows you to vary the return value of the call dynamically, based on the input:

    >>> def side_effect(value):
    ...     return value + 1
    ...
    >>> m = MagicMock(side_effect=side_effect)
    >>> m(1)
    2
    >>> m(2)
    3
    >>> m.mock_calls
    [call(1), call(2)]
    

    http://www.voidspace.org.uk/python/mock/mock.html#calling

提交回复
热议问题