Tests
forge test --summary
forge test -vvv
forge test --match-test <SPECIFIC_TEST_FUNCTION_NAME>
forge test --match-contract <SPECIFIC_CONTRACT_NAME>
forge test --debug <SPECIFIC_TEST_FUNCTION_NAME> # h to show/hide helpTest
Description
Writing Tests
import "forge-std/Test.sol";pragma solidity 0.8.10;
import "forge-std/Test.sol";
contract ContractBTest is Test {
uint256 testNumber;
string string1;
string string2;
// An optional function invoked before each test case is run.
function setUp() public {
testNumber = 42;
string1 = ABC;
string2 = XYZ;
}
// Functions prefixed with test are run as a test case.
function test_NumberIs42() public {
assertEq(testNumber, 42);
}
// Inverse test prefix - if the function does not revert, the test fails.
function testFail_Subtract43() public {
testNumber -= 43;
}
function test_CompareStrings() public {
assertEq(
keccak256(abi.encodePacked(string1)),
keccak256(abi.encodePacked(string2))
);
}
}
Naming Convention
Shared test setups
Custom Error Reverts
Event Testing
Unit Testing
Integration Testing
Forked Testing
Staging Testing
Fuzz Testing
Coverage
Coverage line highlighting
Last updated
