Forge Standard Library (Forge Std for short) is a collection of helpful contracts that make writing tests easier, faster, and more user-friendly.
Using Forge Std is the preferred way of writing tests with Foundry.
What's included:
-
Vm.sol: Up-to-date cheatcodes interfaceimport "forge-std/Vm.sol";
-
console.solandconsole2.sol: Hardhat-style logging functionalityimport "forge-std/console.sol";
Note:
console2.solcontains patches toconsole.solthat allow Forge to decode traces for calls to the console, but it is not compatible with Hardhat.import "forge-std/console2.sol";
-
Script.sol: Basic utilities for Solidity scriptingimport "forge-std/Script.sol";
-
Test.sol: The complete Forge Std experience (more details below)import "forge-std/Test.sol";
The Test contract in Test.sol provides all the essential functionality you need to get started writing tests.
Simply import Test.sol and inherit from Test in your test contract:
import "forge-std/Test.sol";
contract ContractTest is Test { ...What's included:
-
Std Libraries
- Std Logs: Expand upon the logging events from the DSTest library.
- Std Assertions: Expand upon the assertion functions from the DSTest library.
- Std Cheats: Wrappers around Forge cheatcodes for improved safety and DX.
- Std Errors: Wrappers around common internal Solidity errors and reverts.
- Std Storage: Utilities for storage manipulation.
- Std Math: Useful mathematical functions.
- Script Utils: Utility functions which can be accessed in tests and scripts.
- Console Logging: Console logging functions.
-
A cheatcodes instance
vm, from which you invoke Forge cheatcodes (see Cheatcodes Reference)vm.startPrank(alice); -
All Hardhat
consolefunctions for logging (see Console Logging)console.log(alice.balance); // or `console2`
-
All Dappsys Test functions for asserting and logging (see Dappsys Test reference)
assertEq(dai.balanceOf(alice), 10000e18);
-
Utility functions also included in
Script.sol(see Script Utils)// Compute the address a contract will be deployed at for a given deployer address and nonce address futureContract = computeCreateAddress(alice, 1);