question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Coverage Incorrect for Async Method using Linq

See original GitHub issue

Latest of everything:

- netcoreapp3.1
- coverlet.collector 1.2.0 / coverlet.msbuild 2.8.0 (both have the same issue)
- Microsoft.NET.Test.Sdk 16.5.0
- NUnit 3.12.0
- NUnit3TestAdapter 13.16.1

If an async method uses Linq, coverage for all lines except the linq line(s) will be missing. Thus all other lines in the method are implied as uncoverable.

The following solution reproduces the issue and has the output file which shows the issue.

CoverletExample.zip

To reproduce, perform the following command:

dotnet test ExampleTest.csproj --configuration Release --collect:"XPlat Code Coverage"

The class in question:

public class ExampleClass
    {
        public async Task DoSomethingAsync(IEnumerable<object> objects)
        {
            await Task.Delay(TimeSpan.FromMilliseconds(1));
            foreach (var o in objects)
            {
                Console.WriteLine(o);
            }
        }
        
        public async Task DoSomethingAsyncWithLinq(IEnumerable<object> objects)
        {
            await Task.Delay(TimeSpan.FromMilliseconds(1));
            var selected = objects.Select(o => o);
            foreach (var o in selected)
            {
                Console.WriteLine(o);
            }
        }

        public void DoSomethingSync(IEnumerable<object> objects)
        {
            foreach (var o in objects)
            {
                Console.WriteLine(o);
            }
        }
        
        public void DoSomethingSyncWithLinq(IEnumerable<object> objects)
        {
            var selected = objects.Select(o => o);
            foreach (var o in selected)
            {
                Console.WriteLine(o);
            }
        }

Each method has a basic test passing it 1000 objects. Note that in the output all lines except for those in the method “DoSomethingAsyncWithLinq” are correctly reported. For the method “DoSomethingAsyncWithLinq”, only the Linq line var selected = objects.Select(o => o); is reported:

<class name="Example.ExampleClass/&lt;&gt;c" filename="ExampleClass.cs" line-rate="1" branch-rate="1" complexity="2">
          <methods>
            <method name="&lt;DoSomethingAsyncWithLinq&gt;b__1_0" signature="(System.Object)" line-rate="1" branch-rate="1">
              <lines>
                <line number="22" hits="1001" branch="True" condition-coverage="100% (2/2)">
                  <conditions>
                    <condition number="126" type="jump" coverage="100%" />
                  </conditions>
                </line>
              </lines>
            </method>
          </methods>
          <lines>
            <line number="22" hits="1001" branch="True" condition-coverage="100% (2/2)">
              <conditions>
                <condition number="126" type="jump" coverage="100%" />
              </conditions>
            </line>
          </lines>
        </class>

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:16

github_iconTop GitHub Comments

1reaction
zmhhcommented, Mar 16, 2020

Edit: Issue is resolved using nightly version 1.2.38-g34d6dc5ff6. Thanks Marco!

1reaction
MarcoRossignolicommented, Mar 10, 2020

Guys we fixed the bug…can someone give it a try using nightly build tomorrow? https://github.com/tonerdo/coverlet/blob/master/Documentation/ConsumeNightlyBuild.md

Thank’s a lot to all for the help!Really appreciated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Coverage Incorrect for Async Method using Linq · Issue #730
If an async method uses Linq, coverage for all lines except the linq line(s) will be missing. Thus all other lines in the...
Read more >
Where clause in LINQ calling an async method
You can only use asynchronous Where calls on IAsyncEnmerable and then only if you add the System.Linq.Async package. – Panagiotis Kanavos. May 9 ......
Read more >
C# async methods are not highlighted (red/green) in code ...
Hello, I'm using SonarCloud and I have c# source code using Task and async ... When using the version with async/await, the code...
Read more >
Code Coverage doesn't show async methods
The Code Coverage functionality in Visual Studio 2017 ignores async methods. To reproduce this problem create a project with a class with a ......
Read more >
Mock Asynchronous Repository with EF Core
Have you ever faced problems mocking asynchronous methods that EF core ... Linq.Expressions; using System.Threading; using System.Threading.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found