Code Coverage for Golang Applications

In this 10mins video, you will see how to generate code coverage for golang based application in command line interface using RKTracer Tool with 3 simple steps.

  1. Enable the RKTracer tool and rebuild the application.
  2. Test instrumented application and save coverage data.
  3. Generate code coverage reports

To generate code coverage for golang applications using the RKTracer tool. You need to add the RKTracer runtime library to the golang installation folder be it windows or Linux platform and instrument the application with the RKTracer tool. Then test the instrumented application and generate code coverage.

Adding RKTracer tool runtime library to Golang Installation

Add RKTracer runtime library rk4go to the GOROOT, i.e., go installation folder. It’s a one-time setup. 

Windows or Linux:

To Integrate RKTracer runtime library source file to Golang installation. You can follow the following instructions.

Step 1: Open a command prompt and run the command. 

go env GOROOT

If Golang is in the path, then you should see the following output. In my case, it’s in path “/usr/lib/go-1.13”

Step 2: The command output gives us the path where Golang is installed and now create rk4go in src Golang installation folder and copy rk4go from the RKTracer installation folder to rk4go folder.

sudo cp  /home/username/share/rktracer/rk4go.go /usr/lib/go-1.13/src/rk4go/

Code Coverage for Golang applications example

Identify the project folder that needs to be instrumented and generate coverage data. Please make sure the project is configured and you’re able to test it.

Enable the RKTracer tool

Then execute the following command to Turn ON (instrument)  or Turn OFF (un-instrument) RKTracer tool will instrument all Golang source files in a folder recursively.

RKTracer Turn ON/OFF commands 

  • rktracer -on  path-toproject/module/ go  -v
  • rktracer -off  path-toproject/module/ go  -v

“go” at the end means rktracer needs to only instrument python source files.

“-v”  at the end means rktracer needs show verbose instrumentation log.

The above command will instrument all the Golang source files in the project folder recursively. 

Rebuild the application with commands go clean and go build commands.

Run Tests on instrumented application

Testing can be unit-testing or functional or integration testing of the application. 

Once the testing is completed, you should see the file “rk-coverage.txt file.” now copy this file to the project folder or the path where you have executed the command “rktracer -on path-to project/module/ go  -v” to instrument the project folder using the RKTracer tool. You should also see the “rktracer” folder with rktracer tool intermediate files. The “rktracer” folder will get generated at the time of the instrumentation. You need to copy the rk-coverage.txt file parallel to the rktracer folder in the project.

Generate Code Coverage reports

Once you have the rk-coverage.txt file, you need to run the “rkresults” command to generate HTML reports. Ensure that you run the “rkresults” command in the application root folder or where you have the “rktracer” folder generated at the time of build/Instrumentation.

rkresults command will search coverage data file “rk-coverage.txt” and map with JSON files (generated during instrumentation) in the rktracer folder and generate the HTML reports. You can manually open the HTML report using index.html.

After generating code coverage, make sure to turn off(un-instrument) the RKTracer tool for the application that we instrument to create reports.

rktracer -off  path-toproject/module/ go  -v

Enable coverage for selected files

Generate coverage reports  for the selected folders

C:\project\sound\drivers\base\power\

C:\project\sound\drivers\base\power\firmware_loader\

C:\project\sound\drivers\core\

C:\project\sound\security\keys\

C:\project\sound\security\lockdown\

Suppose you need code coverage for source files from three different folders, i.e., core, keys, power, and ignore coverage for folders firmware_loader and lockdown. Edit rktracer.config in the RKTracer installation folder and go to the end of the file add the following information. 

ignore *.go

instrument */power/* */core/* */keys/*

never */firmware_loader/* */lockdown/*

ignore *.go = Ignores all golang source files from instrumentation

Instrument = Instrument source files from given folders

never = ignore selected folder  

Generate coverage reports only for selected functions from three different files.

fun_X() in source-file-X 

fun_Y() in source-file-Y

fun_Z() in source-file-Z

Suppose you need code coverage for selected functions from three different files. Edit rktracer.config in the RKTracer installation folder and go to the end of the file and set the following variables as shown below.  

ignore *.go

instrument *source-file-X.go *source-file-Y.go *source-file-Z.go

function-ignore *

function-instrument fun_X() fun_Y() fun_Z()

ignore *.go = Ignore all golang programming source files

instrument *file-X.go *file-Y.go *file-Z.go   = Instrument only these three source files.

function-ignore * = Then ignore all functions in the above three files.

function-instrument fun_X() fun_Y() fun_Z() = Don’t ignore these three functions from these three files from instrumentation.

Similar Posts