ALWAYS load before writing or modifying Foundry test files (.t.sol). Covers fuzz testing, gas benchmarks, naming conventions, and test patterns.
bound() over vm.assume - Prefer bound(value, min, max) to constrain inputs; vm.assume discards runs and wastes iterations// Good: bound with max range
amount = bound(amount, 1, type(uint96).max);
// Avoid: vm.assume wastes fuzz runs
vm.assume(amount > 0 && amount < type(uint96).max);
_gas suffix - Enables filtering with forge test --mt gastest_foo_gas() calls test_foo(fixed, values) for deterministic gas measurementfunction test_stake_gas() public returns (uint256 depositId) {
depositId = test_stake(address(this), 1 ether, OPERATOR, 0, address(this));
}
function test_stake(
address depositor,
uint96 amount,
address operator,
uint256 commissionRate,
address beneficiary
) public givenOperator(operator, commissionRate) returns (uint256 depositId) {
// Implementation...
}
givenOperator(operator, rate), givenSpaceHasPointedToOperator(space, operator)contract MyContractHarness is MyContractBase {
function internalFunction(uint256 x) external pure returns (uint256) {
return _internalFunction(x);
}
}
vm.expectEmitvm.expectRevert// Event verification
vm.expectEmit(address(contract));
emit SomeEvent(expectedArg1, expectedArg2);
contract.someFunction();
// Revert verification
vm.expectRevert(SomeError.selector);
contract.shouldRevert();
// Storage slot verification (EIP-7201)
function test_storageSlot() public pure {
bytes32 slot = keccak256(
abi.encode(uint256(keccak256("namespace.storage")) - 1)
) & ~bytes32(uint256(0xff));
assertEq(slot, MyStorage.STORAGE_SLOT);
}
| Pattern | Usage |
| -------------------------------------- | --------------------------------------- |
| test_functionName(params) | Fuzz test (default when has parameters) |
| test_functionName_gas() | Gas benchmark with fixed inputs |
| test_functionName_revertIf_Condition | Revert condition tests |
| test_functionName_EdgeCase | Specific edge case tests |
To compare gas usage before/after code changes:
# 1. Stash source changes (keep test changes)
git stash push -m "optimization" -- src/path/to/Changed.sol
# 2. Run baseline snapshot (--mt gas filters to _gas suffix tests)
forge snapshot --mc TestContract --mt gas
# 3. Pop stash to restore changes
git stash pop
# 4. Run diff against baseline
forge snapshot --mc TestContract --mt gas --diff
This workflow:
_gas test functions while measuring the old implementationnpx skills add towns-protocol/foundry-testing下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer