I see the truffle have 3 major ways of testing smart contract

1) Testing smart contract with solidity
2) Testing smart contract with javascript (without web3)
3) Testing smart contract with javascript (with web3)

What are the advantages and disadvantages of each and which to decide.


1 Answer 1


1) You can write solidity unit tests by using assert() and require() functions, but I don't recommend that approach since it's not as flexible as javascript and it costs gas.

2) You need web3 import the smart contract so you are able to test it

3) I think the only way to effectively test your smart contract is using javascript with web3.

Particularly using truffle and a testing framework such as mocha. You can find a lot of useful info in this tutorial: Test A Smart Contract With Truffle.

  • can you elaborate on not as flexible and its costs gas for solidity unit test?
    – vincentsty
    Nov 16, 2018 at 11:26
  • assert() and require() are considered as 'guard' functions in solidity, require() is generally used in validating function parameters and input, while assert() is used to protect against invalid or impossible cases, if assert failed then something is terribly wrong. They are not meant to be used in unit tests. Cost: Every call to the smart contract is a transaction that you are going to pay gas for, the assert() itself consumes a lot of gas.
    – abed
    Nov 16, 2018 at 11:35
  • Not as flexible: that's because if you want to change something to your test method you'll have to deploy a new smart contract every time.
    – abed
    Nov 16, 2018 at 11:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.