Example tests show that selected inputs produce expected outputs. Invariants ask whether an important property remains true across many sequences of actions. The useful starting point is the product promise, not the testing framework. A claim should not be consumed twice, accounted resources should reconcile and an unauthorised actor should not gain a privileged capability. These statements guide tests toward consequences users would actually notice.

Choose properties with a clear failure meaning

Write each invariant in ordinary language before expressing it in code. Specify the relevant state and any legitimate exceptions. A supply relationship may include reserved or burned units; a simplistic equality can fail for a valid reason if the model omits one category. Ask what a counterexample would mean for a user or operator. Avoid properties that merely restate an implementation detail, such as one variable matching another variable updated by the same function. Strong invariants independently describe the behaviour the system is intended to preserve.

Model the actors and allowed actions

Include ordinary users, privileged operators and relevant external components. Decide which actions the test environment can generate and which prerequisites they require. A test that mostly produces invalid calls may explore little useful state despite running many iterations. Use handlers or an equivalent structure appropriate to the framework to create meaningful sequences, while preserving adverse cases. Record assumptions about balances, time and dependencies. The test model should be broad enough to challenge the design without accidentally forbidding the very sequence that could reveal a real defect.

Track independent evidence

Where a property involves cumulative deposits, claims or distributions, maintain test-side accounting that does not simply read back the implementation's own total. This independent model can expose errors hidden by internally consistent but incorrect updates. Review its logic carefully; a mistaken reference model can produce false alarms or miss a shared misunderstanding. Keep the property focused enough that a failure is interpretable. A single enormous invariant covering every feature may be difficult to debug and easier to weaken when it begins producing inconvenient results during development.

Make counterexamples reproducible

Preserve the sequence, seed, configuration and relevant dependency versions when a failure occurs. Reduce the sequence where practical so reviewers can understand the essential cause. Turn a confirmed defect into a focused regression test alongside the broader invariant. Do not dismiss a difficult sequence merely because a normal interface would not generate it; public contracts may be called through other routes. At the same time, distinguish an impossible test environment from a valid contract interaction and document the reasoning when the model itself needs correction.

Use invariants as living requirements

Review the property set when product behaviour changes. A new emergency function or conversion route may create a legitimate exception that needs precise modelling rather than removal of the test. Include invariants in the review brief and explain which important properties remain outside automated coverage. The result is stronger evidence, not a guarantee of complete correctness. Well-chosen invariants help the team explore interactions between features while keeping the question anchored in the promises the contract is supposed to uphold for its users.

TAKE IT FORWARD

The key decisions.

  • Where a property involves cumulative deposits, claims or distributions, maintain test-side accounting that does not simply read back the implementation's own total.
  • Preserve the sequence, seed, configuration and relevant dependency versions when a failure occurs.
  • Review the property set when product behaviour changes.

Sources & further reading

Primary references for the concepts discussed. Check the applicable documentation when planning implementation.

  1. Foundry testing workflows