unittest.mock.Mock.return_value

return_value

Set this to configure the value returned by calling the mock:

1
2
3
4
>>> mock = Mock()
>>> mock.return_value = 'fish'
>>> mock()
'fish'

The default return value is a mock object and you can configure it in the normal way:

1
2
3
4
5
>>> mock = Mock()
>>> mock.return_value.attribute = sentinel.Attribute
>>> mock.return_value()
<Mock name='mock()()' id='...'>
>>> mock.return_value.assert_called_with()

return_value can also be set in the constructor:

1
2
3
4
5
>>> mock = Mock(return_value=3)
>>> mock.return_value
3
>>> mock()
3
doc_python
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.