如何在 Azure DevOps 构建中为 asp.net 单元测试生成代码覆盖率报告 [英] How to generate code coverage report for asp.net unit tests in Azure DevOps build

本文介绍了如何在 Azure DevOps 构建中为 asp.net 单元测试生成代码覆盖率报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 azure 构建管道中生成 Asp.net 单元测试的代码覆盖率报告的指导.我的项目基于 .Net Framework 4.6.

I need guidance in generating code coverage report of Asp.net unit tests in azure build pipeline. My project is based on .Net Framework 4.6.

我能够使用 visual studio test" 任务运行所有单元测试.

I am able to run all the unit tests using "visual studio test" task.

我尝试了 报告生成器" 任务,但它需要 cobertura 或 jacoco 等 xml 文件,无法在构建管道中生成.

I tried the "report generator" task, but it require cobertura or jacoco etc xml files, which am unable to generate in the build pipeline.

期望 - 我想获得运行单元测试的代码覆盖率报告,该报告将显示完整的信息,如行覆盖率、分支覆盖率、函数覆盖率等,与报告生成器"生成的内容相同.

Expectation - I want to get code coverage report for the runned unit tests which will show complete information like the lines coverage, branch coverage, function coverage etc. same as what "report generator" generates.

注意:我能够在本地系统上使用 opencover 和 reportgenerator 生成报告,但无法找到在 azure 构建管道中执行相同操作的方法.

Note: I am able to generate the reports using opencover and reportgenerator on my local system but am unable to find a way to do the same in azure build pipeline.

推荐答案

要在 .Net 框架中获得代码覆盖率结果,您只需在Visual Studio 测试"任务中启用它:

To get the Code Coverage results in .Net framework you just need to enable it in the "Visual Studio Test" task:

如果你使用 .yml 构建语法是:

If you are use .yml builds the syntax is:

- task: VSTest@2
  inputs:
    codeCoverageEnabled: true

结果:

注意:如果您使用 Microsoft Hosted Agent,您将看到结果,如果您使用 Self Hosted Agent,您必须使用 Visual Studio Enterprise 版本才能看到代码覆盖率结果.

Note: if you use Microsoft Hosted Agent you will see the results, if you use Self Hosted Agent you must Visual Studio Enterprise version to see the Code Coverage results.

如果您想要更详细的代码覆盖率报告,您可以在 .Net 框架中使用 coverlet 通过在管道期间安装该工具然后生成报告.您可以使用 PowerShell 脚本:

If you want more detailed code coverage report you can use coverlet in .Net framework by install the tool during the pipeline and then generate the report. you can do with a PowerShell script:

dotnet tool install dotnet-reportgenerator --tool-path . --version 4.0.12
dotnet tool install coverlet.console --tool-path . --version 1.4.1
mkdir .
eports
$unitTestFile = gci -Recurse | ?{ $_.FullName -like "*bin*test*.dll" }
$coverlet = "$pwdcoverlet.exe"
& $coverlet $unitTestFile.FullName --target "dotnet" --targetargs "vstest $($unitTestFile.FullName) --logger:trx" --format "cobertura"
gci -Recurse |
?{ $_.Name -eq "coverage.cobertura.xml"} |
%{ &"$pwd
eportgenerator.exe" "-reports:$($_.FullName)" "-targetdir:reports" "-reportstypes:HTMLInline;HTMLChart" }

然后使用这些参数添加发布代码覆盖率"任务:

Then add "Publish code coverage" task with these parameters:

结果:

这篇关于如何在 Azure DevOps 构建中为 asp.net 单元测试生成代码覆盖率报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆