I’m continuing to explore testing and code coverage on open source .NET Core. Earlier this week I checked out coverlet. There is also the venerable OpenCover and there’s some cool work being done to get OpenCover working with .NET Core, but it’s Windows only.

Today, I’m exploring AltCover by Steve Gilham. There are tools that use the . Profiling API at run-time, instead, AltCover weaves IL for its coverage.

As the name suggests, it’s an alternative coverage approach. Rather than working by hooking the .net profiling API at run-time, it works by weaving the same sort of extra IL into the assemblies of interest ahead of execution. This means that it should work pretty much everywhere, whatever your platform, so long as the executing process has write access to the results file. You can even mix-and-match between platforms used to instrument and those under test.

is a NuGet package but it’s also available as .NET Global Tool which is awesome.

dotnet tool install --global altcover.global

This makes “altcover” a command that’s available everywhere without adding it to my project.

That said, I’m going to follow the AltCover Quick Start and see how quickly I can get it set up!

I’ll Install into my test project hanselminutes.core.tests

dotnet add package AltCover

and then run

dotnet test /p:AltCover=true

90.1% Line Coverage, 71.4% Branch CoverageCool. My tests run as usual, but now I’ve got a coverage.xml in my test folder. I could also generate LCov or Cobertura reports if I’d like. At this point my coverage.xml is nearly a half-meg! That’s a lot of good information, but how do I see  the results in a human readable format?

This is the OpenCover XML format and I can run ReportGenerator on the coverage file and get a whole bunch of HTML files. Basically an entire coverage mini website!

I downloaded ReportGenerator and put it in its own folder (this would be ideal as a .NET Core global tool).

c:ReportGeneratorReportGenerator.exe -reports:coverage.xml -targetdir:./coverage

Make sure you use a decent targetDir otherwise you might end up with dozens of HTML files littered in your project folder. You might also consider .gitignoring the resulting folder and coverage file. Open up index.htm and check out all this great information!

Coverage Report says 90.1% Line Coverage

Note the Risk Hotspots at the top there! I’ve got a CustomPageHandler with a significant NPath Complexity and two Views with a significant Cyclomatic Complexity.

Also check out the excellent branch coverage as expressed here in the results of the coverage report. You can see that EnableAutoLinks was always true, so I only ever tested one branch. I might want to add a negative test here and explore if there’s any side effects with EnableAutoLinks is false.

Branch Coverage

Be sure to explore AltCover and its Full Usage Guide. There’s a number of ways to run it, from global tools, dotnet test, MSBuild Tasks, and PowerShell integration!

There’s a lot of great work here and it took me literally 10 minutes to get a great coverage report with AltCover and .NET Core. Kudos to Steve on AltCover! Head over to https://github.com/SteveGilham/altcover and it a STAR, file issues (be kind) or perhaps offer to help out! And most of all, share cool Open Source projects like this with your friends and colleagues.

Sponsor: Preview the latest JetBrains Rider with its built-in spell checking, initial Blazor support, partial C# 7.3 support, enhanced debugger, C# Interactive, and a redesigned Solution Explorer.

Source link http://feeds.hanselman.com/~/559664618/0/scotthanselman~AltCover-and--give---coverage-on-NET-Core.aspx


Please enter your comment!
Please enter your name here