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?
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>"
/d:sonar.login="<sonar-token>"
/d:sonar.cs.opencover.reportsPaths=/*/.xml
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:
- 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. - do the build
- run the coverage to output in opencover format.
- run the end step.
Using the wildcards worked, thanks!