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
Key Components
- Boilerplate structure required by the Solc compiler.
- Placement of state variables versus functions.
Practical Implementation
A comprehensive test suite for a Vault.
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.
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
Interactive Visualization
See exactly how the EVM handles this code in memory, storage, and the stack.
Click Run to execute the simulation
Then use the step controls to navigate