Python: inspect unittest.mock calls with attached mocks 0 ▲ Adam Johnson 2 hours ago · Tech · hide · 0 comments When you use a unittest.mock mock, you often make assertions on the calls it received, such as through the mock_calls list. But sometimes you want to assert on the order of calls across multiple mocked functions, for example to check that steps in a process happen in the right sequence. To do this, use attached mocks, which let you collect calls from multiple mocks into a single timeline. A parent mock has multiple child mocks attached to it, and its mock_calls list records calls from all of its children together, interleaved in the order they actually happened. Sometimes that attachment happens automatically, and sometimes you have to set it up yourself. Say we have a Kettle class, and a function that fills one by opening the lid, adding water, and closing the lid again: class Kettle: def open(self) -> None: ... def add_water(self, litres: float) -> None: ... def close(self) -> None: ... def fill(kettle: Kettle, litres: float) -> None: kettle.open() kettle.add_water(litres)… No comments yet. Log in to reply on the Fediverse. Comments will appear here.