Get the FREE Ultimate OpenClaw Setup Guide →

solidity-testing

Scanned
npx machina-cli add skill 0xlayerghost/solidity-agent-kit/solidity-testing --openclaw
Files (1)
SKILL.md
3.0 KB

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.solMyToken.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

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

Foundry Cheatcodes Quick Reference

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

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

Common Commands

# Run all tests
forge test

# Run specific test function
forge test --match-test test_transfer

# Run specific test contract
forge test --match-contract MyTokenTest

# Verbose output with full trace
forge test -vvvv

# Gas report
forge test --gas-report

# Fuzz with more runs
forge test --fuzz-runs 10000

# Test coverage
forge coverage

# Coverage with report
forge coverage --report lcov

Source

git clone https://github.com/0xlayerghost/solidity-agent-kit/blob/main/skills/solidity-testing/SKILL.mdView on GitHub

Overview

Defines a disciplined testing standard for Solidity projects using Foundry. It prescribes test organization, naming conventions, coverage expectations, fuzz tests, and cheatcodes to ensure reliable, isolated tests across contracts.

How This Skill Works

Tests live under test/ with one test contract per source contract, named test_<feature>_<scenario> and testFail_<feature>_<scenario>. The approach uses setUp for shared state, vm cheatcodes for assertions, and fuzzing for math heavy flows, plus the coverage and filtering features of Foundry.

When to Use It

  • Creating new Solidity tests for a new contract
  • Ensuring tests stay independent via setUp and isolation
  • Validating permission, boundary, and error paths per coverage rules
  • Adding fuzz tests for math heavy functions
  • Running targeted test suites and generating coverage with forge test and forge coverage

Quick Start

  1. Step 1: Place tests in test/ and name files using the test_ prefix for features and scenarios
  2. Step 2: Implement setUp for shared state and use cheatcodes like vm.expectRevert and vm.expectEmit in tests
  3. Step 3: Run forge test and forge coverage, using --match-test, --match-contract, and --fuzz-runs as needed

Best Practices

  • One test contract per source contract
  • Name tests with test_<feature>_<scenario> and testFail_<feature>_<scenario>
  • Initialize shared state in setUp and avoid cross-test dependencies
  • Include fuzz tests for math heavy functions with vm.assume constraints
  • Leverage Foundry cheatcodes for deterministic checks and event verification

Example Use Cases

  • test_transfer_revertsWhenInsufficientBalance
  • test_stake_updatesBalanceCorrectly
  • test_transfer_updatesBalanceAndEmitsEvent
  • testFail_withdrawFromNonOwner
  • testFuzz_transfer_amounts_shouldNotOverflow

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers