Skip to content

over-mocked-test

Tests should not use more than five independently rooted test doubles or collaborator substitutions.

Why

Broad double setup obscures the behavior under test and often couples tests to implementation wiring.

Fix

Prefer real in-process dependencies, a component harness, or purpose-built fakes; keep mocks at true external boundaries.

Examples

Before — flagged Test patches six collaborators
tests/test_service.py
from unittest.mock import patch
@patch("shop.inventory.reserve")
@patch("shop.payments.charge")
@patch("shop.shipping.quote")
@patch("shop.tax.calculate")
@patch("shop.email.send_receipt")
@patch("shop.risk.approve")
def test_checkout(risk, email, tax, shipping, payments, inventory):
assert checkout() == "confirmed"
After — preferred Component test keeps one external boundary
tests/test_service.py
from unittest.mock import patch
@patch("shop.payment_gateway.charge")
def test_checkout(gateway, test_database, inventory_service):
result = checkout(test_database, inventory_service)
assert result.status == "confirmed"