Meaning
Stops all currently active patches started by unittest.mock.patch.start(), restoring every patched object to its original state. Addresses the pain point of mock state leaking between tests, which causes unpredictable test failures and order-dependent test behavior. Triggered when multiple patches were started manually via .start() and you need a single call to clean them all up in teardown.
Primary Function
Testing / Mocking
Communicative Purpose
Cleans up mocks to avoid cross-test interference.
Pattern
patch.stopall()
Core Structure
patch.stopall()
Função primária
Testing / Mocking
Propósito comunicativo
Cleans up mocks to avoid cross-test interference.
Situações de gatilho
Unit testing: cleaning up multiple manually-started patches in tearDown; Test suites: preventing mock leakage when tests share module-level objects; Integration testing: restoring global state after patching environment variables or system calls
Contextos
Python unit testing with unittest.mock
Padrão
patch.stopall()
Estrutura central
patch.stopall()
Slots de substituição
None
Colocados típicos
- unittest.mock.patch
- setUp
- tearDown
- addCleanup
Substituições comuns
- patch.stop() on each patcher individually: more granular control but verbose
- with patch(...) as mock: context manager: automatic cleanup on exit
- preferred for single-test scope
- @patch decorator: auto-stops after test function completes
Erros comuns
Forgetting to call stopall() in tearDown after using .start(), causing mocks to leak into subsequent tests; Calling stopall() before all test assertions run, which restores originals mid-test and causes false passes; Using stopall() with patch decorators which already auto-cleanup, leading to a harmless but confusing no-op call; Starting patches in setUp but stopping only some in tearDown, leaving orphaned patches
Similar / contraste
patch.stop() stops a single patch; patch.stopall() stops all active patches.
Interferências
Coming from Java (using Mockito): expecting mocks to be undone automatically; in Python you must call stopall() or use context managers.
Família do chunk
- unittest.mock
- patch
- MagicMock
- addCleanup
Nuance
Do not use stopall() when using patch as a context manager or decorator — those handle cleanup automatically and stopall() is redundant. No measurable performance impact; it simply iterates the internal active-patches dict and calls stop() on each entry. Calling stopall() with no active patches is a safe no-op; it only cleans up patches started via .start(), not patches managed by context managers or decorators.
Efeito pragmático
Guarantees test isolation by ensuring no mock persists beyond the test.
Dica de memória
Like hitting the emergency stop button on a factory floor — shuts down every running mock machine at once so the next shift starts clean.
Nota
Prefer addCleanup(patch.stopall) over calling it in tearDown — addCleanup runs even if setUp or the test method raises an exception.
Upgrade path
Use patch as a context manager: with patch(...) as m: ...
Log in to save chunks.