Csharp Code Coverage for Applications in Visual Studio

In this 8mins video, you will see how to generate Csharp code coverage for application developed with C# in visual studio ide 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

Code Coverage tool integration with Visual Studio IDE

1: Close all instances of Visual Studio IDE.

2: Open a new command prompt and run the following command to install the RKTracer tool plugin for Visual Studio IDE.

rktracer -vs -integrate

Csharp Code Coverage in Visual Studio IDE integration
Code Coverage Visual Studio IDE Integration

Once successfully RKTracer tool is integrated with Visual Studio IDE. You can see RKTracer tool options in Tools menu

Csharp Code Coverage in Visual Studio IDE

Ensure you’re able to build and test the application. Testing can be of unit or functional testing on a host or server machine

Enable the RKTracer tool

Go to Tools menu → RKTracer ON

Csharp Code Coverage in Visual Studio IDE step 1
Csharp Code Coverage in Visual Studio IDE step 1

Rebuild and run tests

In general, testing can be unit testing or functional testing.

Csharp Code Coverage in Visual Studio IDE step 2
Csharp Code Coverage in Visual Studio IDE step 2
Csharp Code Coverage in Visual Studio IDE step 3
Csharp Code Coverage in Visual Studio IDE step 3

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

Generate Code Coverage reports

Go to –> Tools menu –> RKTracer Report to generate Html reports. The RKTracer tool will take 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.

Csharp Code Coverage in Visual Studio IDE step 4
Csharp Code Coverage in Visual Studio IDE step 4
Csharp Code Coverage reports
Csharp 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