5- Perfect! Let's start with a beginner-friendly example using a simple Counter Contract in Solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Counter {
uint256 public count;
function increment() public {
count++;
}
}
Contract: We'll define a simple contract named Counter that has a single function increment to increase a counter by 1 Test: We'll write a test script named Counter.test.sol to test the functionality of the increment function.
1- We import necessary utilities like Test from Foundry.
2- We define a test contract CounterTest that inherits from Test.
3- Use assertEq to check if the new value is equal to 1.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "forge-std/Test.sol";
import {Counter} from "../src/Counter.sol";
contract CounterTest is Test {
Counter counter;
function setUp() public {
counter = new Counter();
}
function testIncrement() public {
counter.increment();
assertEq(counter.count(), 1);
}
}
You can also run specific tests by passing a filter to test one function from contract by this command line
forge test --match-test testIncrement