How to unit test your Nebulas smart contracts?

How to unit test your Nebulas smart contracts?

Nebulas, a third generation blockchain, is an incentive-based, self-evolving blockchain system. It offers many advantages over other blockchains.

Value-based ranking system which works on liquidity and propagation of address, self evolving core protocols, speediness, Proof of Devotion (PoD) consensus algorithm which leverages PoS, PoI and NR are just some of them. Owing to these improved features, one of our recent blockchain projects validated the use of Nebulas for development.

Testing is a critical piece of any implementation, most importantly, a blockchain project. Though Nebulas has a well supported community, a strong testing framework for Nebulas smart contracts does not exist in the market. Inspired by Truffle, a testing framework for ethereum smart contracts, we tried to create an automated testing framework for testing Nebulas smart contracts and we open sourced this library as Nebtest.

Features:

We built the testing framework using Istanbul and it works well with test runners like Mocha as well.

  • With Nebtest, we can automate smart contract testing on Nebulas.
  • Nebtest allows for running test cases with multiple test data sets at a time generating a detailed text execution report.
  • Nebtest enables running test cases in a clean state, by deploying a smart contract before each test case.
  • The development of this framework helped us reduce regression testing effort, which is critical to a blockchain implementation ensuring that 90-95% of the code is tested. Once all test cases are executed, the framework generates a detailed text coverage report.

Installation:

  • Download directly as a npm package
  • npm install –save nebtest
  • npm install –save mocha chai

Test Cases:

You can provide your input test file as a JSON file. Sample test file will look like:

[
 {
   name: 'Test Case Description',
   data: [
      {
        name: 'Test data name',
        testInput: {
                                ...test inputs
},
testExpect: {
      ...test result expect
       }
    }
  ]
}

]

Run Test:

Unit testing the smart contracts with Nebtest is as simple as writing unit tests for any javascript applications with Mocha.

Add following command to scripts. The timeout is based on the mining time of the contract you are running. Increase the timeout if you are getting the timeout error.

“scripts”: {

“test”: “mocha –timeout 600000”
}

Run the test as,

npm run test

Test Result:

Nebulas Testing Smart Contracts

Example:

You can find the complete example here