C# branch code coverage in SonarCloud problem with iterator

Hi, we have a problem (seems like a SonarCloud bug but not sure) related to this new feature.
We have a method “AddRangeToCollection” with 1 uncovered line of code. Sonar tells us that this line is partially covered by tests 4 of 24 conditions. We cannot understand what’s wrong with our unit tests, we think that our tests do cover ALL cases. What is the root cause?

Tests:

               [Test]
            public void AddRangeToCollectionBasedOnListTest()
            {
                //Arrange
                ICollection<int> list = new List<int> { 1, 3, 4 };
                //Act
                list.AddRangeToCollection(new[] { 5, 7, 9 });
                //Assert
                Assert.AreEqual(6, list.Count);
            }
            [Test]
            public void AddEmptyRangeToCollectionBasedOnListTest()
            {
                //Arrange
                ICollection<int> list = new List<int> { 1, 3, 4 };
                //Act
                list.AddRangeToCollection(Enumerable.Empty<int>());
                //Assert
                Assert.AreEqual(3, list.Count);
            }
            [Test]
            public void AddRangeToEmptyCollectionBasedOnListTest()
            {
                //Arrange
                ICollection<int> list = new List<int>();
                //Act
                list.AddRangeToCollection(new[] { 5, 7, 9 });
                //Assert
                Assert.AreEqual(3, list.Count);
            }
            [Test]
            public void AddRangeToCollectionBasedOnArrayTest()
            {
                //Arrange
                ICollection<int> list = new Collection<int>();
                list.Add(1);
                //Act
                list.AddRangeToCollection(new[] { 5, 7, 9 });
                //Assert
                Assert.AreEqual(4, list.Count);
            }
            [Test]
            public void AddRangeToCollectionWithNullCollectionThrowsExceptionTest()
            {
                //Arrange
                ICollection<int> list = null;
                IEnumerable<int> add = new[] { 5, 7, 9 };
                //Assert
                Assert.Throws<ArgumentNullException>(() => list.AddRangeToCollection(add));
            }
            [Test]
            public void AddRangeToCollectionWithNullRangeThrowsExceptionTest()
            {
                //Arrange
                ICollection<int> list = new[] { 1, 3, 4 };
                IEnumerable<int> add = null;
                //Assert
                Assert.Throws<ArgumentNullException>(() => list.AddRangeToCollection(add));
            }
1 Like

Does your system have 12 test projects? You could have something similar to what I have. One of the posts in this thread was split out to a separate thread (see above): “Getter/setter properties said not covered with C# analyzer 8.6”

hi @artem.vysotsky

what code coverage tool are you using?

Might be related to the issue I described (and fixed outside of Sonar) in my blog: https://r-vm.com/sonarsource-showing-too-many-conditions-in-code-coverage-with-multiple-dotnetcore-test-projects

We also submitted this SonarSource Reports Invalid Code Coverage when using OpenCover

3 Likes

We use Pipelines with VS Enteprise, so we use the built in Code Coverage tool there

do you have 6 test projects?

if yes, it’s likely what @rvanmaanen mentioned. we opened Fix Code coverage import from multiple test projects · Issue #3296 · SonarSource/sonar-dotnet · GitHub to track and fix for the next release

We’re released sonar-dotnet 8.6.1 , fixing the branch coverage import bug when importing data from multiple test projects. It will get deployed to :sonarcloud: in the following days. You should expect branch (conditional) code coverage to increase.

To be clear:

  • we fixed the import of branch coverage from OpenCover reports (OpenCover and coverlet have good support for branch coverage)
  • we’ve reverted the changes we did for VS coverage and dotCover, which don’t have support for branch coverage

What we did in 8.6 (and reverted in 8.6.1) for VS coverage: we were looking when there were multiple statements per line, some covered and some not covered. And were showing the incomplete coverage using the “conditional coverage” feature from sonarcloud. We’ve reverted this feature because we need to rethink it in order to support coverage results from multiple test projects.