Testing Smart Contracts

Kacper Hernacki
3 min readDec 22, 2022

--

How to test smart contracts, and why even do it?

The very basic step of developing smart contracts to your web3 solutions is testing it before even deploying to testnet. Running tests which check the implemented feature is crucial!

Agenda:

  • intro,
  • what is it?
  • what it does?
  • tools,
  • conclusion.

What is it?

“Smart contract testing means performing detailed analysis and evaluation of a smart contract to assess the quality of its source code during the development cycle.”

In simple and practical words: testing is writing simple variants of using the features and expecting results to be equal to pre defined statements.

Simple example, smart contract is transferring tokens from address to address and it is all.

It is needed to write a tests like this:

  • generate two wallets (for example Alice & Bob),
  • run transfer function from Alice to bob with assets equal 1 ETH,
  • expect Bob wallet to increase value by 1 ETH,
  • expect Alice wallet to decrease by 1 ETH,
  • check if edge cases are handled properly, like a try to transfer more funds that it is owned, etc.

Tests should be described and grouped properly to have a clean view in the console!

What it does?

“Testing a smart contract makes it easier to identify bugs and vulnerabilities and reduces the possibility of software errors that could lead to costly exploits.”

According to above example with Alice and Bob, testing checks implemented features before further deployment. They should throw errors for bugs and allow to identify them easy, if tests are described properly.

Tools

Smart contracts are mainly written in Solidity, but testing is proceeded with JavaScript. Different platforms, solutions offers some easy architecture for testing, one of them is truffle.

More about truffle:

Truffle creates a proper folder structure which is the same for every project, shortly:

  • there is a contract folder in which contracts are stored,
  • and there is tests folder which stores tests.

Tests are written with JavaScript and performed with a library mocha.js or chai.js. Files have an extension “<contractname>.test.js”.

mocha.js:

chai.js docs:

In test file contracts is initiated and then different conditions are checked.

Tests are run with cli command from the terminal level and it is all.

To have contract deployed locally and implement generated wallets with some funds, extremely helpful is ganache from truffle toolkit. It just runs a loca blockchain and created wallets with 100ETH balance.

More about ganache:

Waffle

It is an another smart contract testing tool created by ETHWorks.

“It lets you create new smart contracts and test them using Mocha and Chai. Many people choose Waffle over Truffle, as it is simpler and easier to learn.”

More in:

Conclusion

Testing smart contracts is a powerful and really needed skill, which can prevent loss of huge amount of assets. Blockchain is immutable, if contract is deployed with errors, in the lucky scenario it lost only some features. The worst case is when some user lose funds.

--

--

Kacper Hernacki

💻Passionate full-stack developer ⛓Blockchain enthusiastic 📌Involved in Web 3.0 startup ✉️ hernackikacper@gmail.com