writing-foundry-tests

Write Foundry tests for Solidity contracts with naming, fuzzing, and cheatcode patterns.

16|4|Updated Mar 22, 2026
One-click install
npx skills add https://github.com/idchain-world/id-agents --skill writing-foundry-tests
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: writing-foundry-tests
Source: https://github.com/idchain-world/id-agents/tree/main/configs/agents/foundry-dev/skills/writing-foundry-tests
Command: npx skills add https://github.com/idchain-world/id-agents --skill writing-foundry-tests

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Writing robust Foundry tests for Solidity contracts can be challenging without clear patterns and best practices. This Skill provides a structured guide to help engineers write deterministic tests, use cheatcodes correctly, and organize tests with invariant checks and fuzzing.

Core Features & Use Cases

  • Enforces test naming conventions (test_, testFuzz_, invariant_) and isolation via setUp to ensure reliable CI outcomes.
  • Demonstrates practical cheatcodes usage (makeAddr, deal, vm.prank, vm.expectRevert) and pattern-driven test patterns for readability.
  • Covers invariants and handler-based fuzzing to catch deep edge cases and maintain global contract health.

Quick Start

Create a new Foundry test file following the conventions shown, then run forge test to validate contract behavior.

Frequently Asked Questions about writing-foundry-tests

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write Foundry tests for Solidity contracts using fuzzing and invariants?

To write Foundry tests, use testFuzz_ for fuzzing and invariant_ prefixes to enforce global contract health. Apply handler-based fuzzing to catch deep edge cases and maintain contract state integrity.

What are the standard naming conventions for Foundry test functions?

Standard Foundry test naming conventions require test_ for unit tests, testFuzz_ for fuzz tests, and invariant_ for invariant checks. Following these prefixes ensures readable testing patterns and reliable CI outcomes.

How do I use Foundry cheatcodes like vm.prank and vm.expectRevert correctly?

Use Foundry cheatcodes like vm.prank to mock caller addresses and vm.expectRevert to assert revert conditions. Apply makeAddr and deal to set up isolated test environments with predictable state modifications.

What is the best way to structure setUp isolation in a Foundry test suite?

Structure Foundry test isolation by implementing the setUp function to initialize fresh contract instances for each test. This ensures deterministic behavior and prevents state leakage across different test cases.

Can I use handler-based fuzzing to test deep edge cases in Solidity smart contracts?

Yes, handler-based fuzzing in Foundry allows you to test deep edge cases by routing random inputs through constrained handler functions. This approach maintains global invariants and catches complex state-dependent vulnerabilities.

Why are my Foundry tests failing in CI despite passing locally?

Foundry tests often fail in CI due to non-deterministic state or improper fuzzing bounds. Enforce strict test isolation via setUp, constrain fuzzing inputs, and use vm.expectRevert to ensure safe revert expectations.