forge-test - Run the project's tests.
forge test
[options]
Run the project's tests.
It is possible to run the tests in a forked environment by passing --fork-url <URL>
.
When the tests are running in a forked environment, you can access all the state of the forked chain as you would if you had deployed the contracts. Cheatcodes are still available.
You can also specify a block number to fork from by passing --fork-block-number <BLOCK>
. When forking from a
specific block, the chain data is cached to ~/.foundry/cache
. If you do not want to cache the chain data,
pass --no-storage-caching
.
Traces that cannot be decoded by local contracts when running in a forked environment (e.g. calls to
contracts that live on mainnet, like tokens) can optionally be decoded using Etherscan. To use Etherscan
for trace decoding, set ETHERSCAN_API_KEY
or pass --etherscan-api-key <KEY>
.
It is possible to run a test in an interactive debugger. To start the debugger, pass --debug <TEST>
.
If multiple tests match the specified pattern, you must use other test filters in order to reduce the matching number of tests to exactly 1.
If the test is a unit test, it is immediately opened in the debugger.
If the test is a fuzz test, the fuzz test is run and the debugger is opened on the first failing scenario. If there are no failing scenarios for the fuzz test, the debugger is opened on the last scenario.
More information on the debugger can be found in the debugger chapter.
You can generate a gas report by passing --gas-report
.
More information on gas reports can be found in the gas reports chapter.
It is possible to list the tests without running them.
You can pass --json
to make it easier for outside extensions to parse structured content.
{{#include test-options.md}}
{{#include evm-options.md}}
{{#include executor-options.md}}
{{#include core-build-options.md}}
{{#include watch-options.md}}
{{#include ../common/display-options.md}}
--list
List tests instead of running them.
{{#include common-options.md}}
-
Run the tests:
forge test
-
Open a test in the debugger:
forge test --debug testSomething
-
Generate a gas report:
forge test --gas-report
-
Only run tests in
test/Contract.t.sol
in theBigTest
contract that start withtestFail
:forge test --match-path test/Contract.t.sol --match-contract BigTest \ --match-test "testFail*"
-
List tests in desired format
forge test --list forge test --list --json forge test --list --json --match-test "testFail*" | tail -n 1 | json_pp