New to Coco v4.3.3 is the ability to instrument a .NET Core application on Linux, for use in obtaining code coverage metrics for such applications. This blog will walk you through how to instrument a simple .NET Core application written in C# and built on Linux. We will then demonstrate using Squish Coco to analyze the code coverage data for the application. 

A Simple .NET Program

We’ll use Microsoft’s “Hello World!” tutorial as our application. The program prints the statement “Hello World!” and the current time to the console. Prior installation of the .NET SDK is a requirement for this tutorial, as is a Squish Coco installation in the root directory.

The program file consists of the following code: 

using System;

namespace myApp
{
      class Program
      {
           static void Main(string[] args)
           {
                Console.WriteLine("Hello World!");
                Console.WriteLine("The current time is " + DateTime.Now);
           }
      }
}

To run the program, issue the following in a terminal:

$ dotnet run

If the program ran successfully, the console should return:

Hello World!
The current time is 12/3/2019 4:16:25 PM

Instrumenting the Application

There are two methods to instrumentation, one method most suitable to those engineers who typically do not have access to the project files and source (e.g., a test or QA engineer), and a second method most suitable to those who develop the code. We will explore both methods.

Method 1: No Access to Source Files

First, clean the project directory:

$ dotnet clean

Set an environment variable to activate the code coverage:

$ export COVERAGESCANNER_ARGS=--cs-on

Then, rebuild the project:

$ dotnet build

Finally, run the project:

$ dotnet run

A *.csexe and *.csmes file will generate. Open these in the CoverageBrowser:

$ coveragebrowser -m ./obj/Debug/netcoreapp3.0/myApp.dll.csmes -e myApp.dll.csexe

The reported code coverage is 100%, as shown in the image above.

Method 2: Access to the Project Files

In this method, we will edit the *.csproj file delivered with the tutorial to include a statement to activate the code coverage.

Open myApp.csproj, and add the following line within the PropertyGroup bracket:

<DefineConstants>COVERAGESCANNER_COVERAGE_ON</DefineConstants>

The complete myApp.csproj file should look like the following:

<Project Sdk="Microsoft.NET.Sdk">

   <PropertyGroup>
       <OutputType>Exe</OutputType>
       <TargetFramework>netcoreapp3.0</TargetFramework>
       <DefineConstants>COVERAGESCANNER_COVERAGE_ON</DefineConstants>
   </PropertyGroup>
</Project>

Save the file. Next, as in Method 1, clean, build, and run the project:

$ dotnet clean
$ dotnet build
$ dotnet run

Similar to Method 1, we can open the coverage results in the CoverageBrowser:

$ coveragebrowser -m ./obj/Debug/netcoreapp3.0/myApp.dll.csmes -e myApp.dll.csexe

Again, we see 100% statement coverage.

Summary

The above methods demonstrate instrumenting a simple .NET Core project on Linux, for obtaining code coverage data for the program using Squish Coco. Note that no changes to the actual source code were required, only minor modifications to the .csproj file, and in the case of Method 1, no changes to the project were required at all.

Leave a Reply

Your email address will not be published. Required fields are marked *

Copy link
Powered by Social Snap