1

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
Christopher Graf
  • 1,929
  • 1
  • 17
  • 34

1 Answers1

-1

The best I came up with is using another Mock object to use when setting the main mock object's attributes. It works, but I guess there is a better solution for this...?

from unittest.mock import Mock
from otherlib import ClassName

def test_stuff():
    mock_data = Mock(spec=["outward"], key=12345)
    mock_object = Mock(spec=ClassName, data=mock_data)
    mock_object.data.outward.key = 12345

    assert mock_object.data.outward.key == 12345  # tests.py::test_stuff PASSED [100%]
Christopher Graf
  • 1,929
  • 1
  • 17
  • 34