Access confirmation attribute for mock instance

How can I claim that the attribute is Mock and / or MagicMock ?

For example,

 from unittest.mock import MagicMock def foo(x): a = x.value m = MagicMock() foo(m) m.attr_accessed('value') # method that does not exist but I wish did; should return True 

What is the actual way to verify that foo tried to access m.value ?

+7
python unit-testing mocking
source share
1 answer

You can use PropertyMock as described here .

eg.

 from unittest.mock import MagicMock, PropertyMock def foo(x): a = x.value m = MagicMock() p = PropertyMock() type(m).value = p foo(m) p.assert_called_once_with() 
+8
source share

All Articles