[New-bugs-announce] [issue38932] MagicMock.reset_mocks does not pass all arguments to its children

Vegard Stikbakke report at bugs.python.org
Wed Nov 27 14:59:26 EST 2019


New submission from Vegard Stikbakke <vegard.stikbakke at gmail.com>:

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

----------
components: Library (Lib)
messages: 357581
nosy: vegarsti
priority: normal
severity: normal
status: open
title: MagicMock.reset_mocks does not pass all arguments to its children
type: behavior
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38932>
_______________________________________


More information about the New-bugs-announce mailing list