我有一个 .NET Web 应用程序,希望代码覆盖率(使用 Coverlet 计算)显示在 Sonarcloud 中,由 GithubAction 触发。 这是相关片段:
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 }}"
问题:Sonarcloud 只是不会显示代码覆盖率,尽管:
coverage.opencover.xml
已在Github Action中创建(它显示Calculating coverage result... Generating report '/home/runner/work/sqs/sqs/coverage.opencover.xml'
)我尝试了所有官方教程和 Sonarcloud 故障排除指南,但似乎没有任何效果。 非常感谢您的帮助
我用的是:
dotnet-coverage collect "dotnet test --results-directory --logger trx" -f xml -o file.xml
它奏效了。但您还需要将此设置传递给 Sonar Cloud Preparation:
sonar.cs.vscoveragexml.reportsPaths=**/file.xml
我尝试使用床单,但它不起作用,所以我使用了不同的工具。