没有从Azure Devops .NET核心版本获得SonarCloud中的代码覆盖率

时间:2019-06-06 10:28:08

标签: .net-core azure-devops code-coverage sonarcloud

我已使用“带有SonarCloud的.NET Core”模板在Azure Devops中为我的.NET Core项目设置了管道。构建时,分析将在SonarCloud中运行,但代码覆盖率为0%(我的解决方案中有测试)。

无论我对构建进行何种配置调整,我都无法使代码覆盖范围正常工作。

我想念什么?

我碰到了这篇文章,https://dejanstojanovic.net/aspnet/2019/may/publishing-code-analysis-to-sonarcloud-from-azure-build-pipeline/实现了其中描述的powershell脚本,但在SonarCloud中仍然没有代码覆盖

我尝试按此处所述使用床罩,但仍然不满意 https://gunnarpeipman.com/aspnet/azure-devops-code-coverage/

我的管道包含以下任务

  • .NET Core-还原
  • 准备分析配置
  • .NET Core-构建
  • .NET Core-测试
  • 运行代码分析
  • 发布质量门结果

我的测试任务已配置:

参数:--configuration $(BuildConfiguration)

发布测试结果和代码覆盖率-已选中

在“运行代码分析”任务的控制台中,我得到:

10:43:54.7  Fetching code coverage report information from TFS...
10:43:54.702  Attempting to locate a test results (.trx) file...
10:43:54.753  Looking for TRX files in: C:\\TFSBuilds\\TJPYHG04-GHJ01\\_work\\475\\TestResults
10:43:54.755  No test results files found
10:43:54.81  Did not find any binary coverage files in the expected location.
10:43:54.811  Falling back on locating coverage files in the agent temp directory.
10:43:54.812  Searching for coverage files in C:\\TFSBuilds\\TJPYHG04-GHJ01\\_work\\_temp
10:43:54.814  No coverage files found in the agent temp directory.

2 个答案:

答案 0 :(得分:0)

  

没有从Azure Devops .NET核心版本获得SonarCloud中的代码覆盖范围

此问题可能是由vstest output path changed recently引起的:

  

vstest coverage文件的输出路径从   D:\a\1\s\TestResults\...D:\a\_temp\...

哪个破坏了管道中的后续脚本(例如将codecoverage.exe转换为xml,然后导入到sonarqube)。

Microsoft建议使用其余的API来检查测试伪像,然后将它们重新下载到构建代理。

有关此问题的更多调查,您可以检查线程Azure DevOps (VSTS) extension no longer import coverage and unit tests automatically进行问题跟踪。

幸运的是,SonarSourcer团队刚刚发布了SonarQube(v4.6.3)和SonarCloud(v1.6.3)扩展的新版本,以解决覆盖率问题和回归问题。

希望这会有所帮助。

答案 1 :(得分:0)

希望这个答案仍然与您相关。

最近,我遇到了与您类似的问题,我也正在使用Azure DevOps解决此问题。

这就是我解决的方法。

第1步-将目录更改为单元测试子文件夹(与单元测试.csproj文件相同),然后运行以下dotnet命令。

dotnet add package coverlet.msbuild

第2步-将以下内容添加到SonarCloudPrepare Task的其他属性中,或直接附加到yml文件中(如果您使用yml而不是经典编辑器)

    extraProperties: |
      sonar.exclusions=**/obj/**,**/*.dll
      sonar.cs.opencover.reportsPaths=$(Build.SourcesDirectory)/**/coverage.opencover.xml
      sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/*.trx

由您选择配置目录。

或者您也可以在您的仓库中创建一个名为sonar-project.properties的文件,并将所有相关的SonarCloud属性存储在其中。

第3步-将以下内容添加到您的dotnet测试任务中

- task: DotNetCoreCLI@2
  inputs:
    command: 'test'
    arguments: '--configuration $(BuildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=opencover --logger trx'
    testRunTitle: 'dotnet test'

您可能会注意到“发布测试结果和代码覆盖率”有一个复选框,但我仍然更喜欢使用/p:CollectCoverage=true

您还可以在本地测试以运行dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover --logger trx命令,并且coverage.opencover.xml将在您的单元测试文件夹中生成。

有关更多参数及其说明,请参见参考2和3。

边注 :如果要进行Sonar测试包含属性,请注意Sonar.Tests和Sonar.Test.Inclusions的测试是不同的。大声笑

参考文献:

  1. Using SonarCloud in Azure pipelines
  2. Analysis Parameters (official docs)
  3. Test Coverage & Execution (official docs)

希望这会有所帮助:)