0Pricing
Web3 & DApp Development Fundamentals · Lesson

forge Testing

Solidity tests.

Tests Are Contracts

In Foundry a test file is a Solidity contract that inherits from Test in the forge-std library. Each public function whose name starts with test is run as an individual test case.

Test files live in test/ and conventionally use the .t.sol suffix.

import {Test} from "forge-std/Test.sol";

contract CounterTest is Test {
    function testInitialZero() public {
        // ...
    }
}

The setUp Function

The special setUp() function runs before every test, giving each test a fresh, isolated state. Deploy your contracts and seed any fixtures here.

Because state resets between tests, one test can never accidentally depend on another.

Counter counter;

function setUp() public {
    counter = new Counter();
}

All lessons in this course

  1. Foundry vs Hardhat
  2. forge Testing
  3. Fuzzing and Invariants
  4. cast and anvil
← Back to Web3 & DApp Development Fundamentals