来自VSTS的SonarQube代码覆盖范围

时间:2018-02-23 09:46:18

标签: sonarqube azure-devops code-coverage

我正在使用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格式,因为我似乎无法做到这一点。

有人使用SonarQube成功使用VSTS并将代码覆盖率结果也提供给SonarQube吗?你是如何设置它的?

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

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

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...

3 个答案:

答案 0 :(得分:5)

VSTS扩展程序应自动导入覆盖率结果,但目前很快就会修复(希望如此):SONARMSBRU-339

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

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

答案 1 :(得分: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安装了“ Inline PowerShell”任务,并将其放置在构建管道的“运行代码分析”步骤之前。

答案 2 :(得分:0)

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

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

Visual Studio Test Agent似乎是免费的。无需为Visual Studio Enterprise付费。