Repository navigation
Description
Describe the bug
On functions that return IAsyncEnumerable, the tool reports uncovered branches that do not actually exist.
To Reproduce
public static async IAsyncEnumerable<T?> FunctionThatReturnsIAsyncEnumerable<T>([EnumeratorCancellation] CancellationToken cancellationToken)
{
T?[] items = [default, default];
foreach (var item in items)
yield return !cancellationToken.IsCancellationRequested ? item : throw new OperationCanceledException();
}
I am unable to fully cover the above function. I am running the following tests, and I still get missing branches:
[Fact]
public async Task FunctionThatReturnsIAsyncEnumerable_Test()
{
var values = await DatabaseClient.FunctionThatReturnsIAsyncEnumerable<int>(TestContext.Current.CancellationToken).ToListAsync(TestContext.Current.CancellationToken);
Assert.Equal(2, values.Count);
}
[Fact]
public async Task FunctionThatReturnsIAsyncEnumerable_ImmediateCancellation()
{
var cts = new CancellationTokenSource();
cts.Cancel();
await Assert.ThrowsAsync<OperationCanceledException>(async () => await DatabaseClient.FunctionThatReturnsIAsyncEnumerable<int>(cts.Token).FirstAsync(cts.Token));
}
[Fact]
public async Task FunctionThatReturnsIAsyncEnumerable_CancelAfterFirst()
{
var cts = new CancellationTokenSource();
await using var enumerator = DatabaseClient.FunctionThatReturnsIAsyncEnumerable<int>(cts.Token).GetAsyncEnumerator(cts.Token);
Assert.True(await enumerator.MoveNextAsync());
cts.Cancel();
await Assert.ThrowsAsync<OperationCanceledException>(async () => await enumerator.MoveNextAsync(cts.Token));
}
Expected behavior
I would expect the above tests to result in 100% branch coverage for the above function.
Actual behavior
The report includes an uncovered branch on the foreach statement, and another uncovered branch on the return:
<method name="FunctionThatReturnsIAsyncEnumerable" signature="()" line-rate="1" branch-rate="0.75" complexity="8">
<lines>
<line number="83" hits="3" branch="false" />
<line number="84" hits="3" branch="false" />
<line number="85" hits="15" branch="true" condition-coverage="75% (3/4)" />
<line number="86" hits="4" branch="true" condition-coverage="100% (2/2)" />
<line number="87" hits="2" branch="true" condition-coverage="50% (1/2)" />
</lines>
</method>
Configuration (please complete the following information):
I'm using .NET 9 in Visual Studio with Fine Code Coverage, coverlet.collector 8.0.0, "any cpu" on Win10.
I'm just starting using code coverage tools, so I might just be missing something. To the best of my current efforts, this looks like a bug.
Activity
Add unit test for issue coverlet-coverage#1836: IAsyncEnumerable with…
Update investigation notes for coverlet-coverage#1836 IAsyncEnumerabl…
Bertk commentedon Mar 29, 2026
Similar issue #1335
Update investigation notes for coverlet-coverage#1836 IAsyncEnumerabl…
Add async iterator branch coverage tests for coverlet-coverage#1836/c…
add and execute plan for branch coverage improvement
Investigate and fix branch coverage (#1865)
Bertk commentedon Apr 1, 2026
see https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/UnderstandingBranchCoverage.md
FilippoPolo commentedon Apr 1, 2026
Thanks. I've read all of that, and, as I understand, none of those cases apply to the situation I reported. The "Compile-Generated Branches" paragraph mentions that untestable branches can exist, especially in async blocks, that Coverlet should in theory exclude them, and to report cases in which this is not happening. After reading that page, I believe this is one such case.
# This is a combination of 2 commits.
Bump coverlet.collector from 8.0.1 to 10.0.0 (#82)
Merged PR 928: chore(deps): update coverlet.collector to 10.0.0
34 remaining items
chore(csharp): bump coverlet.collector from 6.0.4 to 10.0.1 (#52)
chore(csharp): Bump coverlet.collector from 8.0.1 to 10.0.1 (#4347)
Bump coverlet.msbuild from 8.0.0 to 10.0.1 (#330)
chore(deps): Bump the test-dependencies group with 1 update (#34)
Bump coverlet.collector from 8.0.1 to 10.0.1 (#52)
chore(deps): Bump coverlet.collector from 6.0.2 to 10.0.1 (#13)
Bump coverlet.collector and 3 others (#6)
Bump coverlet.collector from 6.0.4 to 10.0.1 (#19)
Bump coverlet.collector and 7 others (#86)
chore: Bump coverlet.collector from 8.0.1 to 10.0.1 (#283)
Bump coverlet.collector from 6.0.4 to 10.0.1 (#6)
[nuget][Find]- Bump Azure.Identity and 26 others (#705)
Bump the test-dependencies group with 3 updates (#2)
Bump coverlet.msbuild from 8.0.0 to 10.0.1 (#893)
chore(deps): Bump coverlet.collector from 6.0.4 to 10.0.1 (#438)