solidity-testing
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTesting 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.solMyToken.t.sol - File location: All tests in directory
test/ - Naming: for passing tests,
test_<feature>_<scenario>for expected revertstestFail_<feature>_<scenario>- Example:
test_transfer_revertsWhenInsufficientBalance - Example:
test_stake_updatesBalanceCorrectly
- Example:
- Independence: Each test must run in isolation — use for shared state, no cross-test dependencies
setUp() - Filtering: Support and
--match-testfor targeted runs--match-contract
- 每个源合约对应一个测试合约:→
MyToken.solMyToken.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:
| Scenario | What to verify |
|---|---|
| Happy path | Standard input → expected output, correct state changes |
| Permission checks | Unauthorized caller → |
| Boundary conditions | Zero values, max values ( |
| Failure scenarios | Every |
| State changes | Storage updates, balance changes, event emissions ( |
| Edge cases | Empty arrays, duplicate calls, self-transfers |
每个核心函数的测试必须覆盖以下场景:
| 场景 | 验证内容 |
|---|---|
| 正常路径 | 标准输入→预期输出,状态变更正确 |
| 权限检查 | 未授权调用者→使用 |
| 边界条件 | 零值、最大值( |
| 失败场景 | 每一条 |
| 状态变更 | 存储更新、余额变更、事件触发( |
| 边缘情况 | 空数组、重复调用、自转账 |
Foundry Cheatcodes Quick Reference
Foundry Cheatcodes 快速参考
| Cheatcode | Usage |
|---|---|
| Next call from |
| All calls from |
| Set |
| Set |
| Set ETH balance |
| Next call must revert with specific error |
| Verify event emission (topic checks) |
| Track storage reads/writes |
| Create labeled address for readable traces |
| Cheatcode | 用途 |
|---|---|
| 下一次调用以 |
| 所有后续调用均以 |
| 设置 |
| 设置 |
| 设置ETH余额 |
| 下一次调用必须回滚并返回指定错误 |
| 验证事件触发(主题检查) |
| 跟踪存储的读写操作 |
| 创建带标签的地址,便于查看调用轨迹 |
Fuzz Testing Rules
模糊测试规则
- All math-heavy and fund-flow functions must have fuzz tests
- Pattern:
function testFuzz_<name>(uint256 amount) public - Use to constrain inputs:
vm.assume()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
undefinedbash
undefinedRun 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
undefinedforge coverage --report lcov
undefined