coverlet-coverage / coverlet

Cross platform code coverage for .NET
MIT License
2.99k stars 386 forks source link

[BUG] Throw exception via method causes missing coverage #1652

Closed manuel-rw closed 6 months ago

manuel-rw commented 7 months ago

Describe the bug After throwing an exception via a method the method ending } is not being marked as covered.

To Reproduce

    public static void EnsureNull(int? value)
    {
        if (!value.HasValue)
        {
            return;
        }

        CustomException.Throw(value.Value);
    }

https://github.com/meggima/coverlet-reproductions/pull/5

Expected behavior All four lines are fully covered.

Actual behavior Last line is marked as not covered:

325590667-31c186ce-a057-4543-9252-67266e61d699

Configuration (please complete the following information): Please provide more information on your .NET configuration:

Additional context Add any other context about the problem here.

:exclamation: Please also read Known Issues

daveMueller commented 6 months ago

Sorry for the late response. Even tho' you marked your Throw method with the DoesNotReturnAttribute, you still need to tell coverlet that statements after this method call should be excluded from coverage (methods-that-do-not-return).

Thus you just need to add this parameter to your test execution:

/p:DoesNotReturnAttribute="DoesNotReturnAttribute"

The complete call in your repro then will look like this:

dotnet test .\CoverletReproductions.sln --no-build --no-restore --configuration release --logger:trx -v minimal /p:DoesNotReturnAttribute="DoesNotReturnAttribute" /p:CollectCoverage=true /p:CoverletOutputFormat=opencover

which results in a 100% coverage.

grafik

daveMueller commented 6 months ago

I will close this as it is as-design and a duplicate of this #1265. @manuel-rw feel free to reopen if it doesn't help.