✅Introduction
Last updated
Last updated
Install foundry toolchain:
If everything goes well, you will now have four binaries at your disposal: forge
, cast
, anvil
, and chisel
.
To update foundryup
after installation, simply run foundryup
again, and it will update to the latest Foundry release. You can also revert to a specific version of Foundry with foundryup -v $VERSION
.
Start a new project:
Suppose we named the project "hello_foundry", then the project layout is:
Build the prject:
Test the project:
Install dependency, such as solmate:
The dependency is going to be installed in the /lib
directory.
Forge can remap dependencies to make them easier to import. Forge will automatically try to deduce some remappings for you:
These remappings mean:
To import from forge-std
we would write: import "forge-std/Contract.sol";
To import from ds-test
we would write: import "ds-test/Contract.sol";
To import from solmate
we would write: import "solmate/Contract.sol";
To import from weird-erc20
we would write: import "weird-erc20/Contract.sol";
You can customize these remappings by creating a remappings.txt
file in the root of your project.
Update dependency:
Remove dependency:
Forge will look for the tests anywhere in your source directory. Any contract with a function that starts with test
is considered to be a test. Usually, tests will be placed in test/
by convention and end with .t.sol
.
Run all tests:
Run a specific test:
Inverse versions of these flags also exist (--no-match-contract
and --no-match-test
).
Match a glob pattern:
The inverse of the --match-path
flag is --no-match-path
.
Verbosity:
Level 2 (-vv
): Logs emitted during tests are also displayed. That includes assertion errors from tests, showing information such as expected vs actual.
Level 3 (-vvv
): Stack traces for failing tests are also displayed.
Level 4 (-vvvv
): Stack traces for all tests are displayed, and setup traces for failing tests are displayed.
Level 5 (-vvvvv
): Stack traces and setup traces are always displayed.
Copy the contract from solidity-by-example.org:
Compile it:
Create a test file HelloWorld.t.sol
. Copy and paste the content of Counter.t.sol
into this new test file and write our own test cases:
Note that setup()
will be executed before executing each test case, and test case function name must start with the test
prefix.
Run test:
Note that forge test
would run all test files in the test/
directory. If you only want to run a single test file:
Let's make the test fail:
Run test with verbosity marks:
You can see why the test case failed in the Traces
output.