【问题标题】:Using Cobertura files for code coverage with SonarQube from Azure DevOps Pipelines通过 Azure DevOps Pipelines 中的 SonarQube 使用 Cobertura 文件进行代码覆盖
【发布时间】:2020-08-16 02:24:33
【问题描述】:

我有一个 dot net core 版本:'3.0.100',基于 'Ubuntu 16.04',我正在尝试将代码覆盖率推送到我们自托管的 SonarQube。

我一直在使用Coverlet 生成 Cobertura 文件,然后可以使用 PublishCodeCoverageResults@1 发布到 Devops 管道代码覆盖率查看器。

我无法将 cobertura.xml 文件推送到 sonarqube。

我读过this,在我看来,唯一提到cobertura 的是python 和flex。是否可以使用该文件来覆盖我的 C# 项目?

我一直在玩以下内容,但怀疑我在 extraProperties 中的内容不正确。

- task: SonarQubePrepare@4
  inputs:
    SonarQube: 'My SonarQube'
    scannerMode: 'MSBuild'
    projectKey: 'dcap'
    projectName: 'DCAP'
    extraProperties: 'sonar.flex.cobertura.reportPaths=**/DCAP.Testing.Unit/TestResults/*/coverage.cobertura.xml'

谢谢:-)

【问题讨论】:

    标签: azure-devops sonarqube


    【解决方案1】:

    是否可以使用该文件来覆盖我的 C# 项目?

    恐怕没有这种开箱即用的属性可以用Cobertura 格式覆盖C# 项目。

    正如您所读到的,cobertura 适用于 python 和 flex。对于 C#,我们需要使用 sonar.cs.dotcover.reportsPathssonar.cs.opencover.reportsPaths,格式为 dotCoverOpenCover

    要解决此问题,您可以尝试使用 Chameera Dulanga 提供的自定义 powershell 脚本作为解决方法:

    $Env:DOTNET_ROOT= (Get-Command dotnet).Path.Replace('dotnet.exe','sdk\2.1.300')
    dotnet tool install dotnet-reportgenerator-globaltool --tool-path . --version 4.0.12
    dotnet tool install coverlet.console --tool-path . --version 1.4.1
    mkdir .\reports
    $testDll = gci -Recurse | ?{ $_.FullName -like ("*bin\{0}\{1}" -f "$(BuildConfiguration)", "$(TestDllFile)") }
    $coverlet = "$pwd\coverlet.exe"
    & $coverlet $testDll.FullName --target "dotnet" --targetargs ("vstest {0} --logger:trx" -f $testDll.FullName) --format "cobertura"
    gci -Recurse |
    ?{ $_.Name -eq "coverage.cobertura.xml"} |
    %{ &"$pwd\reportgenerator.exe" ("-reports:{0}" -f $_.FullName) "-targetdir:reports" "-reportstypes:HTMLInline;HTMLChart" }
    

    你可以查看他的博客Running ASP.NET NUnit Tests in Azure DevOps Build Pipeline and Publishing Results to Sonar Cloud (LINK BROKEN)了解更多详情。

    希望这会有所帮助。

    【讨论】:

    • 该死的微软。这是垃圾。使用 Coverlet 在 Linux 上运行时 Azure DevOps 中的代码覆盖率选项卡需要 cobertura XML。但要使覆盖范围与 SonarQube 一起使用,还需要 OpenCover。纠正这种荒谬。任选其一。
    【解决方案2】:

    解决方案是使用MSBUILD Escaping 告诉coverlet 在单元测试套件的一次执行中发出cobertura 和opencover 结果文件。

    您可以使用两个文件发布 cobertura 一个用于 Azure DevOps 代码覆盖选项卡,并通过 extraProperties 将 opencover 一个提供给 SonarQubePrepare。

    这是我的管道中重要部分的摘录。

    variables:
      APEX_SOLUTION: 'Redacted.sln'
      BUILD_CONFIGURATION: 'Release'
      ARTIFACTORY_ROOT: 'Artifactory'
      REPOSITORY_CODE: 'redacted-generic-local'
      PRODUCT_PREFIX: 'BLAH'
      REPOSITORY_GROUP: 'Some/Path'
      REPOSITORY_APPLICATION: 'REDACTED'
      PUBLISH_FOLDER: 'publish'
      PACKAGING_FOLDER: 'packaging'
      COBERTURA_OUTPUT: 'coverage.cobertura.xml'
      OPENCOVER_OUTPUT: 'coverage.opencover.xml'
      IS_INTEGRATION_BRANCH: $[eq(variables['Build.SourceBranch'], 'refs/heads/develop')]
      SONAR_SERVER_ENDPOINT: 'SonarQube'
      SONAR_PROJECT_KEY: 'redacted-web'
      SONAR_PROJECT_NAME: 'REDACTED-Web'
      .
      .
      .
    - task: SonarQubePrepare@4
      displayName: 'Prepare analysis on SonarQube'
      inputs:
        SonarQube: $(SONAR_SERVER_ENDPOINT)
        scannerMode: MSBuild
        configMode: manual
        projectKey: $(SONAR_PROJECT_KEY)
        projectName: $(SONAR_PROJECT_NAME)
        extraProperties: |
          sonar.cs.opencover.reportsPaths="$(Build.SourcesDirectory)/TestResults/Coverage/$(OPENCOVER_OUTPUT)"
          sonar.cs.nunit.reportsPaths="$(Agent.BuildDirectory)/TestResult.xml"
          sonar.scm.exclusions.disabled=true
          sonar.exclusions=Redacted/wwwroot/**
          sonar.branch.name=$(Build.SourceBranchName)
          sonar.projectVersion=$(Build.BuildNumber)
      .
      .
      .
    - task: DotNetCoreCLI@2
      displayName: 'Unit Tests'
      inputs:
        command: 'test'
        projects: '$(APEX_SOLUTION)'
        arguments: '--configuration $(BUILD_CONFIGURATION) --logger "nunit;LogFilePath=$(Agent.BuildDirectory)/TestResult.xml" /p:CollectCoverage=true /p:CoverletOutputFormat="cobertura%2copencover" /p:CoverletOutput=$(Build.SourcesDirectory)/TestResults/Coverage/ /p:Threshold=50 /p:ThresholdType=branch /p:ThresholdStat=Average /p:Exclude="[Redacted.Views]*"'
        publishTestResults: true
    
    - task: PublishCodeCoverageResults@1
        displayName: 'Publish code coverage report'
        inputs:
          codeCoverageTool: 'Cobertura'
          summaryFileLocation: "$(Build.SourcesDirectory)/**/$(COBERTURA_OUTPUT)"
      .
      .
      .
    

    【讨论】:

    • 谢谢。我现在正在做其他事情,但应该在几个月后回到这个项目。那我去看看。
    【解决方案3】:

    我认为最简单的解决方案是配置 Coverlet 以输出多种格式(参见documentation)。

    对于本地测试,这将为 SonarQube 生成 opencover 格式,为 Azure DevOps 生成 cobertura 格式:

    dotnet test --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura,opencover
    

    用于集成到您的 Azure 管道中

    ...
    - task: SonarQubePrepare@4
      displayName: Prepare SonarQube
      inputs:
        SonarQube: 'SonarQube Enterprise'
        projectKey: $(ProjectKey)
        projectName: $(ProjectName)
        extraProperties: |
          sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/*.trx
          sonar.cs.opencover.reportsPaths=$(Agent.TempDirectory)/*/coverage.opencover.xml
    ...
    - task: DotNetCoreCLI@2
      displayName: Run tests
      inputs:
        command: test
        arguments: --configuration $(BuildConfiguration) --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura,opencover
    

    【讨论】:

      猜你喜欢
      • 2021-06-04
      • 2019-05-17
      • 2016-10-29
      • 2017-01-15
      • 2011-04-28
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      • 2021-08-13
      相关资源
      最近更新 更多