Coverage Incorrect for Async Method using Linq
See original GitHub issueLatest 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.
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/<>c" filename="ExampleClass.cs" line-rate="1" branch-rate="1" complexity="2">
<methods>
<method name="<DoSomethingAsyncWithLinq>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:
- Created 4 years ago
- Comments:16
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Edit: Issue is resolved using nightly version 1.2.38-g34d6dc5ff6. Thanks Marco!
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.