如何解决代码覆盖率 - Azure Pipelines SonarQube
我使用 Azure 管道来运行单元测试和 SonarQube 集成。当我使用 cobertura 进行单元测试时,我无法将代码覆盖率结果传递到 SonarQube,尽管我在管道日志中看到了结果。但是,当我使用 OpenCover 时,同样的事情也有效。
我也在管道日志中看到了这个警告,尽管我不确定这是否相关:“以下文件缺少责任信息”
当前管道如下
- 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 Test 阶段时,在此路径中获得测试结果 - E:\agent-5\2\s\test\ApiTest\coverage.cobertura.xml
如有任何建议,我们将不胜感激。
解决方法
您的步骤顺序需要从 test -> prepare for tests -> build -> analyze
更改为 prepare for tests -> build -> test -> 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
,
更新:显然,SonarQube 默认不允许发布 Cobertura 结果文件(或者至少我无法运行)。
使用了一种解决方法 - 我必须将 OpenCover 测试结果文件传递给 SonarQube。为了将结果推送到 Azure Devops,我必须使用 reportgenerator 工具将结果转换为 Cobertura,然后再将它们传递给 Azure Devops。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。