coverlet icon indicating copy to clipboard operation
coverlet copied to clipboard

Incorrect coverage for methods returning `IAsyncEnumerable` in generic classes

Open arseni-mourzenko opened this issue 3 years ago • 1 comments
trafficstars

Hi.

Using coverlet.msbuild.3.1.2, I noticed an issue with code coverage for a method which returns IAsyncEnumerable, as soon as the method is inside a generic class.

Here's a short example which reproduces the issue:

public class A
{
    public static async IAsyncEnumerable<int> Demo()
    {
        yield return 5;
        yield return 2;
    }
}

public class A<T>
{
    public static async IAsyncEnumerable<int> Demo()
    {
        yield return 5;
        yield return 2;
    }
}

Given the following tests:

[Fact] public async Task Test1() => Assert.Equal(new [] { 5, 2 }, await A.Demo().ToListAsync());
[Fact] public async Task Test2() => Assert.Equal(new [] { 5, 2 }, await A<string>.Demo().ToListAsync());

the coverage report will show that the Demo method inside the class A has 100% branch coverage, whereas the method Demo inside A<T> has only partial coverage (one branch out of two) on the line {, as well as on the following two yield return lines.

The code of the two methods and the corresponding tests being identical, the coverage should instead be identical, that is, 100% in both cases.

arseni-mourzenko avatar Sep 17 '22 20:09 arseni-mourzenko

Thanks for reporting this.

daveMueller avatar Sep 17 '22 22:09 daveMueller

Seeing similar behavior where a Generic Class has a method that returns an IAsyncEnumerable, the method is marked with ExcludeFromCodeCoverage however coverage is reported as zero instead of being ignored.

RobARichardson avatar Oct 13 '22 19:10 RobARichardson

@RobARichardson I created another issue for that (#1431). I think it is another bug and my PR will not fix this.

daveMueller avatar Jan 08 '23 01:01 daveMueller

@MarcoRossignoli, do you know when the new version of Coverlet would be available through NuGet?

arseni-mourzenko avatar May 06 '23 07:05 arseni-mourzenko