Skip to content

Instantly share code, notes, and snippets.

@igniteflow
Created October 3, 2016 11:03
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save igniteflow/1be88c18185ffe42a66a46e48118f486 to your computer and use it in GitHub Desktop.
Save igniteflow/1be88c18185ffe42a66a46e48118f486 to your computer and use it in GitHub Desktop.
How to mock an object property in Python
import mock
with mock.patch('path.to.ObjectClass.my_property', new_callable=mock.PropertyMock) as mock_my_property:
mock_my_property.return_value = 'my value'
@lhoncorty
Copy link

lhoncorty commented Jun 12, 2023

Can someone please demonstrate how to do it in other forms? I.e. without a context manager

there is one decorator method:

@patch("...", new_callable=mock.PropertyMock,return_value="some_value")
def test_fun(self, mock):
    pass
@patch('path.to.ObjectClass.my_property', new_callable=mock.PropertyMock)
def test_fun(self, mock):
    mock.return_value="some_value"
    pass

@Alfo5123
Copy link

Alfo5123 commented Jul 9, 2023

Thanks!

@khalilgreenidge
Copy link

@lhoncorty Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment