@pytest.fixture def mocker_fixture(mocker): return mocker.patch('my_module.my_func', return_value=123)
Testing Patterns

Meaning

Defines a pytest fixture that creates a mock object replacing a specified function with one that returns a fixed value. Addresses the pain point of tests depending on external or non-deterministic functions. Reached for when a test needs a controlled, predictable substitute for a real function.

Primary Function

Test isolation

Communicative Purpose

Enables isolated unit testing by replacing real dependencies with deterministic mock return values.

Pattern

@pytest.fixture def fixture_name(mocker): return mocker.patch('module_path.function_name', return_value=value)

Core Structure

@pytest.fixture def ...(mocker): return mocker.patch('...', return_value=...)

Função primária

Test isolation

Propósito comunicativo

Enables isolated unit testing by replacing real dependencies with deterministic mock return values.

Situações de gatilho

Unit testing: replacing an external API call with a fixed response. Database testing: mocking a query function to avoid real DB access. Legacy code: isolating a function under test from its hard dependencies.

Contextos

Unit tests written with pytest and the pytest-mock plugin.

Padrão

@pytest.fixture def fixture_name(mocker): return mocker.patch('module_path.function_name', return_value=value)

Estrutura central

@pytest.fixture def ...(mocker): return mocker.patch('...', return_value=...)

Slots de substituição

fixture_name: valid Python identifier, module_path: dotted module path string, function_name: target function name string, value: any return value

Colocados típicos

  • pytest
  • mocker
  • patch
  • return_value
  • mock
  • test

Substituições comuns

  • Different module/function names
  • alternative return values
  • use of side_effect
  • autospec=True

Erros comuns

Forgetting to include mocker as a parameter → NameError at fixture resolution time. Patching the wrong import path (where used vs where defined) → mock not applied and real function still called. Not returning the mock object from the fixture → tests cannot configure or assert on the mock.

Similar / contraste

Using unittest.mock.patch directly, using the monkeypatch fixture, using pytest's tmpdir fixture for isolation

Interferências

Coming from unittest: using unittest.mock.patch as a decorator or context manager instead of pytest-mock's mocker → loses pytest fixture integration and automatic cleanup. Coming from JavaScript/Jest: expecting jest.fn() style inline mocking → pytest-mock requires explicit patch paths.

Família do chunk

  • pytest mocker fixture

Nuance

Do not use when the real function is simple and deterministic enough to test directly — mocking adds indirection and maintenance cost. Each patched function adds negligible setup overhead. Patching the wrong location (where defined vs where imported) is the most common boundary condition error.

Efeito pragmático

Signals that the test isolates the unit under test by replacing its dependency with a predictable stub.

Dica de memória

Like hiring a stunt double for an actor — the fixture swaps in a stand-in that always delivers the same line so the scene plays out predictably.

Nota

Useful when the dependent function's exact return value is known and does not affect test branching.

Upgrade path

Evolve to use side_effect for dynamic returns or autospec for stricter contract checking.

Frequência: HighFormulaicidade: Semi-fixedTipo de construção: pytest fixture definitionPrioridade de aquisição: Active recallPrioridade de output: OutputTag de espaçamento: Medium-term

Log in to save chunks.