Skip to content
Back to Curriculum
ADVANCED

Testing & Verification

Smart contracts are immutable. Bugs are forever. Testing is the only defense. Modern testing involves Unit Tests (Foundry/Hardhat), Fuzzing (random inputs), and Formal Verification (proving math).

Syntax Structure

Example.sol
Loading...

Key Components

  • Boilerplate structure required by the Solc compiler.
  • Placement of state variables versus functions.

Practical Implementation

A comprehensive test suite for a Vault.

Practical.sol
Loading...

Core Concepts

Unit Test

Testing one function in isolation.

Fuzzing

Throwing millions of random inputs at a function to find crashes.

Invariant

A property that must ALWAYS be true (e.g. Total Solvency).

Mental Model

The Crash Test Dummy

Testing is putting your car (contract) in a facility and smashing it into walls, dropping it from cranes, and flooding it with water (Fuzzing). If it survives the torture chamber, it might survive the Public Mempool.

Under the Hood

Foundry tests are actually solidity contracts running in a local EVM. Specific cheatcodes (`vm.`) allow you to manipulate time, block number, and even other addresses.

STATICCALL (vm)

Gas Analysis

Gas reporting in tests helps optimization.

  • • Use `forge test --gas-report` to see which functions consume the most gas.

Security Risks

100% coverage does not mean 100% secure.

  • ! Tests only check what you tell them to check. Logic flaws (correct implementation of bad ideas) are rarely caught by tests.

Common Use Cases

Catching edge cases.
Proving invariants (e.g. 'Token supply never exceeds 1M').
Simulating mainnet forks.

Interactive Visualization

See exactly how the EVM handles this code in memory, storage, and the stack.

Interactive Simulation
0 / 0
Loading...
Type Solidity code (v0.8.26) and click Run. Compiler loads on first use (~2MB).

Click Run to execute the simulation

Then use the step controls to navigate