Code Coverage: How to Improve Test Quality

Original author: SCOTT HANSELMAN
  • Transfer
Our colleague, Scott Hanselman, continues to study testing and code coverage in the .NET Core. He has already managed to talk about some tools for improving the quality of testing, and today he is talking about one more thing : AltCover . Look under the cat!

I give the floor to Scott Hanselman .

Earlier this week I read the coverlet . There are also the respected OpenCover tool and several interesting projects that are trying to get OpenCover to work with .NET Core, but only on Windows.

Today I will be studying AltCover by Steve Gilham (Steve Gilham). There are tools for defining coverage that use the .NET Profiling API at runtime. AltCover, on the other hand, refers to IL .

As the name suggests, this is an alternative approach to coverage. Instead of working through the .NET-API profiling at runtime, it adds the same additional IL to what we want to build before it is executed. This means that it should work almost everywhere, on any platform, as long as the execution process has access to modify the result file. You can combine different platforms used for instrumentation and those that are being tested.

AltCover is not only included in the NuGet package, but is also available as part of the .NET Core Global Tool, which is just great.

dotnet tool install --global

This allows you to use the altcover command anywhere without adding it to the project.

Still, I'm going to go through the AltCover quick setup guide and see how quickly I can do it!

I will install it in my test project hanselminutes.core.tests,

dotnet add package AltCover

and then launch.

dotnet test /p:AltCover=true

Fine. My tests run as usual, but now I have a test.xml file in the test folder. If desired, I could also generate reports from LCov or Cobertura. Already, the coverage.xml file weighs almost half a megabyte! It has a lot of useful information, but how can I see the results in a readable form?

The file is in OpenCover XML format, so I can run the ReportGenerator for the coverage file and get a whole bunch of HTML files. In fact, the whole mini-site coverage!

I downloaded the ReportGenerator and put it in a separate folder (this is the best solution for the .NET Core Global Tool).

c:\ReportGenerator\ReportGenerator.exe -reports:coverage.xml -targetdir:./coverage

Make sure you use good targetDir, otherwise you will get dozens of unnecessary HTML files in the project folder. You should also consider using .gitignoring for the final folder and the coverage file. Open index.htm and appreciate all this useful information!

Pay attention to Risk Hotspots from above! I have a CustomPageHandler with significant NPath complexity and two views with significant cyclomatic complexity .

Also appreciate the excellent track coverage as indicated here in the coverage report results. You may notice that EnableAutoLinks was always true, so I checked only one way. Perhaps I will perform a negative test here and see if there are any side effects if EnableAutoLinks is false.

Read the complete AltCover usage guide . There are many ways to run this tool, from global Global Tools, dotnet tests, MSBuild tasks, to PowerShell integration!

  • To see usage examples, click here .
  • To familiarize yourself with the modes of operation, click here .
  • To start AltCover from dotnet test, go to the integration dotnet test.
  • To run AltCover from MSBuild, go here .
  • To run AltCover and its associated Windows PowerShell or PowerShell Core tools, go here .

There are a lot of good examples here, and it took me just 10 minutes to get an excellent coverage report using AltCover and .NET Core. Thanks to Steve from AltCover! Visit to give him an asterisk, report problems with files or offer help! And most importantly, share open source projects like this with friends and colleagues.

Also popular now: