-
-
Notifications
You must be signed in to change notification settings - Fork 32k
Mock.assert_has_calls([]) is surprising for users #68841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
from unittest import mock
mmock = mock.MagicMock()
mmock.foobar("baz")
mmock.assert_has_calls([]) # No exception raised. Why?mmock.assert_has_calls(['x']) # Exception raised as expected.Traceback (most recent call last):
File "tt.py", line 7, in <module>
mmock.assert_has_calls(['x']) # Exception raised as expected.
File "/home/robertc/work/cpython/Lib/unittest/mock.py", line 824, in assert_has_calls
) from cause
AssertionError: Calls not found.
Expected: ['x']
Actual: [call.foobar('baz')] |
|
This might go back further, haven't checked 3.3, but IIRC we're only doing fixes on 3.4 up anyhow. |
|
assert_has_calls checks that the calls you specified were made. So if you specify an empty list it *should* pass (or be disallowed as it has no meaning). If you want to check that these calls and *only* those calls were made you should use something like: self.assertEqual(mock.mock_calls, [])http://www.voidspace.org.uk/python/mock/mock.html#mock.Mock.assert_has_calls Maybe a documentation improvement instead. |
|
Ok, so as a doc bug this should still be tracked here - I'm going to reopen it to reflect that, hope thats ok. |
|
Why this case should be explicitly mentioned? Is it an exception from some rules? Is the empty list special? Does existing documentation say that assert_has_calls([]) should raise an exception? |
|
I concur with Serhiy. Current document and example describe clearly Current example: """
>>> mock = Mock(return_value=None)
>>> mock(1)
>>> mock(2)
>>> mock(3)
>>> mock(4)
>>> calls = [call(2), call(3)]
>>> mock.assert_has_calls(calls)
>>> calls = [call(4), call(2), call(3)]
>>> mock.assert_has_calls(calls, any_order=True)
"""Empty list is allowed by same rule. There are no exception. And when people understand this API checks inclusion, calling with |
I can see a thought process being «all my other tests use thing.assert_has_calls([call0, call1]), here let’s check there are no calls with thing2.assert_has_calls([])». Are the correct methods advertised in the right place? (I don’t use mock so I forgot if there is a method to assert not called or if it’s assertEqual(mock calls count, 0) or some False property) |
There is assert_not_called https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.assert_not_called
|
|
I'm reopening this because I don't agree. I opened the bug because we have evidence that users find the current documentation confusing. Saying that its not confusing to us doesn't fix the confusion. Why should we mention the special case of an empty set? Because its the only special case. A simple single sentence: "Note: to assert that no calls were made see |
Is there evidence people get confused by the document? I suppose people get confused because they guessed behavior from existing assert_has_calls usages, without reading docs. The document describe well it tests inclusion, not equality. There are no confusion about it. |
|
alist.extend([]) is also a special case, but it is not explicitly documented, because it is not exceptional. |
|
I agree that the confusion was probably due to assumptions rather than a misreading of the doc, and I would not object to closing this issue. However, Robert did object so I would like to suggest an adjustment that may help: Currently the documentation of assert_not_called is just after that of assert_has_calls. If we reverse their order, then there can be less room for confusion about how to check that nothing was called - you would read that first. https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.assert_has_calls |
|
I do not find the behavior of However I have an opposite problem with
Imagine you are changing a library which adds |


Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: