Writing Unit Test Cases and Coverage Reports for the Chaincode Project

Blockchain App Builder includes support for writing unit test cases and coverage reports for the generated chaincode projects.

Note:

If you're running your unit tests in VS Code, it can be done in the Terminal window.

TypeScript

To write unit test cases for a TypeScript chaincode, refer to the file <chaincodeName>.spec.ts in the tests folder inside the generated chaincode project. This file provides the complete unit testing setup for TypeScript chaincodes, and also an example unit test case in the comments section for reference. Following the example, you will be able write unit test cases for your chaincode methods.

The unit test cases can be run by executing the command npm run test from the chaincode project folder. This will also give you the coverage reports.


Screen capture of unit tests for a chaincode project

Go

To write unit test cases for a Go chaincode, refer to the file src/src_test.go inside the generated chaincode project. This file provides the complete unit testing setup for Go chaincodes, and also an example unit test case in the comments section for reference. Following the example, you will be able write unit test cases for your chaincode methods.

The unit test cases can be run by executing the command go test from the chaincode project folder. For coverage, add the flag --cover.

Example: go test --cover.