I am trying to use unittest.mock.MagicMock
to mock an API. I am not understanding to how check the arguments passed to a method on a mocked object. I thought that this should work:
from unittest import mock
import unittest
class TestMock(unittest.TestCase):
def setUp(self):
pass
def test_basic(self):
obj = unittest.mock.MagicMock()
obj.update(1,2,a=3)
print("obj.update: ", obj.update)
print("obj.update.call_args: ", obj.update.call_args)
print("obj.update.call_args.args: ", obj.update.call_args.args)
self.assertEqual((1,2), obj.update.call_args.args)
However, pytest test_mock.py
fails:
test_mock.py:17: AssertionError
------------------------------------------------ Captured stdout call ------------------------------------------------
obj.update: <MagicMock name='mock.update' id='140707024533072'>
obj.update.call_args: call(1, 2, a=3)
obj.update.call_args.args: args
============================================== short test summary info ===============================================
FAILED test_mock.py::TestMock::test_basic - AssertionError: (1, 2) != args
================================================= 1 failed in 0.10s ==================================================
The docs say that I should be able to access the positional args via the call_args.args
, but that seems to be creating a mocked field instead. What is the correct way to verify the arguments?