unittest.mock

The mocking library in Python's standard library, which builds mock objects and records how they were called.

In more detail

It provides Mock and MagicMock, patch for swapping names during a test, create_autospec for doubles that match a real signature, and assertion methods such as assert_called_once_with. It is part of Python itself, so it needs no installation.

Example

gateway.charge.assert_called_once_with(1000, "abc")

Related terms

Taught in

References