feat(tests): Add helper to count mock calls #103941
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The Python built-in
MagicMockclass hasassert_called_once_with,assert_any_call,assert_has_calls, etc, but none of those is helpful if what you really want isassert_called_twice_withorassert_called_exactly_three_times_with. This therefore adds a new testing helper,count_matching_calls, which works exactly the same way as the built-in ones, except that you also have to pass it the mock. So, for example, the following sets of assertions are equivalent (yes, I know some of the mock methods don't actually exist - that's the point):This will be helpful for testing an upcoming PR involving caching, where for example it will be nice to be able to test that
cache.getis called twice whilecache.setis only called once (or not at all, if certain criteria aren't met).