solidity-testing

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Testing Standards

测试标准

Language Rule

语言规则

  • Always respond in the same language the user is using. If the user asks in Chinese, respond in Chinese. If in English, respond in English.
  • 始终使用与用户相同的语言进行回复。如果用户用中文提问,就用中文回复;如果用英文,就用英文回复。

Test Organization

测试组织

  • One test contract per source contract:
    MyToken.sol
    MyToken.t.sol
  • File location: All tests in
    test/
    directory
  • Naming:
    test_<feature>_<scenario>
    for passing tests,
    testFail_<feature>_<scenario>
    for expected reverts
    • Example:
      test_transfer_revertsWhenInsufficientBalance
    • Example:
      test_stake_updatesBalanceCorrectly
  • Independence: Each test must run in isolation — use
    setUp()
    for shared state, no cross-test dependencies
  • Filtering: Support
    --match-test
    and
    --match-contract
    for targeted runs
  • 每个源合约对应一个测试合约
    MyToken.sol
    MyToken.t.sol
  • 文件位置:所有测试文件放在
    test/
    目录下
  • 命名规范:通过的测试命名为
    test_<功能>_<场景>
    ,预期会回滚的测试命名为
    testFail_<功能>_<场景>
    • 示例:
      test_transfer_revertsWhenInsufficientBalance
    • 示例:
      test_stake_updatesBalanceCorrectly
  • 独立性:每个测试必须独立运行——使用
    setUp()
    设置共享状态,测试之间不得有依赖
  • 过滤支持:支持使用
    --match-test
    --match-contract
    进行定向测试运行

Coverage Requirements

覆盖率要求

Every core function must have tests covering:
ScenarioWhat to verify
Happy pathStandard input → expected output, correct state changes
Permission checksUnauthorized caller →
vm.expectRevert
with correct error
Boundary conditionsZero values, max values (
type(uint256).max
), off-by-one
Failure scenariosEvery
require
/
revert
/ custom error path
State changesStorage updates, balance changes, event emissions (
vm.expectEmit
)
Edge casesEmpty arrays, duplicate calls, self-transfers
每个核心函数的测试必须覆盖以下场景:
场景验证内容
正常路径标准输入→预期输出,状态变更正确
权限检查未授权调用者→使用
vm.expectRevert
验证正确的错误信息
边界条件零值、最大值(
type(uint256).max
)、差一错误
失败场景每一条
require
/
revert
/ 自定义错误路径
状态变更存储更新、余额变更、事件触发(
vm.expectEmit
边缘情况空数组、重复调用、自转账

Foundry Cheatcodes Quick Reference

Foundry Cheatcodes 快速参考

CheatcodeUsage
vm.prank(addr)
Next call from
addr
vm.startPrank(addr)
All calls from
addr
until
vm.stopPrank()
vm.warp(timestamp)
Set
block.timestamp
vm.roll(blockNum)
Set
block.number
vm.deal(addr, amount)
Set ETH balance
vm.expectRevert(error)
Next call must revert with specific error
vm.expectEmit(true,true,false,true)
Verify event emission (topic checks)
vm.record()
/
vm.accesses()
Track storage reads/writes
makeAddr("name")
Create labeled address for readable traces
Cheatcode用途
vm.prank(addr)
下一次调用以
addr
身份执行
vm.startPrank(addr)
所有后续调用均以
addr
身份执行,直到调用
vm.stopPrank()
vm.warp(timestamp)
设置
block.timestamp
vm.roll(blockNum)
设置
block.number
vm.deal(addr, amount)
设置ETH余额
vm.expectRevert(error)
下一次调用必须回滚并返回指定错误
vm.expectEmit(true,true,false,true)
验证事件触发(主题检查)
vm.record()
/
vm.accesses()
跟踪存储的读写操作
makeAddr("name")
创建带标签的地址,便于查看调用轨迹

Fuzz Testing Rules

模糊测试规则

  • All math-heavy and fund-flow functions must have fuzz tests
  • Pattern:
    function testFuzz_<name>(uint256 amount) public
  • Use
    vm.assume()
    to constrain inputs:
    vm.assume(amount > 0 && amount < MAX_SUPPLY);
  • Default runs: 256 — increase to 10,000+ for critical functions:
    forge test --fuzz-runs 10000
  • 所有涉及大量数学运算和资金流转的函数必须进行模糊测试
  • 命名模式:
    function testFuzz_<名称>(uint256 amount) public
  • 使用
    vm.assume()
    约束输入:
    vm.assume(amount > 0 && amount < MAX_SUPPLY);
  • 默认运行次数:256次——关键函数需增加到10000次以上:
    forge test --fuzz-runs 10000

Common Commands

常用命令

bash
undefined
bash
undefined

Run all tests

运行所有测试

forge test
forge test

Run specific test function

运行指定测试函数

forge test --match-test test_transfer
forge test --match-test test_transfer

Run specific test contract

运行指定测试合约

forge test --match-contract MyTokenTest
forge test --match-contract MyTokenTest

Verbose output with full trace

带完整调用轨迹的详细输出

forge test -vvvv
forge test -vvvv

Gas report

Gas报告

forge test --gas-report
forge test --gas-report

Fuzz with more runs

增加模糊测试运行次数

forge test --fuzz-runs 10000
forge test --fuzz-runs 10000

Test coverage

测试覆盖率

forge coverage
forge coverage

Coverage with report

生成覆盖率报告

forge coverage --report lcov
undefined
forge coverage --report lcov
undefined