False positive branch coverage

Open
#218 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
66/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
csharp
Domain
testing-qa

Research direction

Start with MicrosoftCoverageTest.zip and run the shown dotnet test command against Class1.Test1. Compare the generated Cobertura report with the single executed path in the test; done means the report no longer marks both branch outcomes as covered for this reproduction.

Written by the indexing model from the issue text.

Description

Class under test

namespace CoverageTest;

public static class Class1
{
    public static int Test1(int n)
    {
        if (n < 10)
            n = Random.Shared.Next();
        return n;
    }
}

Test class

using Xunit;

namespace CoverageTest.Tests;

public static class Class1Tests
{
    [Fact]
    public static void Test1()
    {
        Class1.Test1(2);
    }
}

Command executed

dotnet test -verbosity:diagnostic -c Debug --collect:"Code Coverage;IncludeTestAssembly=False;Format=cobertura;CoverageFileName=R:\coverage\CoverageTest.Tests\cobertura.xml"

Excerpt from coverage results

<method line-rate="1" branch-rate="1" complexity="2" name="Test1" signature="(int)">
  <lines>
    <line number="6" hits="1" branch="False" />
    <line number="7" hits="1" branch="True" condition-coverage="100% (2/2)">
      <conditions>
        <condition number="0" type="jump" coverage="100%" />
      </conditions>
    </line>
    <line number="8" hits="1" branch="False" />
    <line number="9" hits="1" branch="False" />
    <line number="10" hits="1" branch="False" />
  </lines>
</method>

The branch is executed only once, so it should not be possible to have coverage of both sides of the branch (taken and not-taken).

MicrosoftCoverageTest.zip

Dominant language
C#
Stars
125
Forks
17
Avg merge
1h 17m
Merged PRs (30d)
2

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/codecoverage

All issues in microsoft/codecoverage

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.