Testing Contracts
Test smart contracts with Foundry for RISE
Foundry includes a powerful testing framework that lets you write tests in Solidity. Tests run extremely fast compared to JavaScript-based testing frameworks.
Write Tests
Create Test File
Create a test file in the test/ directory:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import {Test, console} from "forge-std/Test.sol";
import {Counter} from "../src/Counter.sol";
contract CounterTest is Test {
Counter public counter;
function setUp() public {
counter = new Counter();
}
function test_InitialValue() public view {
assertEq(counter.number(), 0);
}
function test_Increment() public {
counter.increment();
assertEq(counter.number(), 1);
}
function test_SetNumber() public {
counter.setNumber(42);
assertEq(counter.number(), 42);
}
function testFuzz_SetNumber(uint256 x) public {
counter.setNumber(x);
assertEq(counter.number(), x);
}
}Run Tests
Execute your tests:
forge testOutput:
Running 4 tests for test/Counter.t.sol:CounterTest
[PASS] test_InitialValue() (gas: 5453)
[PASS] test_Increment() (gas: 28334)
[PASS] test_SetNumber() (gas: 28312)
[PASS] testFuzz_SetNumber(uint256) (runs: 256, μ: 27564, ~: 28387)
Test result: ok. 4 passed; 0 failed; finished in 10.23msVerbose Output
See detailed test output with -v flags:
forge test -vv # Show logs
forge test -vvv # Show execution traces
forge test -vvvv # Show full traces including setupGas Reports
Generate gas usage reports:
forge test --gas-reportRun Specific Tests
Run a single test file:
forge test --match-path test/Counter.t.solRun tests matching a pattern:
forge test --match-test test_IncrementRun tests in a specific contract:
forge test --match-contract CounterTestFuzz Testing
Foundry automatically runs fuzz tests on functions prefixed with testFuzz_:
function testFuzz_SetNumber(uint256 x) public {
counter.setNumber(x);
assertEq(counter.number(), x);
}Configure fuzz runs in foundry.toml:
[fuzz]
runs = 1000Cheatcodes
Foundry provides cheatcodes for testing advanced scenarios:
// Set block timestamp
vm.warp(1641070800);
// Set msg.sender
vm.prank(address(0x1234));
// Expect a revert
vm.expectRevert("Error message");
// Deal ETH to an address
vm.deal(address(0x1234), 1 ether);