coverlet-coverage / coverlet

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

Reported condition-coverage is too high #1279

Open Chris-D-Turk opened 2 years ago

Chris-D-Turk commented 2 years ago

Coverlet reports 50% condition-coverage for an if-condition that is only 25% covered.

Environment

Steps to reproduce

Class under Test

public static class Unit
    {
        public static bool DoSomething(int i1, int i2)
        {
            if (i1 > 0 || i2 > 0)
                return true;
            else 
                return false;
        }
    }

Test class

public class Tests
    {
        [Test]
        public void Test_DoSomething()
        {
            Assert.True(Unit.DoSomething(1, 0));
        }
    }

Generate a coverage report using dotnet test --collect:"XPlat code coverage"

(see attachment for complete code) CodeCoveragePlayground.zip

Expected result

coverage.cobertura.xml <line number="10" hits="1" branch="True" condition-coverage="25% (1/4)"> (Only the i1 > 0 condition is covered)

Actual result

coverage.cobertura.xml <line number="10" hits="1" branch="True" condition-coverage="50% (2/4)">

Chris-D-Turk commented 2 years ago

@MarcoRossignoli Could you have a look at this? Is my expectation wrong? I'm really having trouble to understand how coverlet computes condition-coverage. Seems to be way different than jacoco for example.

daveMueller commented 2 years ago

Sorry I can't reproduce it. I tested on 3.1.0 and 3.1.2. Is there anything in addition we have to think of e.g. Release configuration?

grafik

Chris-D-Turk commented 2 years ago

But according to your screenshot you did reproduce it. The condition coverage is 50% (2/4). I would expect 25% (1/4) DoSomething(1, 0) <- covered DoSomething(0, 1) <- not covered DoSomething(0, 0) <- not covered DoSomething(1, 1) <- not covered

When I rewrite this testcase in Java and collect coverage via JaCoCo, I get the expected result. condition-coverage seems to have a different meaning to it. Is this intentional or is it a bug?

daveMueller commented 2 years ago

Sorry for the late response and sorry for not reading your issue more carefully. Indeed coverlet produces here 50% coverage. We calculate the coverage according to the branch points the compiler generates. We can see if they were hit by trackers we inject into the assemblies. In the example there are 2 of the branch points hit and thats why it's 50%.

@MarcoRossignoli Do you have a bit more info here? It's really hard for me to understand the branch points the compiler is generating.

daveMueller commented 2 years ago

@Chris-D-Turk Sorry I'm not much of a help here. I debugged this yesterday and tried to understand our coverage calculation for this case but I didn't fully get it. Could also be that there still is bug.

Chris-D-Turk commented 2 years ago

@daveMueller no problem, I hope you guys find the cause of this issue - confuses me everytime I inspect the coverage of our codebase ;) (sry for the late response - missed the notification)

github-actions[bot] commented 12 months ago

This issue is stale because it has been open for 3 months with no activity.

stg609 commented 10 months ago

How is it going? We have a similar question about the branch coverage. The reported branch coverage is 100% while only half is covered actually.

github-actions[bot] commented 7 months ago

This issue is stale because it has been open for 3 months with no activity.

Bertk commented 1 month ago

Please replicate with latest coverlet V6.02 version.