Search code examples
pythonmockingpython-mock

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?


Solution

  • 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)