Book a free call

Smart Contract Testing: What to Check Before Launch

Smart contract testing finds bugs before real money is at stake. Learn the testing types, the tools teams use, and how testing differs from an audit.

Hertiv engineering teamReviewed by a senior engineer
7 min read
Smart Contract Testing: What to Check Before Launch
On this page

Short answer

Smart contract testing is the process of running a contract through unit, integration, fuzz and invariant tests on a local chain and a testnet before it handles real funds. It matters because a deployed contract cannot easily be changed, and a single bug can drain the funds it holds. Testing catches logic errors early, while an external audit checks the finished contract afterwards.

What is smart contract testing?

Smart contract testing is the process of running a contract through a set of checks — unit tests, integration tests, fuzz tests and invariant tests — on a local chain and a testnet before it is deployed to handle real funds. Each test calls the contract's functions with known inputs and checks that the outputs, balances and events match what the team expects.

Testing is different from an audit. Testing is what the team does itself, continuously, while it is still writing the contract. An audit is an external review of the finished contract by a separate team, and it happens after testing, not instead of it. A contract that has not been tested is not ready for an audit.

The goal is simple: find the bug while it is still cheap to fix. A logic error found in a local test costs an hour to repair. The same error found after deployment can cost the funds the contract holds, because a deployed contract cannot easily be changed. For the wider picture, see how smart contract security works.

Why does testing matter more for smart contracts?

Testing matters more for smart contracts than for ordinary software because a deployed contract is hard to change and it often holds real money. A web app with a bug can be patched in minutes and the data stays where it is. A contract with a bug may need a new deployment, a migration of funds and a public explanation, and the bug may already have been used by then.

The second reason is that contract code runs in public. Anyone can read a deployed contract and its past transactions, so an attacker has the same information as the team, plus the time to study it. A bug the team missed is a bug an attacker can find at leisure, which is why testing has to cover the paths an attacker would try, not just the paths a normal user takes.

The third reason is that contracts call each other. A lending contract, a token and an exchange can interact in ways no single team predicted, and a flaw in one can drain the others. Testing must therefore cover the contract's interactions, not only its own functions in isolation.

What types of tests should a contract pass?

A contract should pass four types of tests before launch, each catching a different kind of error. Unit tests check single functions, integration tests check contracts working together, fuzz tests throw random inputs at the contract, and invariant tests check that rules which must always hold — such as the total supply never changing without a mint — actually hold.

Test typeWhat it checksWhat it catches
Unit testOne function with known inputsWrong maths and missed edge cases
Integration testContracts calling each otherBroken assumptions between contracts
Fuzz testRandom inputs, thousands of runsInputs the team never thought of
Invariant testRules that must always holdBroken accounting and drained balances
Testnet runThe contract on a live test chainGas costs and real network behaviour

Most teams run unit and integration tests on every change, and fuzz and invariant tests before each release. The testnet run comes last, because it checks the contract against a real network with real timing and real gas costs. A contract that passes all five has still not proved it is safe, but it has proved the team looked for the common failures.

Which tools do teams use to test contracts?

Teams test contracts with development frameworks that compile the code, run tests on a local chain and deploy to a testnet. The two most used are Hardhat, which runs on JavaScript and suits teams already building web frontends, and Foundry, which runs on Solidity and suits teams that want fast fuzz and invariant testing. Both are well-known tools in use today.

The local chain is where most testing happens. It starts in seconds, lets the team control the clock and the balances, and makes it easy to replay a failing transaction step by step. A testnet comes next: it is a public chain with free test tokens, where the team checks the contract against real network timing, real gas prices and other live contracts.

Debugging tools complete the set. A transaction tracer shows each internal call a transaction made, a coverage report shows which lines of code the tests never reached, and a gas report shows which functions cost the most. Lines with no test coverage are where the next bug usually hides, so teams treat coverage as a checklist rather than a score. See how Solana teams test and debug contracts for a chain-specific example.

How does testing fit with an audit?

Testing fits with an audit as the step before it: the team tests the contract until its own tests pass, freezes the code, and only then sends it to an external auditor. The auditor reads the code, runs its own tests and writes a report of findings, which the team fixes before deployment. An audit of untested code wastes the auditor's time on bugs the team could have found itself.

The two are not interchangeable. Testing proves the contract does what the team intended, while an audit looks for what the team did not intend — the attack paths, the edge cases and the assumptions that break under pressure. A contract can pass every test and still fail an audit, because tests only check the cases the team thought of.

Budget for both, in order. Testing costs developer time, while an audit costs a fee and takes weeks, so teams that skip testing to save time usually pay for it in audit findings and delayed launches. For what an audit covers, see what a smart contract audit is.

How to run a testing plan in 6 steps

You run a smart contract testing plan by writing the tests alongside the code, not after it, and by treating a failing test as a bug in the contract, not a bug in the test. The plan below fits a small team shipping its first contract, and it scales to larger teams by adding more tests, not more steps.

  1. Write a unit test for every function. Cover the normal path, the failure path and the boundary values, such as zero and the maximum.
  2. Add integration tests for every contract the code calls. Check that balances, allowances and events match after each interaction.
  3. Add fuzz and invariant tests. Let the fuzzer run thousands of random inputs, and state the rules that must never break.
  4. Run the whole suite on every change. A test that only runs before launch is a test that finds bugs too late.
  5. Deploy to a testnet and use the contract as a real user would. Check gas costs, timing and behaviour with other live contracts.
  6. Freeze the code and send it for audit. Fix every finding, re-run the tests, and do not change the code after the audit without re-testing.

Example (hypothetical): a staking contract catches a rewards bug

Example (hypothetical): a team builds a staking contract that should pay 10 tokens per staker per week. One hundred stakers join, so the contract should pay 100 times 10, which is 1,000 tokens each week.

A unit test checks the weekly payout and finds the contract pays 12 tokens per staker instead of 10. That is 100 times 12, which is 1,200 tokens a week, or 200 tokens more than intended. Over ten weeks the extra payout would be 200 times 10, which is 2,000 tokens the team never budgeted for.

The test also shows why the bug happened: the rewards function divided by the number of days in a week using the wrong constant, so each staker's share was too large. The team fixes the constant, re-runs the test, and the payout returns to 1,000 tokens a week. The fix takes an hour because the contract is still on a local chain.

If the bug had reached the main network, the team would have needed a new deployment and a migration of staked funds, and the 2,000 extra tokens would already have been paid out. The test caught a small arithmetic error before it became a real loss.

What are the common mistakes?

The most common mistakes in smart contract testing are testing only the happy path, never running tests on a testnet, and treating a passing audit as proof the contract is safe.

  • Testing only the happy path. Attackers use the failure paths, so test reverts, zero values and maximum values as well.
  • Skipping the testnet. A local chain hides gas costs and network timing, which are where real deployments fail.
  • Treating an audit as a guarantee. An audit finds some bugs, not all of them, and it says nothing about code changed after the audit.
  • Testing after writing. Tests written last are written to pass, while tests written first force the contract to meet the spec.
  • Ignoring coverage. Lines no test reaches are where the next bug hides, so check the coverage report before every release.

Key takeaways

  • Testing is what the team does itself while writing the contract; an audit is the external check afterwards
  • Unit, integration, fuzz and invariant tests each catch a different kind of error
  • A deployed contract is hard to change, so bugs must be found before launch
  • Test on a local chain first, then on a testnet, then freeze the code for audit
  • Lines no test reaches are where the next bug usually hides

Frequently asked questions

What is the difference between testing and auditing a smart contract?

Testing is what the development team does itself, writing tests that check the contract behaves as intended. Auditing is an external review by a separate team that looks for attack paths and edge cases the developers missed. Testing comes first; an audit of untested code wastes the auditor's time.

How much testing is enough before launch?

Enough that every function has unit tests, every interaction has integration tests, and fuzz and invariant tests have run before each release. There is no fixed number of tests, but a contract with untested lines or no testnet run is not ready to hold real funds.

What are fuzz tests?

Fuzz tests feed a contract thousands of random inputs and check that it never breaks its own rules. They catch inputs the team never thought of, such as extreme values or unusual call orders. Foundry is a well-known tool that runs fuzz tests for Solidity contracts.

Do I need a testnet if I test locally?

Yes. A local chain hides gas costs, network timing and interaction with other live contracts. A testnet is a public chain with free test tokens, so the team can check the contract behaves correctly under real conditions before deploying to the main network.

Can testing prove a contract is safe?

No. Testing proves the contract behaves as intended in the cases the team thought of. It cannot prove there is no bug in a case nobody imagined. That is why teams test first and then get an external audit, and still monitor the contract after launch.

What tools do teams use to test smart contracts?

Hardhat and Foundry are the two most used frameworks. Hardhat runs on JavaScript and suits teams building web frontends, while Foundry runs on Solidity and suits fast fuzz and invariant testing. Both compile code, run tests on a local chain and deploy to a testnet.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: blockchain systems in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Building a smart contract? Talk to Hertiv.

A senior engineer replies within one business day. Or see our Smart Contract Audit service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to contact@hertiv.com.

Prefer email? Write to contact@hertiv.com
Estimate costBook a call