I’m using SonarCloud and I have c# source code using Task and async methods.
There is 2 ways I can write the code e.g.:
// version without async
Task DoSomething() {
return Task.Delay(1000);
}
// version with async
async Task DoSomething(){
await Task.Delay(1000);
}
When using the version withasync/await, the code coverage highlight (red/green) is not shown as it is when using the version withoutasync/await. The coverage reports generated are a bit different but both contains all the information. Also, metadata (Coverage percentage, Uncovered lines count, …) is also the same in either case.
I don’t know exactly how to know that but I think yes.
I can say that in the report not usingasync, the information on coverage is part of an element referencing the class in which the method is defined. E.G.:
I saw that blog post you mentioned but I’m not sure it’s the same case.
Now I don’t really know if it’s a problem in the way Visual Studio generates the report or not (because I don’t know what it is supposed to look like).
One thing I know is that the information is in the reports. It’s just not formatted the same way depending on if you use async keyword or not. E.G. the number of Uncovered lines is right in both case. And I can find the number of hits for the lines within those async methods from the report whether I use the async keyword or not. But somehow, it’s not shown with the green/red border in Sonar cloud when I use the async keyword.
I’ll try to do a very simple project and post the full report here. Would that help you help me?
I would expect this. If the lines are found as executable (able to covered by tests) and covered… you end up with the same number of uncovered lines as if they aren’t found to be executable.
Well, in my async method, some lines are covered and some other lines are not covered. Yet, all the metadata (lines to cover, uncovered lines, …) is the same between the 2 coverage reports.
I did a small demo project and generated the coverage report. There are 2 methods in the class under test:
public class MyClass
{
public Task DoSomethingAsync1()
{
return Task.Delay(1);
}
public async Task DoSomethingAsync2()
{
await Task.Delay(1);
}
}
The only difference between the DoSomethingAsync2async method and the other method is that the DoSomethingAsync2async method is reported in a kind of MyClass<DoSomethingAsync2> generated class.
But the filename and line numbers all match so I presume it’s possible to extract the data from that file and show it correctly in the Sonar cloud UI.
can you please show the command and the version of the tool that generated the coverage report? To me, it looks like a limitation of the tool as it doesn’t properly handle the async state machine transposition of the compiler.
But as you can see in this screen shot these lines are neither red or green, they are not seen as covered, BUT the coverage percentage seems to be right (given some none coverage line that could not be seen)
So it clearly seems to be an issue on display in sonar and not an issue of the tool. The xml coverage files as the good format, the computation of line coverage seems good BUT on async C# method nothing is displayed
Could you please create a minimal reproducer project and share it with us (e.g. github repo)? Please mention in the README the commands that you are using.
This will help us jump on the topic quicker and kickstart the investigation.