unittest.mock:如何模拟两次方法调用?

时间:2018-12-14 18:39:26

标签: python-3.x mocking python-unittest.mock

如何使用相同的方法两次调用来模拟datetime.datetime.now?

在我尝试过的下面:

$lookup

1 个答案:

答案 0 :(得分:0)

我成功了。

@patch('mypackage.dt')
def test_metodo(self, mock_date):
    expected = ('2018', "2018-12-14 12:34:56")
    mock_date.today.return_value = mypackage.dt(2018, 12, 14, 12, 34, 56)
    mock_date.today.return_value.strftime.side_effect = list(expected)
    self.assertIsNotNone(metodo())
    self.assertEqual(expected, metodo())