assert mock.call_count == 3
Testing Patterns

Meaning

Asserts that a mock object has been called a specific number of times, typically used in unit tests to verify interaction counts.

Primary Function

Testing / Mock verification

Communicative Purpose

Verify that a function under test interacted with a dependency the expected number of times.

Pattern

assert mock.call_count == ;

Core Structure

assert mock.call_count == ;

Função primária

Testing / Mock verification

Propósito comunicativo

Verify that a function under test interacted with a dependency the expected number of times.

Situações de gatilho

When testing that a mock is called exactly N times; when verifying call counts after invoking code; when ensuring a callback is invoked a certain number of times.

Contextos

Unit testing frameworks using mock objects, e.g., Python's unittest.mock, pytest-mock.

Padrão

assert mock.call_count == ;

Estrutura central

assert mock.call_count == ;

Slots de substituição

expected_call_count: int

Colocados típicos

  • mock object
  • patch decorator
  • unittest.TestCase
  • pytest fixture

Substituições comuns

  • assert mock.call_count >=
  • assert mock.call_count <=
  • assert mock.call_called()

Erros comuns

Using '=' instead of '==', forgetting to reset mock between tests, asserting on the wrong mock object.

Similar / contraste

assert mock.called (checks at least once), assert mock.call_count == 0 (ensures not called), assert mock.assert_called_once_with() (checks args).

Interferências

Coming from languages without built-in mocking (e.g., C): may forget to use a mocking library and instead manually track calls.

Família do chunk

  • mock assertion
  • call count verification
  • interaction testing

Nuance

The assertion only checks call count, not arguments; combine with assert_called_with or assert_called_once_with for argument verification. Also note that call_count includes all calls, including those from earlier tests if mock not reset.

Efeito pragmático

Makes test expectations explicit and helps catch regressions in interaction frequency.

Dica de memória

“Three strikes and you’re out” – assert the mock was called exactly three times.

Nota

Remember to reset mocks between tests (e.g., mock.reset_mock()) to avoid counting calls from previous tests.

Upgrade path

assert mock.call_count == ; and mock.assert_called_with(;)

Frequência: HighFormulaicidade: Semi-fixedTipo de construção: assertion statementPrioridade de aquisição: Active recallPrioridade de output: BothTag de espaçamento: Short-term

Log in to save chunks.