loading...
Cover image for Running .NET Core 3 XUnit Code Coverage in AppVeyor Using OpenCover and Codecov

Running .NET Core 3 XUnit Code Coverage in AppVeyor Using OpenCover and Codecov

petrsvihlik profile image Petr Švihlík ・2 min read

I love AppVeyor. It's my go-to CI/CD service for .NET projects. I use it for both personal and work open-source projects. I also love automation of all kinds so, among other things, I want my test coverage reports to be automatically generated with every build.

Recently, I started upgrading my project portfolio to .NET Core 3.0. With that, I also wanted to level-up my unit tests which are typically XUnit. I didn't want to introduce too much change to my code so, after some research, I decided to stick with the following setup: AppVeyor (which I've already been using) + Codecov (which we use for another project at work) + OpenCover.

Custom PowerShell coverage script 📜

The goal was to create a minimalistic, reusable piece of code (independent of environment settings, tool versions, etc.) that I could use across multiple projects. Here it goes:

coverage.ps1

The script can run locally and in AppVeyor with slightly different configurations. The most tricky part is:

$register = if ($ENV:APPVEYOR -eq $true ) { '-register' } else { '-register:user' }

When I finally made the script run on my local machine, it started failing in AppVeyor. It took me a few hours of digging and debugging to figure out that I can't use -register:user in AppVeyor. Fortunately, debugging is quite easy in AppVeyor as you can RDP to the build worker.

I highly recommend reading OpenCover's documentation. It'll help you understand the syntax of targetArgs, filter and register parameters.

Hooking the script into the CI pipeline 🔗

AppVeyor supports Chocolatey so I'm using cinst to install OpenCover and Codecov CLIs. Then I'm turning off the default test script and replacing it with my own which runs OpenCover to generate the coverage files, and finally, I'm calling Codecov CLI to upload the results.

Cool thing is that if you run codecov -f coverage.xml from AppVeyor you don't need an API key. It just works automagically ✨.

appveyor.yml

The result

I've got this nice sunburst chart indicating which files need attention:

Codecov sunburst

I've also got the Codecov badge with % coverage:

GitHub logo Kentico / kontent-delivery-sdk-net

Kentico Kontent Delivery .NET SDK

Kentico Kontent Delivery .NET SDK

Build status CircleCI codecov Stack Overflow

Paradigm Package Downloads Documentation
Async NuGet NuGet 📖
Reactive NuGet NuGet 📖

Summary

The Kentico Kontent Delivery .NET SDK is a client library used for retrieving content from Kentico Kontent.

You can use it via any of the following NuGet packages:

The first package provides the DeliveryClient object to consume Kentico Kontent data via the traditional async way. The second one provides the DeliveryObservableProxy object that enables the reactive way of consuming the data.

The SDK targets the .NET Standard 2.0, which means it can be used in .NET Framework 4.6.1 projects and above, and .NET Core 2.0 projects and above.

Using the DeliveryClient

The IDeliveryClient interface is the main interface of the SDK. Using an implementation of this interface, you can retrieve content from your Kentico Kontent projects.

To initialize the client, use the DeliveryClientBuilder class and provide a project ID.

// Initializes an

And last but not least, a nice ASCII art in the AppVeyor log here:
Alt Text

So far, I have successfully tested this approach in two repos so I hope it'll work for you too. Any improvements are welcome!

Posted on by:

petrsvihlik profile

Petr Švihlík

@petrsvihlik

The open-source guy @ Kentico.

Discussion

markdown guide