Intermediate

Testing Smart Contracts — Key concepts

Build confidence before deploying.

Developer Intermediate
3/6 — Testing Smart Contracts — Key concepts

Why it matters Each test type catches different bugs. Unit tests enforce intent; integration tests catch wiring errors; fuzzing hits edge combos; invariants guard the core math/state.

Design lens Make tests read like documentation: assert events/reverts explicitly and cover boundaries. CI should be a gate, not a suggestion.

Adversarial thinking Use cheatcodes/snapshots to simulate malicious actors, time shifts, and balance changes. Test what an attacker would do, not just a user.

Maintenance Keep fixtures/builders DRY so adding new tests is cheap. Track coverage and gas snapshots to catch regressions early.

Key points
  • Test types:Unit (small), integration (multi-contract), fuzz (randomized inputs), invariants (properties that always hold).
  • Assertions:Events, reverts, state diffs, math invariants, and custom errors.
  • Fixtures/builders:Reuse state setup; snapshot/rollback in Hardhat; cheatcodes in Foundry (vm.prank, deal, warp, roll).
  • Coverage:Track critical paths; ensure failures are tested; include boundary values.
  • CI:Run tests on every PR; surface gas changes and coverage; fail fast on lints/formatters.
← Previous section
Next section →