@patch.multiple('module', Class1=DEFAULT, func2=MagicMock())\ndef test_something(Class1, func2):
Testing Patterns

Meaning

Applies multiple patches to a module using unittest.mock.patch.multiple, providing mocks or default objects as arguments to the decorated test function.

Primary Function

Apply multiple mocks or default objects to specified attributes of a module for a unit test, injecting them as arguments into the test function.

Communicative Purpose

Indicates that the following test function will receive mocked objects for specified module attributes, enabling isolated testing of code that depends on those attributes.

Pattern

decorator pattern for multiple patching

Core Structure

@patch.multiple('module', Class1=DEFAULT, func2=MagicMock()) def test_function(Class1, func2):

Função primária

Apply multiple mocks or default objects to specified attributes of a module for a unit test, injecting them as arguments into the test function.

Propósito comunicativo

Indicates that the following test function will receive mocked objects for specified module attributes, enabling isolated testing of code that depends on those attributes.

Situações de gatilho

When writing unit tests that need to replace specific module attributes with mocks while leaving others unchanged (using DEFAULT), especially when testing functions that depend on multiple external dependencies.

Contextos

Unit testing with Python's unittest.mock module, typically in test suites using unittest or pytest with mock patching.

Padrão

decorator pattern for multiple patching

Estrutura central

@patch.multiple('module', Class1=DEFAULT, func2=MagicMock()) def test_function(Class1, func2):

Slots de substituição

module: str (module path or object), Class1: str (attribute name to patch with DEFAULT), func2: str (attribute name to patch with a MagicMock), DEFAULT: sentinel from unittest.mock indicating keep original, MagicMock(): mock object constructor

Colocados típicos

  • unittest.mock.patch
  • MagicMock
  • DEFAULT
  • unittest.TestCase
  • pytest fixtures

Substituições comuns

  • Replace MagicMock with PropertyMock for properties
  • use DEFAULT for attributes to keep original
  • use patch.object for single attribute
  • use patch.dict for dictionary patches.

Erros comuns

Forgetting to import patch from unittest.mock, causing NameError; Using wrong attribute names leading to AttributeError during test; Omitting DEFAULT for attributes that should remain unchanged, causing them to be replaced with MagicMock unintentionally; Misordering arguments in test function signature relative to patch decorator order; Applying patch.multiple to non-module objects without proper target specification.

Similar / contraste

@patch.object: patches a single attribute on an object or class; @patch.dict: temporarily modifies a dictionary during a test; Using patch as a context manager (with patch(...)) for limited scope; Using pytest-mock fixture (mocker.patch) for a more concise syntax.

Interferências

Coming from pytest-mock: may expect mocker.patch syntax instead of unittest.mock.patch.multiple; Coming from JavaScript Jest: may expect jest.mock() automatic mocking, forgetting to manually specify DEFAULT for passthrough; Coming from Java Mockito: may expect @Mock annotations, forgetting to pass mocks as function arguments.

Família do chunk

  • unittest.mock
  • patch
  • mock
  • testing

Nuance

Do not use DEFAULT when you actually need a mock; using it passes the real object, potentially turning a unit test into an integration test; Performance impact is minimal as patching occurs only during test execution, though excessive patching can slow test suite startup; Ensure patched attributes are accessed via the injected arguments, not directly via the module, to avoid using unpatched versions.

Efeito pragmático

Allows precise control over which dependencies are mocked, enabling focused unit tests that isolate the unit under test while preserving real behavior for other dependencies.

Dica de memória

Think of patch.multiple as giving each selected actor a custom costume (mock) while letting the rest of the cast wear their regular clothes (DEFAULT).

Nota

Remember to import DEFAULT and MagicMock from unittest.mock; the decorator order matters: the patch decorator must be placed directly above the function definition.

Upgrade path

Consider using pytest fixtures with the mocker fixture for more flexible and reusable mock setups.

Tipo de construção: decorator patternTag de espaçamento: Medium-termIdioma?: Sim

Log in to save chunks.