Csharp Code Coverage for Applications in Dotnet & MSBuild

You will see how to generate Csharp code coverage for application developed with C# using dotnet and msbuild build systems in command line interface using RKTracer Tool with 3 simple steps.

  1. Prefix rktracer command to build command and rebuild application.
  2. Test instrumented application and save coverage data.
  3. Generate code coverage reports

You should be able to build and test the application using the dotnet or msbuild build command in the command line mode. To generate code coverage for applications with dotnet or msbuild build systems, you need to prefix rktracer to dotnet or msbuild build command.

dotnet build prime.csproj or msbuild prime.sln /t:rebuild

  TO

 rktracer dotnet build prime.csproj or rktracer -v msbuild prime.sln /t:rebuild

RKTracer tool makes internal adjustments to dotnet build to instrument all the source files compiled. By default, rktracer instruments for multiple-condition code coverage, and you see the following code coverage information in the HTML report.

  • Function Coverage
  • Line Coverage 
  • Statement Coverage
  • Multi-Condition Code Coverage

Dotnet/msbuild Code Coverage example

Before you enable the RKTracer tool for instrumentation, you need to clean the project, as the RKTracer tool will instrument the project at the source code level.

dotnet build prime.csproj or msbuild prime.sln /t:rebuild

  TO

 rktracer dotnet build prime.csproj or rktracer -v msbuild prime.sln /t:rebuild

dotnet code coverage step 1
dotnet code coverage step 1
dotnet code coverage step 2
dotnet code coverage step 2
  • RKTracer instruments C# source file with “c:\rktracer/lib/librkliz-x86_64-w64-mingw32.exe ” and writes out the instrumented file to the “prime.cs” file in a preprocessed directory. Also, it stores the metadata, structure of instrumentation, and copy of source file content in JSON file. The tool will refer rktracer.config for internal configuration at the time of instrumentation.
  • RKTracer automatically adds runtime library to dotnet build.

Run tests on rktracer instrumented application

You can run unit tests or functional tests. In this case, it’s functional testing.

dotnet code coverage step 3
dotnet code coverage step 3

Post-testing the RKTracer tool will save coverage data to the rk-coverage.txt file. In case if your testing instrumented application in server machine, you need to copy the rk-coverage.txt file from the server to the application working folder to build machine.

Generate Code Coverage HTML reports

Once you have the rk-coverage.txt file, you need to run the command “rkresults” to generate HTML reports. Ensure that you run the “rkresults” command in the project root folder or where we have the “rktracer” folder.

dotnet code coverage step 4
dotnet code coverage step 4

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

dotnet code coverage reports
dotnet code coverage reports

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 *.cs

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

never */firmware_loader/* */lockdown/*

ignore *.cs = Ignores all C# 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 *.cs

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

function-ignore *

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

ignore *.cs = Ignore all C# programming source files

instrument *file-X.cs *file-Y.cs *file-Z.cs   = 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