unit testing - How to return multiple values when using mock patch.object with contextlib.nested -
i new unit testing , trying write unit test using patch.object mock function calls. mocked function getobj()
called twice in function testing. first time when called expecting none
return_value
, second time expecting some_string
. not getting how it.
the code follows:
def test_create(self): contextlib.nested( patch.object(agent, 'try_connect', patch.object(util, 'get_obj', return_value = none), ) (mock_try_connect, mock_get_obj): proxy_info = self.util.create(data)
i tried using side_effects
, , give input return_value
every time returning none
.
mock_value=mock() mock_value.side_effect = [none, 'some_string'] patch.object(util, 'get_obj', return_value = mock_value())
use assert_has_calls
verify mocked out object being called how expect called. debug issue.
def test_create(self): contextlib.nested( patch.object(agent, 'try_connect', patch.object(util, 'get_obj', side_effect = [none, 'some_string']), ) (mock_try_connect, mock_get_obj): proxy_info = self.util.create(data) mock_try_connect.assert_has_calls([mock.call(#params), ...]) mock_get_obj.assert_has_calls([...])
now once you've debugged issue can delete last 2 statements since add nothing test coverage , make code harder maintain.
Comments
Post a Comment