Reputation: 83
Here is the function to test:
def func_to_be_tested():
output_first = func1()
output_second = func1()
.... rest of function
# func1 will be mocked while testing func_to_be_tested
I want output_first
and output_second
to be different, i.e while mocking they should have different return values of func1
in the same test call for the function func_to_be_tested
.
Is there any way to do this?
Thanks
Upvotes: 2
Views: 3065
Reputation: 16805
This is what side_effect
is for. You can assign a list to side_effect
, and each value in the list will be used as result in subsequent calls, e.g.:
from unittest import mock
from path_to_module import func_to_be_tested
@mock.patch('path_to_module.func1')
def test_func_to_be_tested(mocked_func1):
mocked_func1.side_effect = [output_first, output_second]
func_to_be_tested()
In this case func1
will return output_first
in the first call, and output_second
in the second one.
Upvotes: 2
Reputation: 310
Try defining function in a way that either it generates random values or a value dependent on function parameters.
Upvotes: 0