# C# async methods are not highlighted (red/green) in code coverage

**URL:** <https://community.sonarsource.com/t/c-async-methods-are-not-highlighted-red-green-in-code-coverage/78970>\
**Category:** SonarQube Cloud\
**Tags:** coverage\
**Created:** [December 15, 2022, 12:52pm UTC](https://community.sonarsource.com/t/c-async-methods-are-not-highlighted-red-green-in-code-coverage/78970 "2022-12-15T12:52:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![lepagema](https://avatars.discourse-cdn.com/v4/letter/l/d9b06d/32.png) [@lepagema](https://community.sonarsource.com/u/lepagema)\
**Post date:** [December 15, 2022, 12:52pm UTC](https://community.sonarsource.com/t/c-async-methods-are-not-highlighted-red-green-in-code-coverage/78970/1 "2022-12-15T12:52:00Z")

</div>

Hello,

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.:

```csharp
// version without async
Task DoSomething() {
    return Task.Delay(1000);
}
// version with async
async Task DoSomething(){
    await Task.Delay(1000);
}

```

When using the version **with** _async/await_, the code coverage highlight (red/green) is not shown as it is when using the version **without** _async/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.

Is there something I missed?

---

<div class="post-metadata">

**Author:** ![Colin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/colin/32/55255_2.png) [@Colin](https://community.sonarsource.com/u/Colin)\
**Post date:** [December 16, 2022, 1:50pm UTC](https://community.sonarsource.com/t/c-async-methods-are-not-highlighted-red-green-in-code-coverage/78970/2 "2022-12-16T13:50:01Z")

</div>

> [@lepagema](#):
>
> he coverage reports generated are a bit different but both contains all the information.

Do both coverage reports show that these lines can be covered by code? SonarQube isn’t involved in producing the coverage reports – only reading them.

---

<div class="post-metadata">

**Author:** ![lepagema](https://avatars.discourse-cdn.com/v4/letter/l/d9b06d/32.png) [@lepagema](https://community.sonarsource.com/u/lepagema)\
**Post date:** [December 16, 2022, 2:24pm UTC](https://community.sonarsource.com/t/c-async-methods-are-not-highlighted-red-green-in-code-coverage/78970/3 "2022-12-16T14:24:04Z")

</div>

> [@Colin](#):
>
> Do both coverage reports show that these lines can be covered by code?

I don’t know _exactly_ how to know that but I _think_ yes.

I can say that in the report **not using** _async_, the information on coverage is part of an element referencing the **class** in which the **method** is defined. E.G.:

```xml
<class name="My.Namespace.MyClass" filename="My\Namespace\MyClass.cs" ... />
    <method name="DoSomething" ... />

```

In the report **using** _async_, the information on coverage is part of an element referencing a **class that seems to be generated**. E.G.

```xml
<class name="My.Namespace.MyClass/&lt;DoSomething&gt;d__27" filename="My\Namespace\MyClass.cs" ... />

```

---

<div class="post-metadata">

**Author:** ![Colin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/colin/32/55255_2.png) [@Colin](https://community.sonarsource.com/u/Colin)\
**Post date:** [December 16, 2022, 4:27pm UTC](https://community.sonarsource.com/t/c-async-methods-are-not-highlighted-red-green-in-code-coverage/78970/4 "2022-12-16T16:27:38Z")

</div>

This might also be relevant.

> [@Lines to Cover Does not Include Async Methods?](https://community.sonarsource.com/t/lines-to-cover-does-not-include-async-methods/47290/3):
>
> Got it based on your feedback. I hadn’t looked to see if it could be a Visual Studio problem which is apparently is. Thank you! For anyone else looking: [Visual Studio Feedback](https://developercommunity.visualstudio.com/t/code-coverage-not-working-when-using-async-test-me/717167)

---

<div class="post-metadata">

**Author:** ![lepagema](https://avatars.discourse-cdn.com/v4/letter/l/d9b06d/32.png) [@lepagema](https://community.sonarsource.com/u/lepagema)\
**Post date:** [December 20, 2022, 2:11pm UTC](https://community.sonarsource.com/t/c-async-methods-are-not-highlighted-red-green-in-code-coverage/78970/5 "2022-12-20T14:11:28Z")

</div>

Thanks for your reply

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? 🙂

---

<div class="post-metadata">

**Author:** ![Colin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/colin/32/55255_2.png) [@Colin](https://community.sonarsource.com/u/Colin)\
**Post date:** [December 21, 2022, 2:14pm UTC](https://community.sonarsource.com/t/c-async-methods-are-not-highlighted-red-green-in-code-coverage/78970/6 "2022-12-21T14:14:17Z")

</div>

A reproducer is always helpful. 🙂

> [@lepagema](#):
>
> E.G. the number of **Uncovered lines** is right in both case.

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.

---

<div class="post-metadata">

**Author:** ![lepagema](https://avatars.discourse-cdn.com/v4/letter/l/d9b06d/32.png) [@lepagema](https://community.sonarsource.com/u/lepagema)\
**Post date:** [December 22, 2022, 3:45pm UTC](https://community.sonarsource.com/t/c-async-methods-are-not-highlighted-red-green-in-code-coverage/78970/7 "2022-12-22T15:45:56Z")

</div>

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:

```csharp
public class MyClass
{
    public Task DoSomethingAsync1()
    {
        return Task.Delay(1);
    }
        
    public async Task DoSomethingAsync2()
    {
        await Task.Delay(1);
    }
}

```

And to me, the report contains all the results:

```xml
<?xml version="1.0" encoding="utf-8"?>
<coverage line-rate="1" branch-rate="1" version="1.9" timestamp="1671723050" lines-covered="6" lines-valid="6" branches-covered="0" branches-valid="0">
  <sources>
    <source>D:\</source>
  </sources>
  <packages>
    <package name="MyAsyncLib" line-rate="1" branch-rate="1" complexity="2">
      <classes>
        <class name="MyAsyncLib.MyClass" filename="repos\scratch\MyAsyncLib\MyAsyncLib\MyClass.cs" line-rate="1" branch-rate="1" complexity="1">
          <methods>
            <method name="DoSomethingAsync1" signature="()" line-rate="1" branch-rate="1" complexity="1">
              <lines>
                <line number="6" hits="1" branch="False" />
                <line number="7" hits="1" branch="False" />
                <line number="8" hits="1" branch="False" />
              </lines>
            </method>
          </methods>
          <lines>
            <line number="6" hits="1" branch="False" />
            <line number="7" hits="1" branch="False" />
            <line number="8" hits="1" branch="False" />
          </lines>
        </class>
        <class name="MyAsyncLib.MyClass/&lt;DoSomethingAsync2&gt;d__1" filename="repos\scratch\MyAsyncLib\MyAsyncLib\MyClass.cs" line-rate="1" branch-rate="1" complexity="1">
          <methods>
            <method name="MoveNext" signature="()" line-rate="1" branch-rate="1" complexity="1">
              <lines>
                <line number="11" hits="1" branch="False" />
                <line number="12" hits="1" branch="False" />
                <line number="13" hits="1" branch="False" />
              </lines>
            </method>
          </methods>
          <lines>
            <line number="11" hits="1" branch="False" />
            <line number="12" hits="1" branch="False" />
            <line number="13" hits="1" branch="False" />
          </lines>
        </class>
      </classes>
    </package>
  </packages>
</coverage>

```

The only difference between the `DoSomethingAsync2` _async_ method and the other method is that the `DoSomethingAsync2` _async_ 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.

---

<div class="post-metadata">

**Author:** ![Colin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/colin/32/55255_2.png) [@Colin](https://community.sonarsource.com/u/Colin)\
**Post date:** [December 29, 2022, 4:26pm UTC](https://community.sonarsource.com/t/c-async-methods-are-not-highlighted-red-green-in-code-coverage/78970/9 "2022-12-29T16:26:21Z")

</div>

Thanks for the details. I’ve flagged this for some expert attention.

---

<div class="post-metadata">

**Author:** ![Martin\_Strecker](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/martin_strecker/32/32318_2.png) [@Martin\_Strecker](https://community.sonarsource.com/u/Martin_Strecker)\
**Post date:** [February 23, 2023, 12:17pm UTC](https://community.sonarsource.com/t/c-async-methods-are-not-highlighted-red-green-in-code-coverage/78970/10 "2023-02-23T12:17:38Z")

</div>

Hi [lepagema](https://community.sonarsource.com/u/lepagema)

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.

Best Martin

---

<div class="post-metadata">

**Author:** ![Laurent](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Laurent](https://community.sonarsource.com/u/Laurent)\
**Post date:** [March 16, 2023, 1:06pm UTC](https://community.sonarsource.com/t/c-async-methods-are-not-highlighted-red-green-in-code-coverage/78970/12 "2023-03-16T13:06:06Z")

</div>

Hi @Martin_Strecker  
We send a [Generic test data (sonarqube.org)](https://docs.sonarqube.org/latest/analyzing-source-code/test-coverage/generic-test-data/) xml files, so clearly not an issue on the tool…

And we got the problem, for instance the file contains:

```auto
  <file path="MvtMain\Services\Components\Loader\ComponentLoader.cs">
    <lineToCover lineNumber="309" covered="true"/>
    <lineToCover lineNumber="310" covered="true"/>
    <lineToCover lineNumber="312" covered="true"/>
    <lineToCover lineNumber="313" covered="true"/>
    <lineToCover lineNumber="316" covered="true"/>
    <lineToCover lineNumber="318" covered="true"/>
    <lineToCover lineNumber="319" covered="true"/>
    <lineToCover lineNumber="309" covered="true"/>
    <lineToCover lineNumber="310" covered="true"/>
    <lineToCover lineNumber="312" covered="true"/>
    <lineToCover lineNumber="313" covered="true"/>
    <lineToCover lineNumber="316" covered="true"/>
    <lineToCover lineNumber="318" covered="true"/>
    <lineToCover lineNumber="319" covered="true"/>
  </file>

```

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)

 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/3/d/3d48d47ae95616e6899cd3904daf3f5ceacdb7d4.png)

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 ☹

Regards

---

<div class="post-metadata">

**Author:** ![Andrei\_Epure](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/andrei_epure/32/77402_2.png) [@Andrei\_Epure](https://community.sonarsource.com/u/Andrei_Epure)\
**Post date:** [March 21, 2023, 4:58pm UTC](https://community.sonarsource.com/t/c-async-methods-are-not-highlighted-red-green-in-code-coverage/78970/13 "2023-03-21T16:58:31Z")

</div>

Hello @Laurent

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.

---

<div class="post-metadata">

**Author:** ![Laurent](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Laurent](https://community.sonarsource.com/u/Laurent)\
**Post date:** [April 3, 2023, 11:55am UTC](https://community.sonarsource.com/t/c-async-methods-are-not-highlighted-red-green-in-code-coverage/78970/14 "2023-04-03T11:55:13Z")

</div>

If you just try a single c#  
MyClass.cs

```auto
using System.Threading.Tasks;

public class MyClass
{
    public Task DoSomethingAsync1()
    {
        return Task.Delay(1);
    }
        
    public async Task DoSomethingAsync2()
    {
        await Task.Delay(1);
    }
}

```

coverage.xml

```xml
<coverage version="1">
  <file path="MyClass.cs">
    <lineToCover lineNumber="6" covered="true"/>
    <lineToCover lineNumber="7" covered="true"/>
    <lineToCover lineNumber="8" covered="true"/>
    <lineToCover lineNumber="11" covered="true"/>
    <lineToCover lineNumber="12" covered="true"/>
    <lineToCover lineNumber="13" covered="true"/>
  </file>
</coverage>

```

And import this coverage files to sonar, you will get exactly this issue (it is in fact exactly what we did)

Do not know an easy way to reproduce it in github and Sonar, but I think it should be enough to reproduce.

Regard

---

<div class="post-metadata">

**Author:** ![Martin\_Strecker](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/martin_strecker/32/32318_2.png) [@Martin\_Strecker](https://community.sonarsource.com/u/Martin_Strecker)\
**Post date:** [April 3, 2023, 3:05pm UTC](https://community.sonarsource.com/t/c-async-methods-are-not-highlighted-red-green-in-code-coverage/78970/15 "2023-04-03T15:05:14Z")

</div>

Hi @Laurent

can you please also give us the command line calls that produce the `coverage.xml` file? There are a lot of tools that can generate coverage reports and we need to know which tool and options you used.

---

<div class="post-metadata">

**Author:** ![Laurent](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Laurent](https://community.sonarsource.com/u/Laurent)\
**Post date:** [April 3, 2023, 3:32pm UTC](https://community.sonarsource.com/t/c-async-methods-are-not-highlighted-red-green-in-code-coverage/78970/16 "2023-04-03T15:32:18Z")

</div>

We send a [Generic test data (sonarqube.org)](https://docs.sonarqube.org/latest/analyzing-source-code/test-coverage/generic-test-data/) xml file. So the tool does not matter (in fact it is a mixed of different tools…)

BUT as the files is at your own format, it is clearly not a question of tools (you defined the format)

---

<div class="post-metadata">

**Author:** ![Martin\_Strecker](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/martin_strecker/32/32318_2.png) [@Martin\_Strecker](https://community.sonarsource.com/u/Martin_Strecker)\
**Post date:** [April 25, 2023, 9:28am UTC](https://community.sonarsource.com/t/c-async-methods-are-not-highlighted-red-green-in-code-coverage/78970/29 "2023-04-25T09:28:00Z")

</div>

Hello [lepagema](https://community.sonarsource.com/u/lepagema),

we made an investigation with the SonarQube and SonarCloud teams. Neither of the teams was able to reproduce the issue with the data given. The coverage was shown correctly. It seems that we miss some important information from your side. We created a project with the sample cs file you gave and uploaded a coverage file you gave. The result was correctly highlighting the code as defined in the coverage file.  
Please provide us with more information so we can reproduce the issue. Can you give a small sample project and a corresponding coverage file?

Best regards, Martin

---

<div class="post-metadata">

**Author:** ![Laurent](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Laurent](https://community.sonarsource.com/u/Laurent)\
**Post date:** [May 4, 2023, 12:07pm UTC](https://community.sonarsource.com/t/c-async-methods-are-not-highlighted-red-green-in-code-coverage/78970/31 "2023-05-04T12:07:42Z")

</div>

I create a very simple project:  
[MyClass.cs - async - testcsharpasync (sonarcloud.io)](https://sonarcloud.io/code?id=async&selected=async%3AMyClass.cs)

As you can see, the second function has NO MARK but is seen as coverage in the computation of the coverage if you imported this coverage sonar xml file (see [coverage.xml - async - testcsharpasync (sonarcloud.io)](https://sonarcloud.io/code?id=async&selected=async%3Acoverage.xml))

So seems to comes from line number management, it does not like decreasing number ☹

The computation of coverage is correct (only 3 lines not coverage) BUT the display is wrong and we could not know which lines are not covered ☹

---

<div class="post-metadata">

**Author:** ![anita.stanisz](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/anita.stanisz/32/39153_2.png) [@anita.stanisz](https://community.sonarsource.com/u/anita.stanisz)\
**Post date:** [May 4, 2023, 12:50pm UTC](https://community.sonarsource.com/t/c-async-methods-are-not-highlighted-red-green-in-code-coverage/78970/34 "2023-05-04T12:50:49Z")

</div>

Hi @Laurent

Thank you for this example, I confirm we could reproduce the issue and we’ll investigate it.

Anita

---

<div class="post-metadata">

**Author:** ![anita.stanisz](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/anita.stanisz/32/39153_2.png) [@anita.stanisz](https://community.sonarsource.com/u/anita.stanisz)\
**Post date:** [May 10, 2023, 1:54pm UTC](https://community.sonarsource.com/t/c-async-methods-are-not-highlighted-red-green-in-code-coverage/78970/35 "2023-05-10T13:54:29Z")

</div>

Hi @Laurent

The fix has been deployed today, could you confirm that it indeed solved the issue on your side?

Anita

---

<div class="post-metadata">

**Author:** ![lepagema](https://avatars.discourse-cdn.com/v4/letter/l/d9b06d/32.png) [@lepagema](https://community.sonarsource.com/u/lepagema)\
**Post date:** [May 16, 2023, 2:12pm UTC](https://community.sonarsource.com/t/c-async-methods-are-not-highlighted-red-green-in-code-coverage/78970/36 "2023-05-16T14:12:26Z")

</div>

Hello,

It works fine! Thank you!

Marc-André

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/31083_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [May 23, 2023, 2:13pm UTC](https://community.sonarsource.com/t/c-async-methods-are-not-highlighted-red-green-in-code-coverage/78970/37 "2023-05-23T14:13:05Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
