在azure devops门户上查看代码覆盖率报告 [英] view code coverage report on azure devops portal

查看:379
本文介绍了在azure devops门户上查看代码覆盖率报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为azure devops构建管道的一部分,我正在运行NUnit测试(.Net Framework 4.5中的项目).

- 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'

但是我看不到覆盖率报告,只能看到覆盖率结果的下载链接...

如何将.coverage报告转换为JaCoCo格式?还是直接以JaCoCo格式生成报告?

我已经看到了.Net Core的一些解决方案(解决方案

更新:

根据Azure Devops的报告生成器 封面 工具直接

  • 使用 dotnet-vstest 命令,用于通过 Coverlet
  • 运行测试
  • 发布使用报告生成器 Cobertura 格式覆盖范围结果生成的报告


  • 不要使用VS测试任务

    运行此任务将使您可以通过一个简单的复选框来收集覆盖率,但随后您将有机会为代码覆盖率标签

    提供内容



    直接安装工具

    使用Powershell任务(或类似任务)直接安装 Coverlet Report Generator 工具.这样,您就可以在非 .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
    
    



    通过Coverlet使用dotnet vstest

    据我了解,dotnet test .Net Framework 项目/程序集中不能很好地发挥作用.但是,我们仍然可以使用dotnet命令,该命令将在代理计算机上,但是我们需要使用它作为获取 vstest.console.exe 的机制. > 如您链接的文章所述,

    覆盖工具将以 Cobertura 格式输出覆盖结果.

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



    发布结果



    完整的脚本示例

    注意::该脚本非常粗糙,因此可以将其用作针对个人情况的思考练习.

    "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" }
    


    如果要使用 Coverlet 命令来弄清楚引号的转义,请 您并不孤单 .我从 PSCX 比我想承认的要多得多,因此我可以看到我正在进行的.exe呼叫实际上得到了什么.



    结果!

    ...因为这确实很重要






    原始答案:


    由于您提到的链接文章的安装和使用报告生成器全局工具的方式,我认为您仍然可以遵循这些准则来创建HTML内联和图表报告类型.

    我不确定这篇文章说的是什么意思或如何工作,

    重点是报告类型:使用HTMLInLine启用Azure DevOps页面上的输出. Azure DevOps覆盖率页面在网络上显示 index.html .

    我了解您可以使用该工具从 .xml 覆盖率结果创建HTML报告,然后发布覆盖率结果并与Publish Code Coverage任务一起报告.

    >

    因此,似乎您所需要的只是具有 .coverage 工具的 .xml 格式.

    我没有使其直接在Powershell中工作,但是您可以按照link), but none for .Net framework

    解决方案

    Update:

    As per the release to Azure Devops for Sprint 150

    When publishing code coverage reports, you no longer need to specify HTML files.

    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.

    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.

    1. Don't run tests with the VS Test Task in Azure
    2. Install the Report Generator and Coverlet tools directly
    3. Use dotnet-vstest command for running tests through Coverlet
    4. Publish reports generated with Report Generator and Cobertura format coverage results



    Don't use the 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



    Install tools directly

    Use a Powershell task (or similar) to install the Coverlet and Report Generator tools directly. This allows you to use them on projects that are not .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

    It's my understanding that dotnet test doesn't play nice with .Net Framework projects/assemblies. 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.

    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 $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.



    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.

    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. Azure DevOps Coverage page show index.html on the web.

    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.

    So it seems all you need is to have an .xml format of the .coverage tool.

    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.

    这篇关于在azure devops门户上查看代码覆盖率报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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