Explain the basic structure of a Foundry test and how to run tests.

Beginner

Answer

Foundry tests are written in Solidity and follow a specific naming convention. Test functions must start with test and test contracts should inherit from Test:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
import "forge-std/Test.sol";
import "../src/Counter.sol";
contract CounterTest is Test {
    Counter public counter;
    function setUp() public {
        counter = new Counter();
    }
    function testIncrement() public {
        counter.increment();
        assertEq(counter.number(), 1);
    }
}

Run tests using:

forge test                    # Run all tests
forge test --match-test testIncrement  # Run specific test
forge test -vvv              # Verbose output