Collecting coverage for .NET Framework with AltCover

2024-08-28

.NET Framework has been around for long - but Microsoft doesn't actively release newer versions for it in favour of the more powerful .NET Core.

If you have worked with a .NET Framework project you can know how frustrating it is to find a working solution with proper documentation for the problem you are trying to solve.

Let's take a look at how to get coverage reports with .NET Framework projects

What's on the table?

I'll skip past the what and why of coverage and get comparing options, sticking to free & open source ones only.

AltCover, OpenCover, and coverlet top the list here.

OpenCover, being retired and having handed the baton over to AltCover, still maintains a large user base because it works very well with Windows & .NET Framework - which is probably what you were looking for when you came stumbling across this blog.

However let's look at AltCover today - I tried my luck with coverlet but there was not much documentation on getting it working without the dotnet CLI, and my attempts at trying to get it work with MSBuild directly just ran into too many problems.

Steps

  • You'll first need the AltCover executable, which can be found on most of the AltCover NuGet packages - pick the base one if you are unsure.
    • Once it has been added to your project, and your NuGet packages have been restored you can find the executable in your packages folder (in the steps to follow, substitute AltCover.exe with your entire absolute path to this executable)
    • Alternatively, you can choose to install it as a global tool (if you have access to the dotnet CLI) - I preferred this for ease of use
  • Next, we need to get a test runner to execute our tests, which AltCover will hook into and collect coverage from.
  • Then we get our test assemblies ready - clean your bin folder and build the binaries in your Test Project with MSBuild
  • Unlike most of the front-facing documentation for altCover, it's not a single line command to run dotnet test to get it working in most cases for .NET Framework projects. There are many ways to run AltCover apart from the dotnet CLI - the most seamless for me was Instrument Now, Test Later
    • We start off by instrumenting the binaries - AltCover.exe --outputDirectory=__Instrumented
    • Next we run the tests and let AltCover Runner wrap around it - AltCover.exe Runner --recorderDirectory __Instrumented --executable <path-to-runner-step-2.exe> -- ./__Instrumented/<test-assembly>.dll

refer this source for a more detailed look at an example.

This generates your coverage file in OpenCover format by default with both line & branch coverage. AltCover offers a large variety of options in it's documentation. Browse through these to customize the coverage report to your liking.

Post this, convert your OpenCover report to a readable format using the ReportGenerator tool. Once you add the package, run the minimal command line parameters to generate the HTML report. ReportGenerator.exe -reports:<path-to-coverage.xml> -targetdir:reports (Inside the folder with ReportGenerator.exe)

And you would have successfully generated coverage reports for a .NET Framework project which you should migrate to .NET Core ASAP!

Till the next blog post, Auf Wiedersehen!