Truffle allows either one or the other. Now I wonder which I shall use to test my code. I'm not a big fan of mocha. When I see code like

contract('MetaCoin', function(accounts) {   it("should put 10000 MetaCoin in the first account", function() {
    return MetaCoin.deployed().then(function(instance) {
      return instance.getBalance.call(accounts[0]);
    }).then(function(balance) {
      assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account");

I think that something went really wrong in terms of readability. On the other hand the idea to write a contract to test another contract makes me pretty unconfortable. Which are the caveats of unit testing with these two different approaches?

  • 3
    Mocha doesn't have to be this unreadable, if you use async syntax, it gets rid of all the nested then() methods and flattens everything out. – MichaelTaylor3D Mar 12 '18 at 20:02
  • What do you mean, "solidity or mocha"? They're not really comparable. What would a solidity unit test be? – Teleporting Goat Apr 19 '18 at 8:03

I have written more than a thousand tests for different smart contracts and can say that you will almost always want to use javascript/mocha with truffle. There are very few cases that I have run into where you will want to use solidity instead. It is just so much easier to setup your contracts to the state that you want in order to run the tests.

Also... you really don't need to chain up function calls like in the example given. You can use async/await which will make things MUCH cleaner and readable. I have created a boilerplate truffle project which I use for any of my new projects. I keep it updated with examples etc. I would highly recommend checking out the way tests are in there...



direct link to a test file using async/await:



Replace: assert.equal(balance.valueOf(), 10000, "10000 wasn't in the firstaccount");


assert.equal(balance.valueOf() === 10000, 1); // In case of true, for false replace 1 with -1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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