Lesson 1 of 3 Unit Testing Smart Contracts

Testing Fundamentals and Frameworks

Testing is the main defence against bugs in code that cannot be patched. This lesson explains what to test, why tests run on local networks, and how the Hardhat, Foundry and Remix testing tools compare, with the Truffle equivalents that older material uses.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain why smart contracts need thorough testing.
  • Identify what a unit test suite should cover.
  • Explain why unit tests run on local networks.
  • Compare Hardhat, Foundry, Remix and Truffle testing.

Why contract testing matters more

In most software, a bug found after release can be fixed in the next deployment. A smart contract bug may be exploited within minutes and cannot be patched. Tests are therefore written alongside the code, run on every change and aim to cover not only the intended behaviour but every way a function should fail. The following kinds of test serve different purposes.

Unit test
Checks one behaviour of one contract in a known state, for example that withdraw reverts for a caller with no balance.
Integration test
Checks contracts working together, often against real protocols on a forked mainnet.
Fuzz and invariant tests
Generate inputs and call sequences automatically to find edge cases.
End-to-end test
Drives a DApp's front end with a test wallet against a local or test network.

What to test

A thorough unit test suite for a contract covers the following areas.

  • The expected result of every function, including return values and state changes.
  • Every revert condition, with the specific error expected.
  • Access control: every privileged function called by an account without permission.
  • Boundary values: zero, one, the maximum, and amounts just above and below limits.
  • Events emitted, with their arguments.
  • Movements of ether and tokens between accounts.
  • Time-dependent behaviour such as deadlines and vesting.
Select all that apply

Completing a test plan

Exam skills 6.1.1 · 6.2.2

Choose the tests a staking contract's suite still needs. It already checks that staking updates balances.

Log in to try this activity Interactive activities are free with an account.

Local test networks

Unit tests run on a local, simulated network. Hardhat creates one in memory for each test run; Foundry runs tests in its own EVM; older material used Ganache. Local networks mine instantly, provide funded accounts, can snapshot and restore state, and can manipulate time, so a suite of hundreds of tests runs in seconds. Public testnets are slower, shared and need faucet ether, so they are reserved for final deployment checks.

Comparing frameworks

The following comparison sets out the main testing tools.

Hardhat 3
Test languageSolidity, and JavaScript or TypeScript
Commandnpx hardhat test
JavaScript librariesMocha + Chai + ethers.js, or node:test + viem
StrengthsMixes Solidity and JavaScript tests, rich matchers, network helpers, stack traces
Foundry
Test languageSolidity
Commandforge test
JavaScript librariesNone
StrengthsSpeed, cheatcodes, built-in fuzzing and invariants
Remix
Test languageSolidity
CommandSolidity Unit Testing plugin
JavaScript librariesNone
StrengthsNo installation, quick checks in the browser

Hardhat 3's Solidity tests use the same syntax and cheatcodes as Foundry, so the skills transfer. Truffle, which ConsenSys sunset in 2023, appears in much older material: truffle test ran JavaScript tests written in contract("Name", accounts => { ... }) blocks with Mocha and Chai, and Solidity tests using a truffle/Assert.sol library, against Ganache. Those concepts map directly to Hardhat's describe blocks, its signers and its Solidity tests.

Match the pairs

Older test code, newer tools

Exam skills 6.1.2

Match each element of an older Truffle test with its Hardhat 3 equivalent.

Log in to try this activity Interactive activities are free with an account.
Worked example

What is missing from this test plan?

A team's tests for a token check that the owner can mint, that transfers update balances and that Transfer events are emitted. All tests pass. What important cases are missing?

Failure paths: that a non-owner cannot mint, that transferring more than the balance reverts with the right error, that minting beyond a cap reverts, and that transferFrom without enough allowance reverts. Boundary cases: transfers of zero and of the full balance. The current tests prove the token works for honest users, not that it resists dishonest ones.

Worked example

Translating a Truffle test

An old tutorial's test begins contract("Bank", accounts => { it("...", async () => { const bank = await Bank.deployed(); ... }) }). What are the Hardhat 3 equivalents?

contract() becomes describe(); accounts becomes await ethers.getSigners(); Bank.deployed(), which used the migrated deployment, becomes await ethers.deployContract("Bank") inside a fixture, so each test starts from a fresh, known state; and Ganache is replaced by the network Hardhat creates for the test run.

Check yourself

Why are public testnets a poor place to run unit tests?

Show answer

They are slow (12-second blocks), need faucet ether, keep permanent shared state and cannot be reset or have their time manipulated. Local networks are fast, free and resettable.

Exam Tips

Remember for the exam

  • Test failure paths and permissions as thoroughly as success paths.
  • Run unit tests on local networks: fast, free, resettable.
  • Hardhat 3 runs Solidity and JavaScript tests; Foundry runs Solidity tests with cheatcodes.
  • Truffle's contract() and Ganache map to Hardhat's describe() and in-process network.