Skip to content

Partially covered throw statement (async handler) #1103

Closed
@mjeanrichard

Description

@mjeanrichard

Hi,

In the following code the throw statement is marked as partially covered (because of the async statemachine), even though I see no way to cover that hidden branch. Could this be detected and marked as covered by coverlet?

public class AsyncThrowReproduction
{
	public async Task Execute(bool throwException)
	{
		try
		{
			if (throwException)
			{
				throw new InvalidOperationException();
			}
		}
		catch (InvalidOperationException)
		{
			await Task.Delay(1);
			throw;
		}
	}
}

Unit Tests:

public class AsyncThrowReproductionFixture
    {
        [Fact]
        public async Task Execute_ShouldWork_Exception()
        {
            // Arrange
            var sut = new AsyncThrowReproduction();

            // Act
            async Task TestFunc() => await sut.Execute(true);

            // Assert
            await Assert.ThrowsAsync<InvalidOperationException>(TestFunc);
        }

        [Fact]
        public async Task Execute_ShouldWork_NoException()
        {
            // Arrange
            var sut = new AsyncThrowReproduction();

            // Act
            await sut.Execute(false);
        }
    }

Reported Coverage:
image

Coverlet Version: 3.0.3

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingtenet-coverageIssue related to possible incorrect coveragewith reproIssue with repro

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions