How to upload coverlet.collector results to SonarCloud?

Using the coverlet.collector package is the preferred way of collecting code coverage information with Coverlet. But the docs of SonarCloud don’t mention anything about it. Is there a way to integrate coverlet.collector with SonarCloud?

Hey there.

Did you see the documentation on coverlet in the .NET Test Coverage documentation?

I saw it but it doesn’t mention anything about running the tests with dotnet test --collect:"XPlat Code Coverage". This is what Coverlet recommends to use.

At the end of the day, SonarCloud just needs a report in opencover format which can be passed to sonar.cs.opencover.reportsPaths.

And, maybe we should be documenting a dotnet test example. I’ve pinged the right team on this topic to take a look.

@teo-tsirpanis did you try to use the tool with

dotnet test --collect:"XPlat Code Coverage;Format=opencover"

in your integration?

You’re right, we should update our docs. I’ll open an internal thread.

I tried but still sonarscanner does not pick the coverage file automatically.

I then tried finding the coverage file myself and passing it to sonarscanner but it says that it must be passed in the begin phase, and the file is in a random directory which I don’t know how to customize.

It doesn’t know to find files, you need to pass them to the scanner during the begin step.

In the docs Colin shared:

you can find how to use OpenCover reports. You can use wildcards inside, for example:

dotnet sonarscanner begin /k:"<sonar-project-key>"

You can also provide a comma-delimited list of paths to the reportPaths property.

After the begin step, you do the build and then run dotnet test.

The coverage files get picked up during the end step. So, between dotnet test and the dotnet sonarscanner end step, you can move the coverage files in case it’s easier.
I am running a few minutes late; my previous meeting is running over.

To sum up:

  1. pass the paths of the future coverage files in the begin step /d:sonar.cs.opencover.reportsPaths. If you don’t know the name of the output files, use wildcards.
  2. do the build
  3. run the coverage to output in opencover format.
  4. run the end step.

Using the wildcards worked, thanks!

1 Like