【问题标题】:SonarQube Code Coverage from VSTSVSTS 的 SonarQube 代码覆盖率
【发布时间】:2019-08-28 22:47:17
【问题描述】:

我正在使用 VSTS 托管代理 VS2017 来运行构建、测试和 SonarQube 分析。我正在使用市场上的 SonarQube 扩展 (https://marketplace.visualstudio.com/items?itemName=SonarSource.sonarqube)。正在使用的任务版本是 4,它使用 SonarQube Scanner for MSBuild 4.0.2。

分析运行良好,但它没有将代码覆盖率结果转换为 SonarQube。我可以在 VSTS(构建详细信息)中看到代码覆盖率分析,但在 SonarQube 中看不到。

据我了解,问题在于 VSTS 生成了一个扩展名为 .codecoverage 的二进制文件。 SonarQube 不知道如何处理这个文件。我无法将其转换为 XML 格式,因为我似乎没有办法做到这一点。

是否有人成功地将 VSTS 与 SonarQube 一起使用,并将代码覆盖率结果也发送到了 SonarQube?你是怎么设置的?

编辑 1
我正在使用的 SonarQube 版本是:6.7 版(内部版本 33306),社区版

编辑 2
分析结束实际上会生成 xml 文件,如下面的 cmets 所述。这是来自日志:

2018-03-06T11:14:56.4189055Z 11:14:56.417  Attempting to locate the 
CodeCoverage.exe tool...
2018-03-06T11:14:56.4210147Z 11:14:56.42  Attempting to locate the 
CodeCoverage.exe tool using setup configuration...
2018-03-06T11:14:56.4345085Z 11:14:56.433  Code coverage command line tool: 
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Team 
Tools\Dynamic Code Coverage Tools\CodeCoverage.exe
2018-03-06T11:14:56.4349101Z 11:14:56.434  Fetching code coverage report 
information from TFS...
2018-03-06T11:14:56.4363869Z 11:14:56.435  Attempting to locate a test 
results (.trx) file...
2018-03-06T11:14:57.0458245Z 11:14:57.044  Looking for TRX files in: 
D:\a\1\TestResults, D:\a\1\s\TestResults
2018-03-06T11:14:57.0462747Z 11:14:57.045  Located a test results file: 
D:\a\1\s\TestResults\VssAdministrator_factoryvm-az436_2018-03-
06_11_13_09.trx
2018-03-06T11:14:57.0600587Z 11:14:57.059  One code coverage attachment was 
found in the trx file: factoryvm-az436\VssAdministrator_factoryvm-az436 
2018-03-06 11_11_34.coverage
2018-03-06T11:14:57.0602504Z 11:14:57.059  Absolute path to coverage file: 
D:\a\1\s\TestResults\VssAdministrator_factoryvm-az436_2018-03-
06_11_13_09\In\factoryvm-az436\VssAdministrator_factoryvm-az436 2018-03-06 
11_11_34.coverage
2018-03-06T11:14:57.0691948Z 11:14:57.068  Executing file C:\Program Files 
(x86)\Microsoft Visual Studio\2017\Enterprise\Team Tools\Dynamic Code 
Coverage Tools\CodeCoverage.exe
2018-03-06T11:14:57.0692731Z   Args: analyze 
/output:D:\a\1\s\TestResults\VssAdministrator_factoryvm-az436_2018-03-
06_11_13_09\In\factoryvm-az436\VssAdministrator_factoryvm-az436 2018-03-06 
11_11_34.coveragexml D:\a\1\s\TestResults\VssAdministrator_factoryvm-
az436_2018-03-06_11_13_09\In\factoryvm-az436\VssAdministrator_factoryvm-
az436 2018-03-06 11_11_34.coverage 
2018-03-06T11:14:57.0694963Z   Working directory: 
D:\a\1\s\TestResults\VssAdministrator_factoryvm-az436_2018-03-
06_11_13_09\In\factoryvm-az436
2018-03-06T11:14:57.0695792Z   Timeout (ms):60000
2018-03-06T11:14:57.0699007Z   Process id: 3540
2018-03-06T11:14:58.7847582Z 11:14:58.783  Process returned exit code 0
2018-03-06T11:14:58.7858908Z 11:14:58.784  Updating project info files with 
code coverage information...

【问题讨论】:

  • @jessehouwing 它是 6.7 版(内部版本 33306),社区版。将其添加到最初的问题中。
  • 将 /d:sonar.verbose=true /d:sonar.cs.vscoveragexml.reportsPaths=**/*.coveragexml 添加到准备 SonarQube 分析任务的附加设置中,并将 System.debug 变量设置为true,然后在 OneDrive 上排队构建和共享日志。
  • 在sonarqube端分析任务中.coverage文件会被转换成coveragexml,所以你可以分享sonarqube端分析任务的详细日志。
  • 我已将日志添加到帖子中,并且似乎正在正确生成 xml 文件。无论如何,它不会传递给 SonarQube。

标签: sonarqube azure-devops code-coverage


【解决方案1】:

VSTS 扩展应该自动导入覆盖结果,但目前有一个回归(希望很快)修复:SONARMSBRU-339

同时,VSTS extension documentation in "Analysing a .NET solution" 中解释了一种解决方法:在Additional Properties 文本区域中,添加以下属性:

sonar.cs.vscoveragexml.reportsPaths=**/*.coveragexml

【讨论】:

  • 我已阅读有关此问题和解决方法的信息。问题是,没有可用的 .coveragexml 文件。只有一个二进制 .coverage 文件。因此,要使此解决方法真正起作用,我需要将二进制文件转换为 xml 格式。
  • 您能否检查代理(正在运行您的构建)是否具有 CodeCoverage 工具?请看我自己的日志:gist.github.com/bellingard/993979606a7a536e85cf2cbefbb9525a
  • 我从分析部分的 VSTS 端添加了日志(不是全部)。似乎coveragexml文件正在正确生成,但它似乎仍然没有到达SonarQube。
  • 一开始我一定是错误地添加了reportsPaths 属性。我重新跟踪了我的步骤,现在它可以工作了。感谢您的帮助!
【解决方案2】:

即使我设置了“sonar.verbose=true”、“system.debug=true”,我也无法看到“coveragexml”文件。虽然,我找到了另一种效果很好的方法,即使用 PowerShell 脚本将 '.coverage' 文件转换为 '.coveragexml':

Get-ChildItem -Path $(Agent.TempDirectory) -Include "*.coverage" -File -Recurse | % {
$outfile = "$([System.IO.Path]::GetFileNameWithoutExtension($_.FullName)).coveragexml"
$output = [System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($_.FullName), $outfile)
"Analyse '$($_.Name)' with output '$outfile'..."
.$env:USERPROFILE\.nuget\packages\microsoft.codecoverage\15.8.0\build\netstandard1.0\CodeCoverage\CodeCoverage.exe analyze /output:$output $_.FullName
}

要使其正常工作,请确保为 Marketplace 安装了“内联 PowerShell”任务,并将其放置在构建管道中的“运行代码分析”步骤之前。

【讨论】:

    【解决方案3】:

    对于使用自托管代理的用户,您还可以安装Visual Studio Test Agent 2019

    它包含 SonarQubeAnalyze@4 Azure DevOps 任务将找到的 CodeCoverage.exe 文件。根据SonarQube documentation webpage,SonarQube 将能够将 .coverage 文件转换为 .coveragexml。

    Visual Studio 测试代理似乎是免费的。无需为 Visual Studio Enterprise 付费。

    【讨论】:

      猜你喜欢
      • 2018-01-11
      • 2018-09-02
      • 1970-01-01
      • 2021-06-04
      • 2014-08-19
      • 2015-12-28
      • 2018-11-13
      • 2018-04-14
      • 2016-09-13
      相关资源
      最近更新 更多