Solidity theorytheory 0/50 · 0%
Tooling · hard

39. Testing with Foundry

Unit tests, cheatcodes and fuzzing.

Foundry tests are Solidity contracts. `setUp()` runs before each test, and cheatcodes such as `vm.prank`, `vm.warp` and `vm.expectRevert` control the environment.

function testFuzz_Stake(uint96 amt) public {
    vm.assume(amt > 0);
    vm.prank(alice);
    staking.stake(amt);
    assertEq(staking.balanceOf(alice), amt);
}

Any parameterised test is fuzzed automatically; add invariant tests for properties that must hold after any sequence of calls.

Check your understanding

  1. 1. What does `vm.prank` do?

  2. 2. How do you fast-forward time?

  3. 3. What makes a Foundry test fuzzed?