Author vegarsti
Recipients vegarsti
Date 2019-11-27.19:59:26
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
MagicMock, from unittest.mock, has a method reset_mock, which takes optional arguments return_value and side_effect, both with default values False.

In the body of reset_mock, reset_mock is again called on all the _mock_children of of the MagicMock object. However, here the arguments are not passed. This means that if you have a MagicMock object with children that are also mocked, and methods on these have been directly mocked, then it is not enough to call reset_mock on the parent object. A code example that demonstrates this follows below. Here, we could expect m to have been completely reset. But the final print statement shows that m.a() still returns 1.

from unittest.mock import MagicMock

m = MagicMock(a=MagicMock())
m.a.return_value = 1

Pertinent line in Github
Date User Action Args
2019-11-27 19:59:26vegarstisetrecipients: + vegarsti
2019-11-27 19:59:26vegarstisetmessageid: <>
2019-11-27 19:59:26vegarstilinkissue38932 messages
2019-11-27 19:59:26vegarsticreate