Product
Solutions
Resources
Company
Download Trial Book a Demo

Csharp Code Coverage for Applications in Dotnet & MSBuild

Generate C# code coverage for applications built with dotnet or msbuild on the command line. Prefix the rktracer command to your build, run your tests on the instrumented application, and produce an HTML report.

You should be able to build and test the application using the dotnet or msbuild build command in the command line mode. RKTracer provides multiple coverage metrics: Function Coverage, Line Coverage, Statement Coverage, and Multi-Condition Code Coverage.

In short

Prefix rktracer to the dotnet build or msbuild command, run your tests, then run rkresults to generate the HTML report.

Dotnet/msbuild Code Coverage example

Before enabling RKTracer for instrumentation, you should clean the project, as the tool instruments at the source code level. The process involves changing:

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

to:

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

RKTracer instruments C# source files with c:\rktracer/lib/librkliz-x86_64-w64-mingw32.exe and writes instrumented output to the prime.cs file in a preprocessed directory. It stores metadata, instrumentation structure, and source file content in a JSON file, referencing rktracer.config during instrumentation. The tool automatically adds the runtime library to dotnet build.

Run tests on rktracer instrumented application

You can run unit tests or functional tests on the instrumented application. After testing, RKTracer saves coverage data to the rk-coverage.txt file. If testing occurs on a server machine, copy the rk-coverage.txt file from the server to the application working folder on the build machine.

Generate Code Coverage HTML reports

Run the rkresults command in the project root folder where the rktracer folder exists.

generate the report
rkresults

The rkresults command searches the rk-coverage.txt file and maps it with JSON files generated during instrumentation in the rktracer folder, then generates HTML reports. You can manually open the HTML report using index.html.

Enable coverage for selected files

For selected folders, edit rktracer.config and add:

rktracer.config
ignore *.cs
instrument */power/* */core/* */keys/*
never */firmware_loader/* */lockdown/*
  • ignore *.cs ignores all C# source files from instrumentation.
  • instrument instruments source files from the given folders.
  • never ignores the selected folders.

For selected functions, edit rktracer.config and set:

rktracer.config
ignore *.cs
instrument *file-X.cs *file-Y.cs *file-Z.cs
function-ignore *
function-instrument fun_X() fun_Y() fun_Z()
  • ignore *.cs ignores all C# source files.
  • instrument *file-X.cs *file-Y.cs *file-Z.cs instruments only these files.
  • function-ignore * ignores all functions in these files.
  • function-instrument fun_X() fun_Y() fun_Z() does not ignore these specific functions.

Stuck on your build?

Open a support ticket and an engineer who knows your toolchain will help. You get a tracked ticket ID by email.