Orchestrating Visual Studio Code : Part 6 : Unit Test Code Coverage

We will learn how to add test code coverage reports using xUnit, Gulp and Coverlet, and LCOV.

The Files

The following files will be created in this section:

.
├── gulpfile.js
├── package.json
├── .coverage

NodeJS

In order to use Gulp, you will need to have NodeJS installed.

brew install node

Windows users can either load the installer from https://nodejs.org or use chocolatey to install node.

Gulp

Gulp is a toolkit for automating repetitive tasks in your development environment.

Installation

To use gulp, we will need to initialize npm (Node Package Manager). Run the following in the root of your project:

npm init

Then add gulp to your project:

npm install gulp --save-dev

And the LCOV html report generator:

npm install gulp-lcov-to-html --save-dev

While we have tried to focus on VS Code's built-in task runner and local scripts throughout this series, sometimes you need to step out of the box and leverage node and gulp for heavier workloads.

The Gulp Task

Create a file gulpfile.js in the root of your solution and add the following code:

const gulp = require('gulp')
const lcov = require('gulp-lcov-to-html')

gulp.task('generate-coverage-report', function () {

    return gulp
        .src("test/**/coverage.info") // grab the lcov files
        .pipe(lcov({
            name : "My WebApp"
        })) 
        .pipe(gulp.dest(".coverage")) // output to .coverage folder

});
gulpfile.js

This task generate-coverage-report will look through the test folder for coverage.info files and generate an html report with them in the .coverage directory.

Coverlet

Coverlet is a code coverage library for .NET Core, with support for line, branch, and method coverage. We will be using it in our test project to output coverage data in lcov format.

Installation

In the root of test/UnitTests, run the following to install coverlet in your project:

dotnet add package coverlet.msbuild --version 1.2.0

The Scripts

We will add the following to our unitTests() method in project-tasks.sh to generate a coverage report when our tests run.

unitTests () {

    for dir in test/*.UnitTests*/ ; do
        ...
        dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=lcov
        ...
    done
    
    ...
    
    gulp generate-coverage-report
}
project-tasks.sh

The switches we added to dotnet test tell the test runner that we want to enable code coverage and output the results as lcov. We also call the gulp task that we created earlier to create the report.

Running the Tests

Press F1 and run the unit-tests task. You will see output from our gulp task. Opening the report in a web browser will give you detailed results per project, namespace, and class.

Calculating coverage result...
  Generating report 'C:\Users\CTown\AppDev\VSCode-Orchestration\test\UnitTests\coverage.info'

| Module   | Coverage |
|----------|----------|
   WebApp       0%
 
++++++++++++++++++++++++++++++++++++++++++++++++
+ Generating code-coverage html report
++++++++++++++++++++++++++++++++++++++++++++++++
[11:10:19] Using gulpfile ~\AppDev\VSCode-Orchestration\gulpfile.js
[11:10:19] Starting 'generate-coverage-report'...
[11:10:19] Finished 'generate-coverage-report' after 118 ms
++++++++++++++++++++++++++++++++++++++++++++++++
+ Report generated at:
+
+ /.coverage/index.html
++++++++++++++++++++++++++++++++++++++++++++++++

Since we only have a simple smoke test that doesn't actually cover any of our application code - the module and coverage results will be empty. Go ahead and start writing some real unit tests and you will begin to see percentages.


The Source Code

You can find the source code for this article in the following repository under the part-6-xunit-code-coverage branch:

https://github.com/christophla/VSCode-Orchestration/tree/part-6-xunit-code-coverage

Next Post : Deploying NuGet Packages

In the next post we will learn how to create a task to scan our solution for NuGet projects and deploy them to nuget.org and myget.org.


Let's NuGet!

Previous Post : Unit Testing

https://christophertown.com/orchestrating-vscode-unit-testing