β How to Write Basic Tests
Test Setup

Test for error

vm.expectRevert

Gas Report

Last updated




Last updated
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract Counter {
uint public count;
// Function to get the current count
function get() public view returns (uint) {
return count;
}
// Function to increment count by 1
function inc() public {
count += 1;
}
// Function to decrement count by 1
function dec() public {
// This function will fail if count = 0
count -= 1;
}
}
// SPDX-License-Identifier: UNLICENSED
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 testInc() public {
counter.inc();
assertEq(counter.count(), 1);
}
}function testFailDec() public {
counter.dec();
}function testDecUnderflow() public {
vm.expectRevert(stdError.arithmeticError);
counter.dec();
}function testDec() public {
counter.inc();
counter.inc();
counter.dec();
assertEq(counter.count(), 1);
}forge test --match-path test/Counter.t.sol --gas-report