1

I am using python's unittest.Mock framework.

I want to achieve following goal with mock:

The original method is like:

def mymethod(para1, para2, para3):
    return para1 + para2 + para3

I want to mock it to return the first para1 - discard the rest:

def mymethod(para1, para2, para3):
    return para1

I saw we can set return_value of a mocked object, but seems only a hardcoded one. I saw we can get the parameters of a mocked call, but only after that is called. - is there a way to get the parameter and return it dynamically?

4

1 回答 1

2

side_effect模拟类的使用:

import mock

def mymethod(para1, para2, para3):
    return para1 + para2 + para3

m = mock.Mock(side_effect=lambda *args: args[0])
with mock.patch('__main__.mymethod', m):
    assert mymethod(1, 2, 3) == 1
于 2013-08-26T04:40:26.143 回答