• 【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="..ClassLibrary1ClassLibrary1.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

  • 相关阅读:
    Hadoop源代码分析(五)
    使用Solr Data Import的deltaimport功能
    hbasewriter
    Hadoop源代码分析(四)
    lucene .NET 搜索图片 功能实现
    char类型与string类型的区别
    Windows程序设计:'SM_ MOUSEWHEELPRESENT' : undeclared identifier解决办法
    汇编里的栈空间
    在汇编源程序中,数据不能以字母开头
    中值
  • 原文地址:https://www.cnblogs.com/yyfh/p/13588450.html
Copyright © 2020-2023  润新知