繁体   English   中英

Azure DevOps 构建管道中的 ASP.NET 核心 web 应用程序控制器需要 100% 的代码覆盖率

[英]Require 100% code coverage for ASP.NET Core web application controllers in Azure DevOps build pipeline

在我们的项目中,我们要求所有 controller 方法都应至少进行一项测试。

如果我们的测试失败,我们的构建就会失败,但现在我们正在使用本指南手动检查代码覆盖率:

https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-code-coverage?tabs=windows#generate-reports

基本上这意味着运行两个命令:

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

Note: If you run dotnet test --collect:"XPlat Code Coverage" you can get a coverage.cobertura.xml file but if you use msbuild with /p:CollectCoverage=true you will have to add the package dotnet add package coverlet.msbuild到测试项目一次。

https://github.com/coverlet-coverage/coverlet/issues/201

然后我们得到这样的报告:

在此处输入图像描述

在这种情况下,我们的线路覆盖率不是很好,但我们的控制器有 100% 的线路覆盖率。 检查像Project.Web.Controllers这样的特定命名空间是否具有 100% 的行覆盖率是可以的。

我们不能使用正常的代码覆盖率结果来使构建失败,因为我们只想在未测试控制器的情况下使构建失败。

https://docs.microsoft.com/en-us/azure/devops/pipelines/test/review-code-coverage-results?view=azure-devops

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

经过:

在此处输入图像描述

失败:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM