4

Truffle offers two ways to test smart contracts: either using test files written as Solidity contracts, or Javascript/Mocha.

https://truffleframework.com/docs/truffle/testing/writing-tests-in-solidity

https://truffleframework.com/docs/truffle/testing/writing-tests-in-javascript

Are there recommendations on when to choose between the two? Do you stick to one or do you use both but for different purposes?

4 Answers 4

0

I use exclusively javascript (for now), because:

  • I am more comfortable with the nodejs dev environment and tools than with solidity
  • I like the debugging messages I can put in js (you could probably fire some events that would act as logging things)
  • I haven't written complex smart contracts that need to interact with one another, so that I should test their interactions
  • I can take nodejs snippets from the tests, and use them in the web UI almost without any modification.

I'm not saying my approach is the best and everybody should use it and testing in solidity is useless. I rather say I'm lazy, didn't had enough time to learn and get to feel comfortable using solidity for testing and nodejs testing is good enough for me for now.

If you have the time, I'd recommend you to get comfortable using both ways.

2
  • Hmm, based on what you just said, I suppose we would have to use Solidity when testing interactions between contracts?
    – Bastien
    Nov 12, 2018 at 14:38
  • I think that's the best use case for it. Of course, it can be used to test single contracts too, but web3js did a very good job until now for me. Nov 12, 2018 at 14:43
0

I agree with Tudor, I use javascript as well. Would like to add:

  • tests in solidity truffle does some extra transactions which is quite difficult to track
  • tests in javascript much comfortable to debug
0

Testing your smart contracts with JavaScript

And the result:

Contract: FundRaise ✓ has an owner (38ms ) ✓ is able to accept funds (234ms )
2 passing (473ms )

Handsomely. This is a process that is systematically carried out to cover smart contracts with a test written in JavaScript. Just keep doing it until all the conditions of a smart contract are fulfilled.

0

Here's a great Medium article that covers everything related to testing on Ethereum.

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.

Source: https://betterprogramming.pub/how-to-test-ethereum-smart-contracts-35abc8fa199d

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.