Azure DevOps发布代码覆盖率结果时触发NullReferenceException求助
解决Azure DevOps代码覆盖率发布时的NullReferenceException问题
可能的原因及修复步骤
1. 修正路径匹配语法错误
你的PublishCodeCoverageResults@2任务中,summaryFileLocation的路径匹配缺少**后的斜杠,虽然日志显示找到了文件,但可能导致任务内部处理异常。修正为:
summaryFileLocation: '**/CalculatorApp.nUnit.Test/TestResults/Coverage/*coverage.cobertura.xml'
2. 添加显式的构建与还原步骤
在测试任务前添加dotnet restore和dotnet build步骤,确保项目依赖和构建状态正确:
- task: DotNetCoreCLI@2 inputs: command: 'restore' projects: '**/*.sln' feedsToUse: 'select' - task: DotNetCoreCLI@2 inputs: command: 'build' projects: '**/*.sln' arguments: '--configuration $(buildConfiguration)'
3. 使用ReportGenerator生成并发布覆盖率报告
改用ReportGenerator工具处理Coverlet生成的cobertura文件,避免旧版PublishCodeCoverage任务的兼容性问题:
- 在测试任务后添加ReportGenerator安装与执行步骤:
- task: DotNetCoreCLI@2 inputs: command: 'custom' custom: 'tool' arguments: 'install --global dotnet-reportgenerator-globaltool' - task: CmdLine@2 inputs: script: | reportgenerator -reports:"**/TestResults/Coverage/coverage.cobertura.xml" -targetdir:"$(Build.SourcesDirectory)/coveragereport" -reporttypes:"Html;Cobertura"
- 修改PublishCodeCoverageResults任务指向生成的新报告:
- task: PublishCodeCoverageResults@2 inputs: summaryFileLocation: '$(Build.SourcesDirectory)/coveragereport/Cobertura.xml' reportDirectory: '$(Build.SourcesDirectory)/coveragereport'
4. 指定.NET SDK版本
确保Azure DevOps代理使用的.NET SDK版本与项目兼容,添加SDK安装步骤:
- task: UseDotNet@2 inputs: packageType: 'sdk' version: '6.x' # 根据你的项目实际.NET版本替换
5. 检查测试项目的Coverlet包
确认测试项目已安装Coverlet.Microsoft.TestPlatform NuGet包,建议使用较新的稳定版。
内容的提问来源于stack exchange,提问作者R.Pussepitiya
相关产品推荐
相关产品推荐

