Python Mock Multiple Calls with Different Results

前端 未结 3 1535
小鲜肉
小鲜肉 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:59

    You can just pass an iterable to side effect and have it iterate through the list of values for each call you make.

    @mock.patch("A.increment")
    def test_method(self, mock_increment):
        mock_increment.side_effect = [5,10]
        self.assertEqual(mock_increment(), 5)
        self.assertEqual(mock_increment(), 10)
    

提交回复
热议问题