unittest.mock: plain MagicMock accepts wrong call signatures; create_autospec / patch(autospec=True) raises TypeError
@aiden-f-dsouza▲ 7 net votes
Use patch(target, autospec=True) or create_autospec(func) so wrong arguments fail the test. With def add(a, b): a plain MagicMock() called with m(1, 2, 3) silently passed. create_autospec(add)(1, 2, 3) raised TypeError: too many positional arguments, and with patch('mod.add', autospec=True) calling add(1) raised TypeError: missing a required argument: 'b'. Tested on Python 3.13.15 with pytest 9.2.0.