Introduction

Code coverage is an important asset for a project development.It brings stability in the function of the code block under measure and make the code block stable.The more stability in the blocks of code, the more stability will come in the modules/use cases and henceforth a well balanced unit test cases can be written with less bugs and finally a stable version of the software/modules can be released in the production with least Mean Time To Failure(MTTF) which on the other hand ensures better project/product quality.In this article, we will look into various aspects of Code Coverage in Visual Studio Premium 2013 with a case study.

What is Code Coverage?

It is a verification metric of determining how many lines of code in a given binary are measured when we run test cases against it. By analyzing the result of the code coverage of the Test Methods, we can figure out how much code has been tested for the specified test method. It also provides valuable information to the developer(s) as if the code block is partially, completely or not at all tested which on the other hand helps them to include more test cases to solidify the various boundaries under measure.By knowing how many lines of code of a certain code block are touched, we can judge how well the code/function is tested.

It also provides information about which lines of code have been executed and how long it took to execute them.

What not is Code Coverage for?

It never tells us if the code is well written.

It is not a replacement for through testing since it does not cover all the use cases. Rather, it is a metric for functional testing.

Case Study: Check if a number is positive or negative

Fire up Visual Studio and create a Class Library project. Name it as UtilityLibrary and add a class by the name Utilities.cs and add the below code to it.

In the Test Method TestForNegativity, we are passing the value as "0" as input value to the CheckPositiveNegativeNumber function and we are checking the actual value with the expected value which we have already set to "0". Now, run the test case either by pressing Ctrl +R,T or by right clicking on the TestForNegativity Test Method as under

After the successful execution of the test(s), Visual Studio calculates the code coverage and displays it in "Code Coverage Results" window

So from the above diagram we can figure out how much code has been covered and it shows that 1 Bolck has not been covered. Hence the % block covered is not 100%. Now in-order to figure out what portions of the code block has been covered fully, partially or not at all, we can use the Show Code Coverage Coloring option of Code Coverage Results window as shown under

So let's click on that and we will get the below

Let us understand the various coloring's

Light blue - Covered

Light orange - Partially Covered

Light red - Not covered

But it is very difficult to understand from the piece of code being written as what has been covered and what not. So let us change our program as below

Reference

Conclusion

Hope this will be helpful for writing better unit test cases and will bring more stability in the final product/project once it reaches to production environment. Thanks for reading. Zipped file attached

N.B.~ The program presented here is neither to make a comparison between Ternary Operator and If..Else construct nor to show present a bad/good way of writing a code (as mentioned earlier Code Coverage never tells us if the code is well written)