Running a Basic Forge Test to Test a Smart Contract

Basics

This assumes you have already setup foundry on your computer or server.

Forge can be used to test smart contracts, here we will look at a simple contract, and use Forge to test the smart contract.

forge init

Go to the folder where you want to add your project that you want to make or test with Forge, e.g.

mkdir projects
cd projects
forge init NumberStore

Forge will now have made the folder NumberStore with various folders for testing including
/test/
and
/src/

At the time of writing this forge automatically creates and example contract and a test contract, namely
src/Counter.sol
test/Counter.t.sol

forge test

Now go to test/ and run

forge test

You should see the successful output of the test, showing the tests that have been passed.

Next steps

Create your own source files in the src folder, and tests in the test folder.

Optionally delete the exmaple contract tests, or keep them there for reference. Note that depending on your syntax that you use in tests these tests may also be run along with your tests.

back to home