You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET项目中SonarCloud无法显示Coverlet生成的代码覆盖率

问题:SonarCloud无法显示Coverlet生成的代码覆盖率(.NET + GitHub Actions)

我有一个.NET Web应用程序,通过GitHub Action触发SonarCloud分析,但SonarCloud始终无法显示Coverlet计算的代码覆盖率。相关GitHub Action代码片段如下:

sonar-analysis:
    #name: Build and analyze
    runs-on: ubuntu-latest
    steps:
      - name: Set up JDK 11
        uses: actions/setup-java@v3
        with:
          java-version: 11
          distribution: 'zulu' # Alternative distribution options are available.
      - name: Setup .NET
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: 5.0.x
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0  # Shallow clones should be disabled for a better relevancy of analysis
      - name: Cache SonarCloud packages
        uses: actions/cache@v3
        with:
          path: ~/sonar/cache
          key: ${{ runner.os }}-sonar
          restore-keys: ${{ runner.os }}-sonar
      - name: Cache SonarCloud scanner
        id: cache-sonar-scanner
        uses: actions/cache@v3
        with:
          path: ./.sonar/scanner
          key: ${{ runner.os }}-sonar-scanner
          restore-keys: ${{ runner.os }}-sonar-scanner
      - name: Install SonarCloud scanner
        if: steps.cache-sonar-scanner.outputs.cache-hit != 'true'
        run: |
          mkdir -p ./.sonar/scanner
          dotnet tool update dotnet-sonarscanner --tool-path ./.sonar/scanner
      - name: Install Coverlet
        run: |
          dotnet tool install --global coverlet.console
      - name: Build and analyze
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}  # Needed to get PR information, if any
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        run: |
          ./.sonar/scanner/dotnet-sonarscanner begin /k:"<...>" /o:"<...>" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.opencover.reportsPaths=coverage.opencover.xml
          dotnet build --no-incremental
          coverlet <path to Tests.dll> --target "dotnet" --targetargs "test --no-build" -f=opencover
          ./.sonar/scanner/dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"

已知情况:

  • GitHub Action日志显示已成功生成coverage.opencover.xml文件
  • 控制台能看到Coverlet输出的覆盖率数据
  • 已尝试官方教程和SonarCloud故障排查指南,但无效

排查与修复步骤

1. 确认覆盖率报告路径匹配Sonar配置

Sonar的sonar.cs.opencover.reportsPaths参数需精准指向报告文件:

  • 检查coverlet命令生成的报告路径是否与Sonar配置一致,若报告在子目录生成,需调整路径(如./Tests/coverage.opencover.xml)
  • 可在coverlet命令后添加--output ./,强制将报告输出到项目根目录,确保路径匹配

2. 修正测试项目与目标项目的关联

Coverlet需明确关联被测试项目,否则报告无法被Sonar正确解析:

  • 避免模糊的Tests.dll路径,改用完整相对路径,同时在targetargs中指定测试项目文件:
    coverlet ./Tests/bin/Debug/net5.0/Tests.dll --target "dotnet" --targetargs "test ./Tests/Tests.csproj --no-build" -f=opencover
    

3. 检查Sonar项目扫描范围

  • 确认SonarCloud项目设置中,代码覆盖率模块已启用,且未排除目标Web项目的文件
  • 若配置了sonar.projectBaseDir,需确保其覆盖Web项目所在目录

4. 优化扫描流程参数

  • 确保dotnet build命令覆盖所有需扫描的项目,比如指定解决方案文件:dotnet build MySolution.sln --no-incremental
  • 在sonarscanner begin命令中添加/d:sonar.verbose=true启用详细日志,查看扫描阶段是否加载了覆盖率报告

5. 验证Coverlet报告有效性

  • 在GitHub Action中添加步骤打印报告内容,确认包含正确的文件路径和覆盖率数据:
    cat coverage.opencover.xml
    
  • 若报告中是绝对路径,可在coverlet命令中添加--use-source-link参数,让报告使用相对路径

6. 检查权限配置

  • 确认SONAR_TOKEN拥有项目分析权限,且GitHub Secrets配置正确
  • 确保GITHUB_TOKEN拥有contents: read权限(私有仓库需重点检查)

内容的提问来源于stack exchange,提问作者chDu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 08:23:23