Closed
Description
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.
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>