我正在尝试将我的项目连接到 sonarcloud 中。我使用 Azure Devops Yaml 管道构建和启动我的测试。这是正在发生的事情的摘要。
首先我有一个准备分析任务:
- task: SonarCloudPrepare@1
displayName: 'Prepare analysis on Sonarcloud'
inputs:
SonarCloud: 'Sonarcloud'
organization: ***redacted***
scannerMode: 'MSBuild'
projectKey: ***redacted***
projectName: ***redacted***
extraProperties: |
sonar.exclusions=**/obj/**,**/*.dll
sonar.branch.name=$(Build.SourceBranchName)
sonar.cs.opencover.reportsPaths=$(Build.SourcesDirectory)/**/coverage.opencover.xml
sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/*.trx
然后我建立项目,这里一切都很好。然后我启动测试,并使用报告生成器为我的 azure 管道创建一个 html 报告:
- task: DotNetCoreCLI@2
displayName: dotnet test
inputs:
command: test
arguments: '--configuration $(BuildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=opencover --logger trx'
projects: 'Tests/**/*.csproj'
nobuild: true
- script: |
dotnet tool install -g dotnet-reportgenerator-globaltool
reportgenerator "-reports:$(Build.SourcesDirectory)/**/coverage.opencover.xml" "-targetdir:$(Build.SourcesDirectory)/CodeCoverage" "-reporttypes:HtmlInline_AzurePipelines;Cobertura"
displayName: Create Code coverage report
- task: PublishCodeCoverageResults@1
displayName: 'Publish code coverage'
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: '$(Build.SourcesDirectory)/CodeCoverage/Cobertura.xml'
reportDirectory: '$(Build.SourcesDirectory)/CodeCoverage'
在这里一切都很好,测试运行,计算覆盖率,生成报告。
然后我安装 typescript(用于声纳分析)并运行代码分析:
- script: |
cd $(Build.SourcesDirectory)/Src/***ProjectDir***
npm install typescript
displayName: 'Install typescript for sonar analysis'
- task: SonarCloudAnalyze@1
displayName: 'Run code analysis'
continueOnError: false
- task: SonarCloudPublish@1
displayName: 'Publish code analysis'
continueOnError: false
inputs:
pollingTimeoutSec: '300'
即使在准备分析任务的 sonar.cs.opencover.reportsPaths 参数中提供了路径,声纳分析也会找到 trx 文件而不是 opencover 覆盖率报告。
这是日志:
D:\a\_tasks\SonarCloudPrepare_14d9cde6-c1da-4d55-aa01-2965cd301255\1.10.0\classic-sonar-scanner-msbuild\SonarScanner.MSBuild.exe end
SonarScanner for MSBuild 4.8
Using the .NET Framework version of the Scanner for MSBuild
Post-processing started.
17:31:43.118 Property 'sonar.cs.vstest.reportsPaths' provided, skipping the search for TRX files in default folders...
17:31:43.197 Did not find any binary coverage files in the expected location.
17:31:43.197 Falling back on locating coverage files in the agent temp directory.
17:31:43.197 Searching for coverage files in D:\a\_temp
17:31:43.243 No coverage files found in the agent temp directory.
分析已正确上传到 sonarcloud 但覆盖率显示为 0%...