Skill 6.1.1

Explain the role of unit tests and local test networks

Key Concepts

A unit test checks one behaviour of one contract, such as "withdraw reverts for a caller with no balance", in isolation and in a known starting state. Integration tests check several contracts together, often against real protocols on a forked mainnet, and end-to-end tests exercise a DApp's front end with a wallet.

Unit tests run on a local test network: Hardhat's in-process simulated network, Anvil, or, in older material, Ganache. These networks mine instantly, provide funded accounts, can take snapshots and revert to them, and can manipulate time, so thousands of tests run in seconds without cost. Public testnets are slow, need faucet ether and share state with others, so they are used only for final checks of a deployment.

Good contract tests cover the expected behaviour, every revert condition, access control for every privileged function, boundary values such as zero and maximum amounts, event emission and the movement of ether and tokens.