Member-only story
How to Test Ethereum Smart Contracts
Test your Smart Contracts with Solidity and JavaScript

Prerequisites: A basic understanding of Blockchain, Ethereum and Javascript.
The full working project code can be found on Github.
The Importance of Software Testing
If you want code to work the way it’s intended to, software testing is paramount.
There are two general types of software test: unit tests and integration tests.
- Unit tests focus on each function in isolation.
- Integration tests focus on ensuring multiple parts of the code work together as expected.
Blockchain software is no different. It might be argued that Blockchain applications require more emphasis on testing, due to immutability.
Blockchain Testing
Truffle suite gives us two avenues for testing solidity smart contracts: solidity tests and JavaScript tests. The question is, which should we use?
The answer is both.

Solidity tests
Writing tests in Solidity gives us the ability to run Blockchain layer tests. They allow tests to call contracts and functions as if they were on the Blockchain themselves. To test the internal behaviour of smart contracts we can:
- Write unit tests to check function return values and state variable values.
- Write integration tests that test the interactions between contracts. These ensure that mechanisms such as inheritance and dependency injection are functioning as expected.
JavaScript tests
We also need to make sure that smart contracts exhibit the right external behavior. To test smart contracts from outside the Blockchain, we use Web3js, just as our DApp would. We need to have confidence that our DApp front end will work properly when calling the smart contracts. These fall under integration tests.