TE

Solidity Testing

Solidity smart contract testing conventions

Details

Language / Topic
soliditySolidity
Category
Testing

Rules

balanced
- Write tests in Solidity using Foundry's `forge-std` library — extend `Test` and use `assertEq`, `assertGt`, `vm.expectRevert` for assertions.
- Name test functions `test_FunctionName_Condition_ExpectedOutcome()` for clarity in test output and CI reports.
- Use `vm.prank(address)` to impersonate accounts and `vm.deal(address, amount)` to set ETH balances in test setup.
- Use `setUp()` to deploy fresh contract instances for each test — never share state between tests to avoid order-dependent failures.
- Write fuzz tests with `function testFuzz_MethodName(uint256 amount) public` and constrain inputs with `vm.assume(amount > 0 && amount < 1e18)`.
- Use `vm.expectEmit(true, true, false, true)` before the action that should emit an event to assert both the event signature and its arguments.
- Test revert paths with `vm.expectRevert(ContractName.CustomError.selector)` to verify that custom errors are thrown in failure conditions.