unittest.mock provides Mock and various subclasses (NoncallableMock, MagicMock, NoncallableMagicMock). These classes are useful and general purpose, b

Make Simple Mocks With SimpleNamespace

submited by
Style Pass
2022-01-14 21:00:10

unittest.mock provides Mock and various subclasses (NoncallableMock, MagicMock, NoncallableMagicMock). These classes are useful and general purpose, but they’re also fairly complex. You have to pick between them, decide on the various arguments they take, and they come with several “gotchas”. Notably, unless you pass in spec, typo’d attribute access will return a new Mock, which is truthy: In [ 1 ]: from unittest import mock In [ 2 ]: m = mock . Mock ( verbose = False ) In [ 3 ]: if m . vrebose : ... : print ( "The medium is the massage" ) ... : The medium is the massage

For simple mocks, I prefer to use Python’s types.SimpleNamespace. This class sets its attributes from given keyword arguments: In [ 1 ]: from types import SimpleNamespace In [ 2 ]: obj = SimpleNamespace ( x = 12 , y = 17 ) In [ 3 ]: obj Out [ 3 ]: namespace ( x = 12 , y = 17 ) In [ 4 ]: obj . x Out [ 4 ]: 12 In [ 5 ]: obj . y Out [ 5 ]: 17

You can use a SimpleNamespace to replace an object when you know only certain attributes are required: from types import SimpleNamespace import example def test_should_log (): config = SimpleNamespace ( verbose = True ) result = example . should_log ( config ) assert result is True

Leave a Comment