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.
我将继续探索在开源.NET Core上的测试和代码覆盖。 本周初,我检查了床罩。 还有令人尊敬的OpenCover ,为了使OpenCover与.NET Core一起工作,还做了一些很酷的工作,但它仅适用于Windows 。
Today, I'm exploring AltCover by Steve Gilham. There are coverage tools that use the .NET Profiling API at run-time, instead, AltCover weaves IL for its coverage.
今天,我正在探索Steve Gilham撰写的AltCover 。 有一些覆盖工具在运行时使用.NET Profiling API,相反,AltCover将IL编织为其覆盖。
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.
顾名思义,这是另一种覆盖方法。 它不是通过在运行时挂钩.net分析API来工作,而是通过在执行之前将相同类型的额外IL编织到感兴趣的程序集中来工作。 这意味着,只要执行过程具有对结果文件的写访问权限,无论您使用什么平台,它都几乎可以在任何地方工作。 您甚至可以在用于测试的平台与被测试的平台之间进行混搭。
AltCover is a NuGet package but it's also available as .NET Core Global Tool which is awesome.
AltCover是一个NuGet程序包,但它也可以作为.NET Core全局工具使用,真棒。
dotnet tool install --global altcover.global
This makes "altcover" a command that's available everywhere without adding it to my project.
这使“ altcover”成为一个随处可用的命令,而无需将其添加到我的项目中。
That said, I'm going to follow the AltCover Quick Start and see how quickly I can get it set up!
就是说,我将遵循AltCover快速入门,并了解如何快速设置它!
I'll Install into my test project hanselminutes.core.tests
我将安装到测试项目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?
凉。 我的测试照常运行,但是现在我的测试文件夹中有一个coverage.xml。 如果愿意,我还可以生成LCov或Cobertura报告。 至此,我的coverage.xml几乎达到了一半。 那是很多有用的信息,但是我如何以人类可读的格式查看结果呢?
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!
这是OpenCover XML格式,我可以在覆盖率文件上运行ReportGenerator并获取一大堆HTML文件。 基本上是整个迷你网站!
I downloaded ReportGenerator and put it in its own folder (this would be ideal as a .NET Core global tool).
我下载了ReportGenerator并将其放在自己的文件夹中(这是作为.NET Core全局工具的理想选择)。
c:\ReportGenerator\ReportGenerator.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!
确保使用体面的targetDir,否则最终可能会在项目文件夹中杂乱地出现许多HTML文件。 您可能还考虑.gitignore生成的文件夹和coverage文件。 打开index.htm并查看所有这些重要信息!

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.
注意顶部的风险热点! 我有一个具有显着的NPath复杂度的CustomPageHandler和两个具有显着的循环复杂度的视图。
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.
还要检查出色的分支机构覆盖率,如覆盖率报告结果所示。 您可以看到EnableAutoLinks始终为true,因此我只测试了一个分支。 我可能想在此处添加阴性测试,并探讨EnableAutoLinks是否为false的任何副作用。

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!
请务必浏览AltCover及其完整的使用指南。 从全局工具,dotnet测试,MSBuild任务和PowerShell集成,可以通过多种方式运行它!
For use cases, see Use Cases.
有关用例,请参见用例。
For modes of operation, see Modes of Operation.
For driving AltCover from
dotnet test
, seedotnet test
integration.要从
dotnet test
驱动AltCover,请参阅dotnet test
集成。For driving AltCover from MSBuild, see MSBuild Tasks.
要从MSBuild驱动AltCover,请参阅MSBuild任务。
For driving AltCover and associated tools with Windows PowerShell or PowerShell Core, see PowerShell integration.
有关使用Windows PowerShell或PowerShell Core驱动AltCover和相关工具的信息,请参阅PowerShell集成。
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 give 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.
这里有很多很棒的工作,而我花了10分钟的时间才获得有关AltCover和.NET Core的出色报道。 史蒂夫在AltCover上感到很荣幸! 前往https://github.com/SteveGilham/altcover并给它加星号,文件问题(请客气)或提供帮助! 最重要的是,与您的朋友和同事共享如此酷的开源项目。
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.
赞助商:预览最新的JetBrains Rider,包括其内置的拼写检查,Blazor初始支持,部分C#7.3支持,增强的调试器,C#Interactive和重新设计的解决方案资源管理器。
翻译自: https://www.hanselman.com/blog/altcover-and-reportgenerator-give-amazing-code-coverage-on-net-core