← Home

Test the Invariant, Not the Happy Path

Sep 30, 2026

“A customer can place an order” is a useful test. It is not the rule that keeps a payment system correct.

The stronger rule might be: no captured payment is associated with more than one order, and every accepted order has exactly one durable payment state. That invariant remains meaningful when a request is retried, a worker crashes, or input arrives in an unexpected order.

Let the rule choose the cases

Once an invariant is written, the cases become less arbitrary:

given one payment intent
when two identical checkout requests race
then at most one order is created
and both callers learn the same terminal result

The test is no longer attached to an implementation detail such as “click this button.” It is checking a property that must survive alternate paths. Property-based testing can generate inputs for suitable domains; targeted concurrency and failure-injection tests do the same work where random data is not enough.

Avoid vague invariants such as “the system remains consistent.” Name observable facts: balances never become negative; a document revision only increases; an unauthorized user never receives a tenant-scoped response; a retry cannot create a second effect.

Examples still matter

An invariant will not tell you whether a checkout label is comprehensible or a tax calculation matches a jurisdiction's published formula. Keep concrete examples for product behavior. Use invariants where the risk is a relationship across many executions rather than one expected output.

References

Final take

Happy paths demonstrate a feature. Invariants state what failures, retries, and creativity are not allowed to break.

Changelog