← Back to Docs
Recipe

Unit test patterns

Repeatable structures for fast, isolated tests that ship with confidence.

Arrange, Act, Assert

Every test follows three phases. Build the world, poke it, then inspect the fallout. No shared state between tests — each stands alone.

Test the contract, not the guts

Call public APIs only. If you need to reach inside a module to verify behavior, the interface is wrong. Refactor the interface, not the test.

One concept per test

A test named it_returns_null_when_input_is_empty tells you exactly what broke. Avoid kitchen-sink tests that assert ten unrelated things.

Fakes over mocks

Prefer in-memory implementations that behave like the real thing. Mocks that record call counts couple tests to implementation details. Fakes couple to behavior.

Deterministic by construction

No wall-clock time, no random seeds without injection, no network. Pass clocks and RNGs as parameters so every run produces identical results.

Meridian rule: Tests are documentation. A new engineer should understand the module by reading its test file before touching the source.