I am trying to mock the return value of the following method
import gitlab
from unittest.mock import patch
def get_all_iters():
gl = gitlab.Gitlab(url='test_url', private_token)
result = gl.groups.get(1).iterations.list() # trying to mock the result of this method call
return result
@patch('gitlab.Gitlab')
@patch('gitlab.v4.objects.GroupManager')
@patch('gitlab.mixins.ListMixin.list')
def test_my_code(mockGitlab, mockGroup, mockList):
mockList.return_value = ['a', 'b']
result = get_all_iters()
print(result)
Although I have tried to mock the return value of the method call, it is still returning the mock object instead of what I tried to mock
<MagicMock name='Gitlab().groups.get().iterations.list()' id='1688988996464'>
I have found the following solution for your problem (I have tested it in my system):
The execution of the test method on my system is:
that is the print of the desired value for
resultset by your instructionmockList.return_value = ['a', 'b'].Some notes about the test
Note the instructions:
By those instructions we can get the right Mock objects.
Furthermore note the presence of the instructions
patch.object()and not only of thepatch()instruction.There is probably a simpler solution, but since you desire to mock the
listget by the complex instruction:I have been able to find only this way!
This post is old but useful to add more details to my explanation.