Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any value I can use for method assert_called_once_with, which would match anything?

I have this function

def my_function(param1, param2):
    ...
    my_other_function(param1, param2, <something else>)
    ...

I want to test that my_other_function is being called with param1 and param2, and I don't care about the rest

I wrote a test like this

@mock.patch('mymodule.my_other_function')
def test_my_other_function_is_called(my_other_function_mock):

   my_function('foo', 'bar')
   my_other_function_mock.assert_called_once_with('foo', 'bar', ?????)

Is there any value I can pass to the assert_called_once_with method (or any of the "sister" method from MagicMock, so that the assertion passes? Or do I have to manually get the calls list, and check each of the parameters with which the function was called?

like image 863
vlad-ardelean Avatar asked Oct 24 '25 15:10

vlad-ardelean


1 Answers

In the docs https://docs.python.org/3/library/unittest.mock.html#any is said that you can use unittest.mock.ANY:

@mock.patch('mymodule.my_other_function')
def test_my_other_function_is_called(my_other_function_mock):

   my_function('foo', 'bar')
   my_other_function_mock.assert_called_once_with('foo', 'bar', ANY)
like image 72
Alexey Ruzin Avatar answered Oct 26 '25 05:10

Alexey Ruzin