Tutorial Chaincode Unit Testing on Hyperledger Fabric

Valerio Mattioli
Coinmonks
5 min readJan 14, 2019

--

Hi everyone! Are you looking for how incorporate a unit test suite to improve the chaincode development in Hyperledger Fabric? I’m here to explain to you how I accomplished to create the testing environment, in golang, that enabled me to really speed up the development of the chaincode because in this way you won’t even need to setup the dev environment anymore (if you design a good test suite, of course).

I organized the tutorial in this way:

  1. MockStub — Shim package that provides APIs for the chaincode to access its state variables, transaction context and call other chaincodes (wiew source).
  2. Go Test — Package testing provides support for automated testing of Go packages. It is intended to be used in concert with the “go test” command, which automates execution of any function (official go documentation).
  3. Test Suite — The Kernel of the tutorial, I’ll show how I implemented my test suite.

MockStub

MockStub, as said before, is the shim package that enables the creation of test functions to directly test the chaincode functions.

Go Test

Thankfully from our point of view testing in go is very, very easy. To run the test suite you have to simply run the command in the repository where the test suite is located:

Test Suite

To write a new test suite, create a file whose name ends _test.go that contains the TestXxx functions, in our case will be <chaincode_name>_test.go

Now we go in depth to see how I developed some test functions specifically for chaincode development using MockStub functionalities.

Test Logger

First of all I set the logger to print the useful information:

Test Comparisons

After that I developed a little “library” composed by some functions that embed the comparisons to speed up the development of the real tests:

  • checkState
  • checkNoState
  • checkQuery
  • checkBadQuery
  • checkInvoke
  • checkBadInvoke

We will look at all these functions in detail.

  • checkState:
  • checkNoState:
  • check Query (1,2,…,n args):

— for 1 argument:

— for 2 arguments:

we can note that the difference is only on the number of arguments in the function call, so we can generalize for n easily…

  • checkBadQuery:
  • checkInvoke:
  • checkBadInvoke:

Test Chaincode Functions (an example)

Now let’s go on the real testing of chaincode’s functionalities, for example let’s take as an example a general ‘CreateFeature’ functionality of the chaincode:

We check 2 things in this test function:

  • Invoke — Directly calling the checkInvoke passing it the functionAndArgs.
  • Query — we call “GetFeature” and we put our expectedResponse JSON String.

If everything went good we will se the output with the embedded logs and closing with a:

if everything went as espected.

Or a:

if there is something that differs from our expectations. In this case, if we properly set the logs we can se where is the source of the fail, likewise:

We can easily spot that the expected feature’s name was “Shape” but we got “Color”.

At the end I show how can look a test suite:

You can easily understand how this can speed up the development, if you also think that you can take advantage of the Init function to populate the Testing Ledger with all the data that you need to simulate whatever scenario you have in your mind. We will look at how to Init the Ledger in another article.

I hope this tutorial gave you a brief introduction to start testing your chaincode! If yes don’t hesitate and gimme a loud clap! ;)

See you!

Valerio

Get Best Software Deals Directly In Your Inbox

--

--