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.
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.
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
Cool. 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!
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.