Testing smart contracts using Foundry
In this tutorial, you’ll learn how to test your smart contracts using Foundry, the toolchain for smart contract development.Objectives
By the end of this tutorial, you should be able to:- Understand the increased importance of testing in smart contract development
- Write and execute tests written in Solidity using the Forge Standard Library with Foundry
- Use the
cheatcodes
that Foundry provides to test your smart contracts
Overview
Testing is a crucial aspect of smart contract development, ensuring the reliability and security of your code. Because it is impossible to patch a smart contract after deployment, you must thoroughly and completely test your code. Foundry provides a robust testing framework that allows developers to create comprehensive test suites for their projects using Solidity.My First Test with Foundry
Consider the default test that theforge init hello_foundry_in_base
command provides in the seed Foundry project.
- Foundry test files are named following the pattern:
<ContractName>.t.sol
- Smart contract test files are named following the pattern:
<ContractName>Test
- All tests inherit from
forge-std/Test.sol
. - All tests contain a public function called
setUp
, which is executed before each test. This is similar to thebeforeEach
hook in the Mocha/Typescript world. - Test cases start with the
test
keyword, for instancetestIncrement
. - Test cases functions are public.
Using Cheatcodes
Foundry includes a set of cheatcodes, which are special instructions that are accessible using thevm
instance in your tests. Cheatcodes allow you to perform various tasks, including:
- Manipulate the state of the blockchain
- Test reverts
- Test events
- Change block number
- Change identity
- And more!
msg.sender
of your tests, and add some console logs via importing the forge-std/console.sol
contract.
The Counter
contract should look as follows:
forge test
, you will see the following:
forge test
command includes a flag that enable you to include more details of the logs emitted during the execution of the tests.
You can control that by including different levels of the verbose flag — -vv
up to -vvvvv
. For more details about the level of verbosity you can refer to the Logs and Traces section of the Foundry documentation.
Run the foundry test -vv
. You should see:
prank
cheatcode, which allow you to modify the msg.sender
of the next transaction. You will also use the addr
cheatcode, which allow you to generate an address using any private key, which can simply be a hex number.
Include some console.log
statements to understand better the execution flow.
The code should look like:
forge test -vv
command, you should see:
vm.prank
before the call to the counter.increment()
and counter.setNumber(x)
functions. This allows you to specify a particular address to become the msg.sender
in the contract. Since the vm.prank
accepts an address, you simply generate an address using the cheatcode vm.addr
, where you pass a simple hexadecimal number, which is a valid private key.