Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Testing and Quality Principles

70. Test Pyramid

Definition. Maintain a healthy balance of unit, integration, and end-to-end tests; favor many fast unit tests over a few slow end-to-end ones.

Rationale. A broad base of fast unit tests gives rapid feedback; a small top of E2E tests covers integration. Inverting the pyramid makes suites slow and brittle.

Example. Hundreds of unit tests for logic, dozens of integration tests for adapters, a handful of E2E tests for critical flows.

Anti-pattern / failure mode. An “ice-cream cone”: many slow E2E tests and few unit tests. Review signal: long CI times and flaky suites.

Related. FIRST (#72), Continuous Integration (#76), capstone review rubrics (practice).

71. Arrange-Act-Assert (AAA)

Definition. Structure tests into setup, execution, and verification; keep each test focused on one behavior.

Rationale. A clear structure makes tests readable and failures easy to diagnose. One behavior per test keeps the cause of failure unambiguous.

Example. Arrange fixtures, act by calling the method, assert the outcome; nothing else in between.

Anti-pattern / failure mode. Tests that mix setup, multiple actions, and assertions; tests asserting several unrelated things. Review signal: a failing test doesn’t say what broke.

Related. Test Isolation (#74), FIRST (#72), testable requests (Command).

72. FIRST Principles

Definition. Tests should be Fast, Independent, Repeatable, Self-validating, and Timely.

Rationale. Fast tests run often; independent tests don’t order-depend; repeatable tests are deterministic; self-validating tests need no manual inspection; timely tests are written close to the code. Together they make tests trustworthy.

Example. A unit test that runs in milliseconds, uses in-memory fakes, and asserts via code.

Anti-pattern / failure mode. Slow, order-dependent, flaky tests that require manual checking. Review signal: developers stop running the suite locally.

Related. Deterministic Testing (#75), Test Isolation (#74), swappable test doubles (Strategy).

73. Shift Left Testing

Definition. Detect defects earlier in the development lifecycle; test during development, not only before release.

Rationale. Bugs are cheaper to fix when found early. Integrating testing into development catches issues before they compound.

Example. Run unit tests on every save and in pre-commit hooks, not just in CI.

Anti-pattern / failure mode. Testing only in a late QA phase; discovering integration issues at release. Review signal: defects found late with large fix cost.

Related. Continuous Integration (#76), Regression Testing (#77), small testable slices (Vertical Slice).

74. Test Isolation Principle

Definition. Tests should not depend on uncontrolled external state; isolate tests so they can run in any order.

Rationale. Shared state causes order-dependent and flaky tests. Isolation makes failures reproducible and parallelization safe.

Example. Use in-memory databases or fakes per test instead of a shared test database.

Anti-pattern / failure mode. Tests that mutate shared global state or depend on execution order. Review signal: tests pass alone but fail in suite.

Related. Deterministic Testing (#75), FIRST (#72), snapshot/restore for tests (Memento).

75. Deterministic Testing

Definition. The same conditions should produce predictable test results; eliminate flakiness.

Rationale. Flaky tests erode trust and waste time. Determinism makes failures meaningful and CI reliable.

Example. Avoid time-based assertions and network calls; use fixed clocks and fakes.

Anti-pattern / failure mode. Tests depending on real time, randomness, or network. Review signal: tests that fail intermittently without code changes.

Related. Test Isolation (#74), FIRST (#72), in-memory fakes (Hexagonal).

76. Continuous Integration Principle

Definition. Integrate and validate changes frequently; small, frequent integrations surface conflicts early.

Rationale. Long-lived branches diverge and merge painfully. Frequent integration with automated validation keeps the mainline healthy.

Example. Merge small PRs daily with CI running tests on each push.

Anti-pattern / failure mode. Long-lived feature branches merged rarely; “integration hell.” Review signal: large, risky merges.

Related. Small Batch (#87), Feedback Loop (#88), enforced module boundaries (Modular Monolith).

77. Regression Testing

Definition. Ensure new changes do not break existing functionality; protect prior behavior with automated tests.

Rationale. Without regression tests, fixes and features reintroduce old bugs. Automated coverage preserves behavior across changes.

Example. Add a test reproducing a bug before fixing it, so it stays fixed.

Anti-pattern / failure mode. Manual regression checks; fixing bugs without tests. Review signal: the same bug recurring.

Related. Continuous Integration (#76), Test Pyramid (#70).