Skip to content

[BUG] wrong branch rate on IAsyncEnumerable #1836

Closed

Description

@FilippoPolo

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

  1. added
    untriaged
    on Mar 9, 2026
  2. added a commit that references this issue on Mar 19, 2026
  3. added a commit that references this issue on Mar 27, 2026
  4. added
    duplicate
    on Mar 29, 2026
  5. Bertk commented on Mar 29, 2026

    @Bertk
    Collaborator

    When testing an AsyncEnumerable extension that transforms an AsyncEnumerable in an nested form of well sized batches, I get a high percentage of uncovered branches. There aren't any obvious branches to cover and I can't figure out how to fix it. It may be a bug.

    Similar issue #1335

  6. FilippoPolo commented on Apr 1, 2026

    @FilippoPolo
    Author

    see https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/UnderstandingBranchCoverage.md

    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.

  7. 34 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateuntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions