I am trying to mock
an existing object from another library's class for unit tests with pytest
.
However, the attributes (not methods) from the other library are mostly set during runtime.
What I want to achieve
- Get all the benefits of mocking the object with
spec
- Set the (nested) attributes (not methods) needed for my unittests to simulate as if they were set during object creation
from unittest.mock import Mock
from otherlib import ClassName
def test_stuff():
mock_object = Mock(spec=ClassName)
mock_object.data.outward.key = 12345
assert mock_object.data.outward.key == 12345 # AttributeError: Mock object has no attribute 'data'
I made these attempts in code changes but with no success
...
def test_stuff():
mock_object = Mock(spec=ClassName, **{'data.outward.key': 12345})
assert mock_object.data.outward.key == 12345
...
def test_stuff():
mock_object = Mock(spec=ClassName)
attrs = {'data.outward.key': 12345}
mock_object.configure_mock(**attrs)
assert mock_object.data.outward.key == 12345