Repaso
Español
TestingIntermediateTST-17

Which assertion verifies that a mock was called exactly once with specific arguments?

Options

  1. assert mock.called is True and mock.count == 1
  2. mock.assert_called_once_with(42, retry=True) (Correct option)
  3. assert mock(42, retry=True) is not None
  4. mock.verify(42, retry=True, times=1)

Answer

mock.assert_called_once_with(42, retry=True)

Why

unittest.mock objects record every call made to them. assert_called_once_with(...) fails if the mock was called zero times, more than once, or with different arguments.

Other useful ones: assert_called_with (last call), assert_not_called(), and mock.call_args_list to inspect every call.

Remember

Check how a mock was called with assert_called_once_with.

Key terms

Practice this question in Repaso