How can I confirm later that the child is bullied by calling parent_mock ?
Well, there is an undocumented _mock_new_parent attribute that you could use like this:
>>> from unittest.mock import MagicMock >>> parent_mock = MagicMock() >>> child_mock1 = parent_mock(a=1) >>> child_mock2 = parent_mock(b='spam') >>> child_mock1._mock_new_parent is parent_mock True >>> child_mock2._mock_new_parent is parent_mock True
... but it seems like the answer to all your other questions is "you can't."
I assume that you could subclass MagicMock track your children with something like this ...
class MyMock(MagicMock): def __init__(self, *args, **kwargs): MagicMock.__init__(self, *args, **kwargs) self._kids = [] def __call__(self, *args, **kwargs): result = MagicMock.__call__(self, *args, **kwargs) self._kids.append((args, kwargs, result)) return result
... then you could do ...
>>> parent_mock = MyMock() >>> child_mock1 = parent_mock(a=1) >>> child_mock2 = parent_mock(b='spam') >>> parent_mock._kids [((), {'a': 1}, <MyMock name='mock()' id='140358357513616'>), ((), {'b': 'spam'}, <MyMock name='mock()' id='140358357513616'>)] >>> parent_mock._kids[0][2] is child_mock1 True >>> parent_mock._kids[1][2] is child_mock2 True
source share