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 using Visual Studio Code, you can run unit test cases 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 an example unit test case in the comments section for reference. You can follow the example to write unit test cases for your chaincode methods.

To run the unit test cases, enter the command npm run test from the chaincode project folder. This command also generates 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 an example unit test case in the comments section for reference. You can following the example to write unit test cases for your chaincode methods.

To run the unit test cases, enter the command go test from the chaincode project folder. For coverage reports, add the flag --cover.
go test --cover