Python Mock Multiple Calls with Different Results

前端 未结 3 1605
小鲜肉
小鲜肉 2021-02-03 19:06

I want to be able to have multiple calls to a particular attribute function return a different result for each successive call.

In the below example, I would like increm

3条回答
  •  没有蜡笔的小新
    2021-02-03 19:57

    I tested and this should work

    import mock
    
    ...
    ...
    
    @mock.patch.object(ClassB, 'method_2')
    @mock.patch.object(ClassA, 'method_1')
    def test_same_method_multi_return_value(self, method_1, method_2):
        # type: () -> None
    
        method_1.return_value = 'Static value'
        method_1.side_effect = [
            'Value called by first time'
            'Value called by second time'
            '...'
        ]
    
    

    Version

    https://mock.readthedocs.io/en/latest/
    mock>=2.0.0,<3.0
    

提交回复
热议问题