Message357581
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
m.reset_mock(return_value=True)
print(m.a())
```
Pertinent line in Github https://github.com/python/cpython/blob/dadff6f6610e03a9363c52ba9c49aa923984640a/Lib/unittest/mock.py#L601 |
|
Date |
User |
Action |
Args |
2019-11-27 19:59:26 | vegarsti | set | recipients:
+ vegarsti |
2019-11-27 19:59:26 | vegarsti | set | messageid: <1574884766.44.0.780090732457.issue38932@roundup.psfhosted.org> |
2019-11-27 19:59:26 | vegarsti | link | issue38932 messages |
2019-11-27 19:59:26 | vegarsti | create | |
|