简体   繁体   English

在 azure devops 门户上查看代码覆盖率报告

[英]view code coverage report on azure devops portal

I am running the NUnit tests (project in .Net Framework 4.5), as part of azure devops build pipeline.我正在运行 NUnit 测试(.Net Framework 4.5 中的项目),作为 azure devops 构建管道的一部分。

- task: VSTest@2
  inputs:
    testAssemblyVer2: 'tests/**/*.Tests.dll'
    pathtoCustomTestAdapters: '$(Build.SourcesDirectory)/packages'
    codeCoverageEnabled: true
  displayName: 'NUnit Testing'

- task: PublishCodeCoverageResults@1
  inputs:
    codeCoverageTool: JaCoCo
    summaryFileLocation: '$(Common.TestResultsDirectory)/**/*.xml'
  displayName: 'Publish Code Coverage'
  //        summaryFileLocation: '$(Common.TestResultsDirectory)/**/*.coverage'

But I am not able to see the coverage report, all I see the download link for coverage results...但是我看不到覆盖率报告,只能看到覆盖率结果的下载链接...

代码覆盖下载链接

How can I convert the .coverage report to JaCoCo format?如何将 .coverage 报告转换为 JaCoCo 格式? OR generate the report directly in JaCoCo format?还是直接以 JaCoCo 格式生成报告?

I have seen some solution for .Net Core ( link ), but none for .Net framework我已经看到了一些针对 .Net Core( 链接)的解决方案,但没有针对 .Net 框架

Update: 更新:

As per the release to Azure Devops for Sprint 150 根据Azure Devops for Sprint 150的发布

When publishing code coverage reports, you no longer need to specify HTML files. 发布代码覆盖率报告时,您不再需要指定HTML文件。

Therefore, the script in my illustration no longer needs to use the report generator tool directly to create the html report, and when publishing the coverage results, the directory containing those html reports doesn't need to be specified. 因此,我的插图中的脚本不再需要直接使用报表生成器工具来创建html报表,并且在发布coverage结果时,不需要指定包含这些html报表的目录。

Edit: 编辑:


The trick I've found for getting the coverage results from a .Net Framework project to show up on the Code Coverage tab is in the same line of thought to your linked article . 我发现从.Net Framework项目获得覆盖结果以显示代码覆盖率选项卡的技巧与链接文章的思路方式相同。

  1. Don't run tests with the VS Test Task in Azure 不要在Azure中使用VS Test Task运行测试
  2. Install the Report Generator and Coverlet tools directly 直接安装Report GeneratorCoverlet工具
  3. Use dotnet-vstest command for running tests through Coverlet 使用DOTNET-vstest命令通过运行测试
  4. Publish reports generated with Report Generator and Cobertura format coverage results 发布使用Report GeneratorCobertura格式覆盖结果生成的报告


Don't use the VS Test Task 不要使用VS Test Task

Running this task will allow you to collect coverage with a simple checkbox, but you then surrender your opportunity to provide the content for the Code Coverage Tab 运行此任务将允许您使用简单的复选框收集覆盖范围,但您放弃了为代码覆盖率选项卡提供内容的机会

没有VsTest任务



Install tools directly 直接安装工具

Use a Powershell task (or similar) to install the Coverlet and Report Generator tools directly. 使用Powershell任务(或类似)直接安装CoverletReport Generator工具。 This allows you to use them on projects that are not .Net Core . 这允许您在不是.Net Core的项目上使用它们。

"install tools:"
&dotnet tool install dotnet-reportgenerator-globaltool --tool-path . --version 4.0.12
&dotnet tool install coverlet.console --tool-path . --version 1.4.1



Use dotnet vstest through coverlet 通过coverlet使用dotnet vstest

It's my understanding that dotnet test doesn't play nice with .Net Framework projects/assemblies. 据我所知, dotnet test.Net Framework项目/程序集不起作用。 However, we can still use the dotnet command, which we know will be on the agent machine, but we need to use it as a mechanism to get to the vstest.console.exe . 但是,我们仍然可以使用dotnet命令,我们知道它将在代理机器上,但是我们需要将它作为一种机制来访问vstest.console.exe

The Coverlet tool, as mentioned in the article you linked, will output coverage results in Cobertura format if you tell it to do so. 如您所链接的文章中所述, Coverlet工具将以Cobertura格式输出覆盖率结果,如果您这样做的话。

&$coverlet $unitTestFile.FullName --target "dotnet" --targetargs "vstest $($unitTestFile.FullName) --logger:trx" --format "cobertura"


Publish results 发布结果

使用发布代码覆盖任务



Complete script sample 完整的脚本示例

note: this script is pretty rough, so use it as a thought exercise for your individual situation. 注意:这个脚本非常粗糙,所以请将它作为个人情境的思考练习。

"install tools:"
&dotnet tool install dotnet-reportgenerator-globaltool --tool-path . --version 4.0.12
&dotnet tool install coverlet.console --tool-path . --version 1.4.1

"`nmake reports dir:"
mkdir .\reports

"`nrun tests:"
$unitTestFile = gci -Recurse | ?{ $_.FullName -like "*bin\*UnitTestProject2.dll" }
Write-Host "`$unitTestFile value: $unitTestFile"

$coverlet = "$pwd\coverlet.exe"

"calling $coverlet for $($unitTestFile.FullName)"
&$coverlet $unitTestFile.FullName --target "dotnet" --targetargs "vstest $($unitTestFile.FullName) --logger:trx" --format "cobertura"

"`ngenerate report(s)"
gci -Recurse | 
    ?{ $_.Name -eq "coverage.cobertura.xml" } | 
    %{ &"$pwd\reportgenerator.exe" "-reports:$($_.FullName)" "-targetdir:reports" "-reporttypes:HTMLInline;HTMLChart" }

If you're struggling to figure out the escaping of quotes and such with the Coverlet command, YOU ARE NOT ALONE . 如果你正在努力找出引号的转义和使用等命令, 你并不孤单 I used the echoargs commandlet from PSCX more times than I care to admit so I could see what was actually getting provided to the .exe calls I was making. 我使用PSCXechoargs命令行开关的时间比我承认的要多,所以我可以看到实际上为我正在制作的.exe调用提供了什么。



The Results!! 结果!!

...because that's really what matters ......因为那才是最重要的

在此输入图像描述



在此输入图像描述




Original Answer: 原答案:


Because of the way the linked article you mentioned is installing and using the report generator global tool I would think you can still follow those guidelines for creating the HTML inline and chart report types. 由于您提到的链接文章正在安装和使用报表生成器全局工具的方式,我认为您仍然可以遵循这些指南来创建HTML内联和图表报表类型。

I'm not sure what is meant or how it works when the article says, 文章说,我不确定它的含义或工作原理,

The point is the reporttypes: Use HTMLInLine for enabling the output on the Azure DevOps page. 重点是reporttypes:使用HTMLInLine在Azure DevOps页面上启用输出。 Azure DevOps Coverage page show index.html on the web. Azure DevOps Coverage页面在Web上显示index.html

I'm understanding that you can use the tool to create the HTML report from the .xml coverage results, and then publish the coverage results and report together with the Publish Code Coverage task. 我理解您可以使用该工具从.xml覆盖率结果创建HTML报告,然后将发布覆盖率结果和报告与Publish Code Coverage任务一起Publish Code Coverage

So it seems all you need is to have an .xml format of the .coverage tool. 因此,似乎所有你需要的是有.coverage工具的.xml格式。

I didn't get it working in straight powershell, but you could follow the instructions from the Report Generator documentation to write a C# utility to access the Coverage.Analysis library. 我没有直接使用PowerShell,但您可以按照Report Generator文档中的说明编写C#实用程序来访问Coverage.Analysis库。

For anyone looking for code coverage in Azure Devops (using classic editor, without Yaml), in current .NET (core) 5, with xUnit tests:对于在当前的 .NET(核心)5 中使用 xUnit 测试在 Azure Devops(使用经典编辑器,不带 Yaml)中寻找代码覆盖率的任何人:

  1. In your xUnit test project, add following (it generally comes by default in .NET 5, xUnit template now):在您的 xUnit 测试项目中,添加以下内容(它通常在 .NET 5 中默认提供,现在 xUnit 模板):

    <PackageReference Include="coverlet.collector" Version="3.0.3" />

    Keep checking for new version.继续检查新版本。

  2. Head to Azure devops, create pipeline using classic editor.前往 Azure DevOps,使用经典编辑器创建管道。 Do the restore, build steps.执行恢复,构建步骤。 (Or you can choose dotnet core template as below): (或者您可以选择 dotnet 核心模板如下): 选择 dotnet 核心管道模板

  3. In the test command of dotnet core task, add argument - --collect:"XPlat Code Coverage" .在 dotnet core 任务的测试命令中,添加参数 - --collect:"XPlat Code Coverage" Remember "XPlat Code Coverage" is friendly name and case sensitive.请记住“XPlat 代码覆盖率”是友好名称且区分大小写。 Your test command would look like:您的测试命令如下所示: 在此处输入图片说明 Check this checkbox if you want to publish your test results: Publish test results and code coverage , but it won't publish code coverage.如果要Publish test results and code coverage请选中此复选框: Publish test results and code coverage ,但不会发布代码覆盖率。 The functionality is not yet working (at least not in non-windows).该功能尚未起作用(至少在非 Windows 中不起作用)。

  4. Next add - Publish code coverage results task.下一步添加 - Publish code coverage results任务。 Choose "Code coverage tool" as "Cobertura" and in "Summary file" field, add $(Agent.TempDirectory)/**/coverage.cobertura.xml .选择“代码覆盖工具”作为“Cobertura”,并在“摘要文件”字段中添加$(Agent.TempDirectory)/**/coverage.cobertura.xml Looks like this:看起来像这样: 管道中的 dotnet 测试任务配置

  5. Save and Queue (in any agent, I use Ubuntu) and see the result once pipeline run completes:保存并排队(在任何代理中,我使用 Ubuntu)并在管道运行完成后查看结果: 流水线运行结果

  6. Coverage report tab:覆盖率报告选项卡: 代码覆盖率总结

  7. HTML Coverage reports and coverage cobertura xml are published as artifacts: HTML 覆盖率报告和覆盖率 cobertura xml 作为工件发布: 覆盖率报告

You can use Publish Code Coverage Results task in azure devops pipeline to see code coverage result in Jacoco format. 您可以在azure devops管道中使用“发布代码覆盖率结果”任务来查看Jacoco格式的代码覆盖率结果。

for further information about setup and configuration , please check the blog in MSDN 有关设置和配置的更多信息,请查看MSDN中的博客

https://docs.microsoft.com/hi-in/azure/devops/pipelines/tasks/test/publish-code-coverage-results?view=tfs-2015#q--a https://docs.microsoft.com/hi-in/azure/devops/pipelines/tasks/test/publish-code-coverage-results?view=tfs-2015#q--a

Hope it helps. 希望能帮助到你。

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

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