Reputation: 6781
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?
Upvotes: 1
Views: 296
Reputation: 369094
Using side_effect
of the Mock Class:
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
Upvotes: 2