This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Better error messages in unittest.mock
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.8
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Petter S, eamanu, michael.foord, vstinner, xtreak
Priority: normal Keywords: patch

Created on 2018-10-23 08:35 by Petter S, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 10090 merged Petter S, 2018-10-25 08:03
PR 13746 merged Petter S, 2019-06-02 18:54
Messages (8)
msg328292 - (view) Author: Petter S (Petter S) * Date: 2018-10-23 08:35
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.
msg328344 - (view) Author: Emmanuel Arias (eamanu) * Date: 2018-10-23 20:53
It seems to be a good idea.

If you attach a sample, it would be great.
msg328378 - (view) Author: Petter S (Petter S) * Date: 2018-10-24 16:41
Sure, consider the following code:

from unittest import mock

m = mock.Mock()
m(1, 3)
m("Test", data=[42])

When I call


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


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.
msg328379 - (view) Author: Petter S (Petter S) * Date: 2018-10-24 16:43
(The example above should have been "m.assert_has_calls([])" but it does not affect my point.)
msg328386 - (view) Author: Emmanuel Arias (eamanu) * Date: 2018-10-24 19:16
I think that is a good change. 

Maybe you can apply the change on 3.6, 3.7 and 3.8
msg328743 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-10-28 20:37
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)
msg328745 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-10-28 20:42
Thanks Petter Strandmark!
msg344636 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-06-04 19:35
New changeset 001d63cefaa9d84d6d59aa9db8bac66040c8f0ee by Victor Stinner (Petter Strandmark) in branch 'master':
bpo-35047: Update whatsnew/3.8 for better mock error message (GH-13746)
Date User Action Args
2022-04-11 14:59:07adminsetgithub: 79228
2019-06-04 19:35:08vstinnersetmessages: + msg344636
2019-06-02 18:54:43Petter Ssetpull_requests: + pull_request13628
2018-10-28 20:42:15vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg328745

stage: patch review -> resolved
2018-10-28 20:37:14vstinnersetnosy: + vstinner
messages: + msg328743
2018-10-27 14:46:57serhiy.storchakasetnosy: + michael.foord
2018-10-25 08:03:31Petter Ssetkeywords: + patch
stage: patch review
pull_requests: + pull_request9420
2018-10-24 19:16:58eamanusetmessages: + msg328386
2018-10-24 16:43:47Petter Ssetmessages: + msg328379
2018-10-24 16:41:40Petter Ssetmessages: + msg328378
2018-10-23 20:53:06eamanusetnosy: + eamanu
messages: + msg328344
2018-10-23 19:16:37serhiy.storchakasettitle: Better error messages un unittest.mock -> Better error messages in unittest.mock
2018-10-23 11:45:28xtreaksetnosy: + xtreak
2018-10-23 08:35:36Petter Screate