c# - Azure DevOps 构建管道中的 ASP.NET Core Web 应用程序控制器需要 100% 的代码覆盖率
问题描述
在我们的项目中,我们要求所有控制器方法都应该至少有一个测试。
如果我们的测试失败,我们的构建就会失败,但现在我们正在使用本指南手动检查代码覆盖率:
基本上这意味着运行两个命令:
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput="Path\To\TestProject\TestResults\coverage.cobertura.xml"
reportgenerator "-reports:Path\To\TestProject\TestResults\coverage.cobertura.xml" "-targetdir:coveragereport" -reporttypes:Html
注意:如果您运行dotnet test --collect:"XPlat Code Coverage"
,您可以获得一个coverage.cobertura.xml
文件,但如果您使用 msbuild,/p:CollectCoverage=true
则必须将包添加dotnet add package coverlet.msbuild
到测试项目一次。
https://github.com/coverlet-coverage/coverlet/issues/201
然后我们得到这样的报告:
在这种情况下,我们的线路覆盖率不是很好,但我们的控制器有 100% 的线路覆盖率。检查一个特定的命名空间Project.Web.Controllers
是否具有 100% 的行覆盖率是可以的。
我们不能使用正常的代码覆盖率结果来使构建失败,因为我们只想在未测试控制器的情况下使构建失败。
https://gunnarpeipman.com/azure-devops-check-code-coverage/
https://stackoverflow.com/a/60894835/3850405
有什么办法可以很好地做到这一点,还是我们需要阅读coverage.cobertura.xml
文件并查看<class name="Project.Web.Controllers
例如?
解决方案
找到了它的命令!
关键是使用/p:Include="[*]Project.Web.Controllers.*" /p:Threshold=100 /p:ThresholdType=line
完成命令:
dotnet test /p:CollectCoverage=true /p:Include="[*]Project.Web.Controllers.*" /p:Threshold=100 /p:ThresholdType=line
https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/MSBuildIntegration.md
经过:
失败:
推荐阅读
- android - RxJava2:处理订阅者后如何避免 InterruptibleException?
- pycurl - 无法在 kali linux 上运行 theharvester。致命异常:Pycurl:链接时版本比编译时版本旧
- url - SWIFTUI - 如何创建链接到 URL 的图像?
- c# - AssemblyLoadContext 是否隔离静态变量?
- python - 数据分组问题,但基于“窗口”
- php - 编译失败:偏移量 12 处的字符类范围无效
- javascript - 有没有办法 console.log 一个 html 对象的内容?
- reactjs - API调用一旦执行就返回promise对象?
- mysql - 如何将我的路由参数与@mysql/x devAPI 连接起来?
- c# - 如何按照 Fluent Builder 模式使用回调将值添加到上下文