Created on 2018-10-23 08:35 by Petter S, last changed 2019-06-04 19:35 by vstinner. This issue is now closed.
When developing unit tests with `unittest.mock`, it is common to see error messages like this:
AssertionError: Expected 'info' to not have been called. Called 3 times.
It would be really helpful if those 3 calls were listed in the assertion error.
I am happy to add this if people agree it is a good thing.
It seems to be a good idea. If you attach a sample, it would be great.
Sure, consider the following code:
from unittest import mock
m = mock.Mock()
m(1, 3)
m("Test", data=[42])
When I call
m.assert_not_called()
I get the following error message:
AssertionError: Expected 'mock' to not have been called. Called 2 times.
This is what I would like to improve. On the other hand, if I call
m.assert_has_calls(mock.call(3))
I get the following error:
AssertionError: Calls not found.
Expected: ['', (3,), {}]
Actual: [call(1, 3), call('Test', data=[42])]
This is great and may serve as a template for what I want to introduce.
(The example above should have been "m.assert_has_calls([mock.call(3)])" but it does not affect my point.)
I think that is a good change. Maybe you can apply the change on 3.6, 3.7 and 3.8
New changeset 47d94241a383e2b8a2c40e81d12d40d5947fb170 by Victor Stinner (Petter Strandmark) in branch 'master': bpo-35047, unittest.mock: Better error messages on assert_called_xxx failures (GH-10090) https://github.com/python/cpython/commit/47d94241a383e2b8a2c40e81d12d40d5947fb170
Thanks Petter Strandmark!
New changeset 001d63cefaa9d84d6d59aa9db8bac66040c8f0ee by Victor Stinner (Petter Strandmark) in branch 'master': bpo-35047: Update whatsnew/3.8 for better mock error message (GH-13746) https://github.com/python/cpython/commit/001d63cefaa9d84d6d59aa9db8bac66040c8f0ee
stage: patch review -> resolved