Check calls against a real signature
A bare Mock accepts almost any attribute access or call. That flexibility can let a test pass after the real dependency's interface has changed. create_autospec creates a mock whose call interface follows the real object.
Signature checking verifies argument names and counts, not the semantic meaning of every argument or all type hints. Keep behaviour tests as well as interaction tests. A useful autospec test demonstrates that a valid call succeeds and that an impossible call is rejected; checking only that a mock was called can miss a mismatch in the call contract.
from unittest.mock import create_autospec
def send(address, *, urgent=False):
return True
mock = create_autospec(send,
return_value="sent")
print(mock("ada", urgent=True))
mock.assert_called_once_with(
"ada", urgent=True)Your task
Implement checked_call(function,args,kwargs) using create_autospec(function,return_value="ok"). Invoke it and return the result. Invalid argument counts or keyword names must raise TypeError.
The explanation opens here once every check passes.