web3-testing

Test smart contracts comprehensively using Hardhat and Foundry with unit tests, integration tests, and mainnet forking. Use when testing Solidity contracts, setting up blockchain test suites, or validating DeFi protocols.

Author

Install

Hot:7

Download and extract to your skills directory

Copy command and send to OpenClaw for auto-install:

Download and install this skill https://openskills.cc/api/download?slug=sickn33-skills-web3-testing&locale=en&source=copy

Web3 Smart Contract Testing Skills Guide

Skill Overview

Using Hardhat and Foundry testing frameworks to thoroughly master smart contract testing strategies, including unit tests, integration tests, Gas optimization tests, fuzz testing, and mainnet fork testing.

Applicable Scenarios

1. Write Smart Contract Unit Tests

Write comprehensive unit tests for Solidity smart contracts, covering all functional paths and edge cases. Use Hardhat’s Chai assertion library or Foundry’s Solidity test contracts to verify the correctness of contract logic, ensuring the contract behaves as expected under a wide range of input conditions.

2. Set Up a Blockchain Test Suite

Build a complete smart contract testing environment, including a local test network, testnet deployments, and CI/CD integration. Configure the Hardhat or Foundry testing framework, integrate code coverage reporting tools, enable automated test execution, and ensure that every code change promptly reveals potential issues.

3. Validate DeFi Protocol Security

Use mainnet fork testing and fuzz testing techniques to verify the security and reliability of DeFi protocols in real environments. Simulate large transactions, extreme market conditions, and malicious attack scenarios to ensure the protocol correctly handles all edge cases and to mitigate reentrancy attacks and access control vulnerabilities.

Core Capabilities

1. Hardhat Test Framework Configuration

Provide a complete Hardhat test environment configuration, including network forking settings, Gas report generation, Etherscan verification, and code coverage statistics. Use the loadFixture pattern to optimize test performance, use time operations to handle time-locked contract functionality, and apply snapshot/revert mechanisms to maintain test isolation.

2. Advanced Foundry Testing Modes

Master Foundry’s Forge testing framework, leveraging its powerful fuzz testing and fast execution capabilities. Use vm.cheatcodes to simulate various testing scenarios, including account impersonation, time manipulation, and mainnet forking. Write property-based test cases to automatically discover edge cases and potential vulnerabilities in contracts.

3. Gas Optimization and Performance Testing

Validate contract Gas efficiency through comparative tests to identify optimizable code paths. Use hardhat-gas-reporter to generate detailed Gas consumption reports, analyze performance differences across different implementations, and reduce deployment and execution costs while maintaining functional correctness.

Common Questions

Should Smart Contract Testing Use Hardhat or Foundry?

Both have their advantages; the choice depends on project needs and team preferences. Hardhat offers a JavaScript/TypeScript testing environment with a rich ecosystem, making it suitable for projects that need integration with frontend toolchains. Foundry uses Solidity for writing tests, executes extremely fast, includes built-in fuzz testing functionality, and is better suited for scenarios focused on test performance and security auditing. Many projects use both: Hardhat for rapid development testing, and Foundry for deep security verification.

What Code Coverage Level Is Considered Satisfactory for Smart Contract Testing?

A coverage rate of 90% or higher is recommended. However, coverage is only one metric—test quality is more important. Ensure there are test cases covering all critical functionality, access control, state transitions, and error handling. For DeFi protocols involving funds or high value, aim for coverage close to 100% and include comprehensive fuzz testing and mainnet fork testing.

How Do You Fork the Ethereum Mainnet for Testing?

Both Hardhat and Foundry support mainnet forking. In Hardhat, specify the RPC URL and block number via forking parameters; in Foundry, use the vm.createSelectFork() function. After forking, you can interact with real contracts on mainnet (e.g., Uniswap, DAI, USDC) to simulate realistic transaction scenarios. This is especially useful for testing contracts that integrate with existing protocols, allowing you to verify contract behavior in the mainnet environment without using real funds.

    Web3 Smart Contract Testing — Hardhat and Foundry Testing Guide - Open Skills