18

What is the reason that tests can be written in both? Is there any distinct advantage to using one over the other?

Or is one in general easy to work with?

2 Answers 2

21

I would use the analogy of unit testing vs integration testing to distinguish these two methods.

In my opinion, a Truffle solidity test can be used to cover a small piece of code, basically you will be able to test every single function in your contract in an isolated way (isolated from Web3 essentially).

On the other hand, a Truffle Javascript test (Mocha) demonstrates that different pieces of the system work together. You will be able to test complex scenarios with multiple calls and transactions.

But you can use Javascript Mocha tests for unit testings as well. So I usually prefer Javascript for quick and dirty testing but I would suggest to separate unit-testing (solidity) from integration/functional testing (Javascript) for a big project that involves a team of analysts, developers and testers.

2
  • Is there any limitations like a Javascript test can't do what Solidity test does and visa versa? Because I can't find how to initialize a struct from a Javascript test.
    – ferit
    Commented Jun 7, 2018 at 19:13
  • 2
    Javascript tests can't cover internal or private solidity functions (only external and public. On the other hand, a solidity test can basically cover all your code but the behavior would be EVM based whilst a JavaScript test reproduce the dApp behavior (analogy of testing an API externally and internally with its implementation, sometime the issue is in the data transport, not in the logic implementation) Commented Jun 8, 2018 at 8:50
2

You currently can't test revert in solidity unit tests. Therefore, I find the javascript testing more useful for testing failure cases.

I wrote a blogpost about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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