Comment by tayo42
19 days ago
You would use something like this
https://docs.python.org/3/library/unittest.mock.html#unittes...
Then you would make the mock filter with patch and test the `func` function
psuedo python code would be
@patch(builtins.filter)
def test_func_filter_calls(mock_filter):
mock_filter.return_value = [2,4,6]
func()
mock_filter.assert_called_once_with([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
That wouldn't fail though. It was called only once with [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. The second time it was called with [2, 4, 6, 8, 10].
Likewise, if some_call returned [2, 4, 6, 8, 10] instead, it should only be called with [2, 4, 6, 8, 10] once then.
However, the purpose of this test then becomes questionable. Why are you testing implementation details rather than observable? Is there anything that you could observe that depended on the filter being called once or twice with the same filter function?
Did you try it? If it doesn't work there's also called once if you scroll up on the doc
And as far as whether it's a good idea or not, I generally wouldn't, but was saying when it is important then you do have these tools available,llms aren't the first thing to check for these mistakes. It's up to the engineer to choose between trade offs for your scenario.
Yes. The test passes. https://imgur.com/a/4qlTKlc
To try to monkey patch this in, you would need to also assert that it wasn't called with [2, 4, 6, 8, 10].
At which point, I would again ask "why are you testing that it _wasn't_ called with a given set of values?"
The comment at the root of this is "Unit tests catch that kind of stuff".
... But unit tests aren't for testing internals of implementation but rather observable aspects of a function.
Consider if the code was written so that it was
instead (with the filter being used in func())
This isn't something that unit tests can (or should) identify. It would come out in a code review that there is redundant functionality in func and print_evens.
Using ChatGPT or another tool to assist in doing code reviews can be helpful (my original premise).
https://chatgpt.com/share/697a64a6-33c0-8011-a0f8-ca4fec74ab...
ChatGPT properly identifies the duplicated functionality (even though the code is using different idioms for doing the filtering for even numbers).
2 replies →