I'm trying to use call_args_list to get the arguments passed to a certain function when it is called multiple times. I'm using this:
call_args_list = mock.add_row.call_args_list
Then I get a list that looks like this: [call('A', []), call('B', []), call('C', ['err'])].
If I only want to check that the second call doesn't have an error in the second argument and the third does, I need to somehow access the items within the call. Does anyone know how can I peel these call objects to get the items inside?
import unittest
import mock
class TestCase(unittest.TestCase):
    def test_my_func(self):
        m = mock.MagicMock()
        m.add_row('A', [])
        m.add_row('B', [])
        m.add_row('C', ['err'])
        calls = m.add_row.call_args_list
        _first_arg, second_arg = calls[1][0]
        self.assertNotEqual(second_arg, ['err'])
        _first_arg, second_arg = calls[2][0]
        self.assertEqual(second_arg, ['err'])
if __name__ == '__main__':
    unittest.main()
                        It doesn't appear to be (well) documented, but mock defines a subclass of tuple named _Call, instances of which are what you see in call_args_list. The first element is the name of the function, the second is a tuple of positional arguments, and the third a dict of keyword arguments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With