如何在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测试" 任务运行所有单元测试.

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 Framework中使用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 .\reports
$unitTestFile = gci -Recurse | ?{ $_.FullName -like "*bin\*test*.dll" }
$coverlet = "$pwd\coverlet.exe"
& $coverlet $unitTestFile.FullName --target "dotnet" --targetargs "vstest $($unitTestFile.FullName) --logger:trx" --format "cobertura"
gci -Recurse |
?{ $_.Name -eq "coverage.cobertura.xml"} |
%{ &"$pwd\reportgenerator.exe" "-reports:$($_.FullName)" "-targetdir:reports" "-reportstypes:HTMLInline;HTMLChart" }

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

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

结果:

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

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