Block coverage

Eike 1 Reputation point
2021-01-20T16:35:07.713+00:00

Hello!

We're using (absolute) block coverage as a measure if code has been extended without extending tests.
But I got my doubts it works as it should...

I have got the following class, ran testing, changed other code and ran testing again.

private class ChannelGroup : IChannelGroup  
{  
    public ChannelGroup(int number, String name)  
    {  
        this.number = number;  
        this.name = name;  
    }  
    public int number { get; set; }  
    public string name { get; set; }  

    public override string ToString()  
    {  
        return name + ": " + number;  
    }  
}  

The ToString() method is not yet covered.
I got an "Not covered (Blocks)" number of 5 for ToString() in the first run (which I don't understand in the first place, but that might be a different question).
Running the same tests on the unchanged class again gave me a number of 6. Can this be correct?

58794-first-run.png

58773-second-run.png

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,292 questions
Visual Studio Testing
Visual Studio Testing
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Testing: The act or process of applying tests as a means of analysis or diagnosis.
329 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Tianyu Sun-MSFT 27,431 Reputation points Microsoft Vendor
    2021-01-21T03:17:17.467+00:00

    Hello @Eike ,

    Thank you for taking time to post this issue in Microsoft Q&A forum.

    This issue is related to the understanding of “Code Coverage (Blocks)”. Actually, a block is a piece of code with exactly one entry and exit point, and a line of code can contain more than one code block.

    For detailed explanation, you can refer to this document: Report in blocks or lines:

    “Code coverage is counted in blocks. A block is a piece of code with exactly one entry and exit point. If the program’s control flow passes through a block during a test run, that block is counted as covered. The number of times the block is used has no effect on the result.”

    “A line of code can contain more than one code block. If this is the case, and the test run exercises all the code blocks in the line, it is counted as one line. If some but not all code blocks in the line are exercised, it is counted as a partial line.”

    If you do some tests, you will find that when you call to test the ToString() method in your unit test project, the Code Coverage Coloring feature will color all covered code lines in ChannelGroup class, and if you don't unit test the ToString() method, the Code Coverage Coloring feature will color all not covered code lines in ChannelGroup class.

    Some sample unit test screenshots based on your code snippets:

    58890-unittest1.png

    58943-unittest2.png

    So actually these methods such as ChannelGroup() number{ get; set;} name{get;set;}are used too.

    I will recommend you change to use the “Code Coverage (Lines)”, as “Code Coverage (Lines)” is more close to the size of the fragments that you see in the source code.

    Right-click in Code Coverage Results window > select Add/Remove columns… > check the Covered (Lines) and Not Covered (Lines) checkbox.

    58963-unittest3.png

    Best Regards,
    Tianyu

    • If the answer is helpful, please click "Accept Answer" and upvote it.
      Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.