Latest
Sonar 1 min read

Import .NET Test Coverage into SonarQube

Import .NET Test Coverage into SonarQube
Sonar · October 3, 2026

Version note: references describe SonarQube Server 2025.2 behaviour. Check your server, scanner and edition requirements. These examples were not run against a live SonarQube server.

What this guide covers

The SonarScanner for .NET wraps your build: begin analysis, build, run tests with coverage, then end analysis. Keeping that order lets the scanner connect coverage to the compiled source.

Before you start

Install a compatible .NET SDK, dotnet-sonarscanner and dotnet-coverage. Use a solution with passing tests. Set SONAR_HOST_URL and securely supply SONAR_TOKEN in the job environment.

Step 1: Run the complete sequence

Run from the solution directory in one job:

set -eu
: "${SONAR_TOKEN:?Inject the analysis credential}"
dotnet sonarscanner begin /k:"inventory-dotnet" \
  /d:sonar.token="$SONAR_TOKEN" \
  /d:sonar.host.url="$SONAR_HOST_URL" \
  /d:sonar.cs.vscoveragexml.reportsPaths="coverage.xml"
dotnet build --no-incremental
dotnet-coverage collect 'dotnet test --no-build' -f xml -o coverage.xml
test -s coverage.xml
dotnet sonarscanner end /d:sonar.token="$SONAR_TOKEN"

Replace the project key. Keep shell command tracing off because the token is passed as a scanner argument. Do not continue after a failed build or test.

Step 2: Use the setting for your report format

dotnet-coverage writes Visual Studio coverage XML in this example. The matching property is sonar.cs.vscoveragexml.reportsPaths. Other tools may write OpenCover or another format; choose that format’s documented property instead. Renaming a file to .xml does not convert it.

Step 3: Inspect the uploaded result

Look for test discovery and coverage-import messages in the job log. Open the completed SonarQube analysis and inspect a known C# file. If it has no coverage, first check whether tests ran, then whether the report paths match the compiled files.

Check the result

The report file should be created before sonarscanner end, and SonarQube should show coverage for the current build. Do not reuse an old report to make a failed test step appear successful.

References

PB
Written by

Parul Bedi

Notes and guides by Parul Bedi.

Up next

Use BuildKit Secrets for Private npm Dependencies

Docker October 7, 2026 1 min read
Open post

Conversation

0 comments

No comments yet. Start the conversation below.

Leave a comment

Replying to a comment.
Be kind. Markdown is not supported in comments.