【问题标题】:Code coverage - Azure Pipelines SonarQube代码覆盖率 - Azure Pipelines SonarQube
【发布时间】:2021-06-04 09:31:53
【问题描述】:

我使用 Azure 管道运行单元测试和 SonarQube 集成。当我使用 cobertura 进行单元测试时,我无法将代码覆盖率结果传递到 SonarQube,尽管我在管道日志中看到了结果。但是,当我使用 OpenCover 时,这同样适用。

我在管道日志中也看到了这个警告,尽管我不确定这是否相关:“Missing blame information for the following files”

管道目前如下所示

 - task: DotNetCoreCLI@2
    inputs:
      command: ‘test’
      projects: | 
        <test-proj>
      arguments: ‘/p:CollectCoverage=true /p:CoverletOutputFormat=cobertura’
    displayName: ‘Run Tests’

 - task: SonarQubePrepare@4
    inputs: 
      SonarQube: <Service-Endpoint>
      scannerMode: ‘MSBuild’
      projectKey: ‘test:service’
      projectName: ‘test:service’
      extraproperties: | 
         sonar.flex.cobertura.reportPaths=$(Build.SourcesDirectory)/**/coverage.cobertura.xml 
  
 - task: DotNetCoreCLI@2
     <Running DotNet build>

 - task: SonarAnalyze@4
  

在运行 DotNet 测试阶段时,测试结果是在这个路径中获得的 - E:\agent-5\2\s\test\ApiTest\coverage.cobertura.xml

任何建议将不胜感激。

【问题讨论】:

  • 您的问题进展如何?下面的回复有用吗?

标签: azure azure-devops sonarqube azure-pipelines sonarscanner


【解决方案1】:

更新:显然,SonarQube 默认不允许发布 Cobertura 结果文件(或者至少我无法让它运行)。

使用了一种解决方法 - 我必须将 OpenCover 测试结果文件传递给 SonarQube。为了将结果推送到 Azure Devops,我必须使用 reportgenerator 工具将结果转换为 Cobertura,然后再将它们传递给 Azure Devops。

【讨论】:

【解决方案2】:

您的步骤顺序需要从test -&gt; prepare for tests -&gt; build -&gt; analyze 更改为prepare for tests -&gt; build -&gt; test -&gt; analyze,如:

 - task: SonarQubePrepare@4
    inputs: 
      SonarQube: <Service-Endpoint>
      scannerMode: ‘MSBuild’
      projectKey: ‘test:service’
      projectName: ‘test:service’
      extraproperties: | 
        sonar.flex.cobertura.reportPaths=$(Build.SourcesDirectory)/**/coverage.cobertura.xml 
  
 - task: DotNetCoreCLI@2
     <Running DotNet build>

 - task: DotNetCoreCLI@2
    inputs:
      command: ‘test’
      projects: | 
        <test-proj>
      arguments: ‘/p:CollectCoverage=true /p:CoverletOutputFormat=cobertura’
    displayName: ‘Run Tests’

 - task: SonarAnalyze@4

【讨论】:

  • 代码覆盖率报告在 Azure 管道上运行良好。但是,当使用 cobertura 时,它们不会传递给 SonarQube。
  • 一旦你准备好 sonarqube 任务,它将开始监控构建产生的日志并将它们发送到 SonarQube,这也将获取代码覆盖率,因此任务的顺序很重要。
猜你喜欢
  • 2018-01-11
  • 1970-01-01
  • 2014-08-19
  • 2015-12-28
  • 2018-11-13
  • 2016-09-13
  • 1970-01-01
  • 2020-08-16
  • 2018-04-14
相关资源
最近更新 更多