【Azure DevOps系列】Azure DevOps生成代碼覆蓋率


前言

在做單元測試時,代碼覆蓋率可以作為我們衡量代碼質量的一個指標,本章我們將使用Azure DevOps幫助我們生成代碼覆蓋率的結果.Azure DevOps構建管道還是具有代碼覆蓋率選項的,在Visual Studio測試平台在已經集成了Coverlet格式的數據收集器,它其實並不難,它是可以開箱即用的。獲取Coverlet格式報告幾乎都是可以拿命令行參數去解決的。

在單元測試項目中需要引入nuget包coverlet.collector,當然只需要在單元測試項目中引用他,下面這個代碼片段是單元測試模板自動生成的,我只是引入了一個我自己的類庫。

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="coverlet.collector" Version="1.3.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" />
  </ItemGroup>
</Project>

如何在Azure DevOps中使用?

第一步是在構建之前對項目進行還原nuget包,這會將所有的包拉到構建代理的本地文件夾中.
還原項目包(dotnet restore)

- task: DotNetCoreCLI@2  
  displayName: 'dotnet restore'  
  inputs:  
    command: restore  

構建項目(dotnet build)

- task: DotNetCoreCLI@2  
  displayName: 'dotnet build'  
  inputs:  
    command: build  

運行單元測試,其實上面的管道任務都是非常簡單的,但是對於單元測試,我們需要設置dotnet cli將測試結果進行收集,搜集為cobertura格式,這是通過命令行參數來完成的。
正如下所示:
運行單元測試

- task: DotNetCoreCLI@2  
  displayName: 'dotnet test'  
  inputs:  
    command: test  
    projects: '**/XUnitTestProject1.csproj'  
    arguments: '--configuration $(BuildConfiguration) --collect "XPlat Code coverage" -- RunConfiguration.DisableAppDomain=true'  

當然我們可以在coverlet中了解更多的信息https://discoverdot.net/projects/coverlet

安裝報告生成工具

- task: DotNetCoreCLI@2
  displayName: Install ReportGenerator Global Tool
  inputs:
    command: custom
    custom: tool
    arguments: install dotnet-reportgenerator-globaltool -g

使用reportgenerator工具生成報告

- script: 'reportgenerator -reports:$(Agent.TempDirectory)/**/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/coverlet/reports -reporttypes:"Cobertura"'  
  displayName: 'Create reports'  

代碼報告發布到Azure DevOps
最后這一步做的是將剛才生成的所有信息上傳到Azure DevOps管道,這樣我們就可以在Azure DevOps Ui中查看覆蓋率的相關信息了。

- task: PublishCodeCoverageResults@1  
  displayName: 'Publish code coverage'  
  inputs:  
    codeCoverageTool: Cobertura  
    summaryFileLocation: '$(Build.SourcesDirectory)/coverlet/reports/Cobertura.xml' 

查看報告
執行構建管道后,結果將在構建的“代碼覆蓋率報告”選項卡中可見。
file

file

常見問題

1.如果在Azure DevOps Linux鏡像系統中是不可以的,可能會出現No executable found matching command "dotnet-reportgenerator" ,這個問題很頭疼,我們在安裝生成報告工具的時候並沒有將全局命令安裝成功,安裝后並沒有更新PATH,要解決這個問題要在調用reportgenerator另外執行一個CommandLine腳本:

 - task: CmdLine@2
   inputs:
     script: 'echo "##vso[task.prependpath]$HOME/.dotnet/tools"'

2.如何生成覆蓋率圖標?

  • 覆蓋率圖標:https://img.shields.io/azure-devops/coverage/{組織名稱}/{項目名稱}/2/{分支} Azure DevOps coverage (branch)
  • 單元測試個數:https://img.shields.io/azure-devops/tests/{組織名稱}/{項目名稱}/2/{分支}Azure DevOps tests (demo2)

Reference

https://github.com/microsoft/azure-pipelines-tasks/issues/9472

https://github.com/hueifeng/AzureDevOpsDemo/tree/demo2


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM