Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected branch coverage with using statement and several awaits #1176

Closed
strout opened this issue Jun 8, 2021 · 2 comments · Fixed by #1201
Closed

Unexpected branch coverage with using statement and several awaits #1176

strout opened this issue Jun 8, 2021 · 2 comments · Fixed by #1201
Labels
tenet-coverage Issue related to possible incorrect coverage untriaged To be investigated

Comments

@strout
Copy link

strout commented Jun 8, 2021

I've encountered this with stable coverlet.collector as well as nightly 3.0.4-preview.31.g4902c245c8. I'm using .NET SDK 5.0.300 and running dotnet test --collect:"XPlat Code Coverage".

This code has 3 out of 6 blocks covered despite having no branches apart from awaits. Removing or reordering any of the statements results in 100% coverage and cyclomatic complexity of 1 instead of 6. I would expect this code to also have cyclomatic complexity of 1 and to be fully covered.

using System.IO;
using System.Threading.Tasks;

namespace Lib
{
    public class Foo
    {
        public static async Task Bar()
        {
            await Task.CompletedTask;
            using var _ = new MemoryStream();
            await Task.CompletedTask;
            await Task.CompletedTask;
            await Task.CompletedTask;
        }
    }
}

The report shows the using statement has 1 of 4 branches covered.

image

<?xml version="1.0" encoding="utf-8"?>
<coverage line-rate="1" branch-rate="0.5" version="1.9" timestamp="1623164584" lines-covered="7" lines-valid="7" branches-covered="3" branches-valid="6">
  <sources>
    <source>C:\</source>
  </sources>
  <packages>
    <package name="Lib" line-rate="1" branch-rate="0.5" complexity="6">
      <classes>
        <class name="Lib.Foo/&lt;Bar&gt;d__0" filename="\tmp\Lib\Foo.cs" line-rate="1" branch-rate="0.5" complexity="6">
          <methods>
            <method name="MoveNext" signature="()" line-rate="1" branch-rate="0.5" complexity="6">
              <lines>
                <line number="9" hits="1" branch="True" condition-coverage="100% (2/2)">
                  <conditions>
                    <condition number="16" type="jump" coverage="100%" />
                  </conditions>
                </line>
                <line number="10" hits="1" branch="False" />
                <line number="11" hits="1" branch="True" condition-coverage="25% (1/4)">
                  <conditions>
                    <condition number="135" type="switch" coverage="25%" />
                  </conditions>
                </line>
                <line number="12" hits="1" branch="False" />
                <line number="13" hits="1" branch="False" />
                <line number="14" hits="1" branch="False" />
                <line number="15" hits="1" branch="False" />
              </lines>
            </method>
          </methods>
          <lines>
            <line number="9" hits="1" branch="True" condition-coverage="100% (2/2)">
              <conditions>
                <condition number="16" type="jump" coverage="100%" />
              </conditions>
            </line>
            <line number="10" hits="1" branch="False" />
            <line number="11" hits="1" branch="True" condition-coverage="25% (1/4)">
              <conditions>
                <condition number="135" type="switch" coverage="25%" />
              </conditions>
            </line>
            <line number="12" hits="1" branch="False" />
            <line number="13" hits="1" branch="False" />
            <line number="14" hits="1" branch="False" />
            <line number="15" hits="1" branch="False" />
          </lines>
        </class>
      </classes>
    </package>
  </packages>
</coverage>

I used this test class.

using System.Threading.Tasks;
using Xunit;

namespace Test
{
    public class FooTest
    {
        [Fact]
        public async Task Test()
        {
            await Lib.Foo.Bar();
        }
    }
}
@strout strout changed the title Unexpected branch coverage with using statement and several awaits. Unexpected branch coverage with using statement and several awaits Jun 8, 2021
@strout
Copy link
Author

strout commented Jun 8, 2021

I get the same with the equivalent try...finally:

image

@strout
Copy link
Author

strout commented Aug 17, 2021

I've confirmed the latest preview build works right with my real code. @daveMueller thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tenet-coverage Issue related to possible incorrect coverage untriaged To be investigated
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants