Sunday, 24 July 2016

Understanding of Code Coverage Analysis in C#

In my previous post i have explained about Unit testing in c#

Code Coverage is to determine to what portion of your project code is being tested by Unit testing, you can use the code coverage feature of Visual Studio.

Code coverage is an option when you run test methods using Test Explorer.

Analyze code coverage on unit tests in Test Explorer:

1.       On the Test menu, choose Analyze Code Coverage.




Code Coverage Results:



2.       To see which lines have been run, choose    Show Code Coverage Coloring IconShow Code Coverage Coloring.




Test Method:

Actual Method:













Discussion on Code Coverage %:

When any programmer starts writing the Unit testing, they will have one question in mind, how much we need to aim for code coverage percentage. So they will go and keep on asks to their Manager about Code coverage percentage.

Below is the discussion happened between Programmers and Manager about Code Coverage percentage.
























Programmer 1: I am ready to write some unit tests. What code coverage should I aim for?
Manager: Don’t worry about coverage, just write some good tests.
Programmer 1: Smiled and left.
 -------------------------------------------------------------
Programmer 2: He also asked the same question (What code coverage should I aim for?) to Manager.
Manager: pointed at a pot of boiling water and said: “How many grains of rice should I put in that pot?”
Programmer 2: “How would I know? It depends on how many people you need to feed, how hungry they are, what other food you are serving, how much rice you have available, and so on.”
Manager: Exactly, you are correct. That will be the answer for you question.
Programmer 2: Smiled and left.
 ----------------------------------------------------------------
Programmer 3: He also came and asked the same question to the Manager about code coverage.
Manager: frustrated and Said 80 percent and no lees with some stern voice.
Programmer 3: Smiled and left.
 --------------------------------------------------------------------
These whole conversation observed one programmer (Programmer 4) and approached Manager
Programmer 4: Sir today I overheard you answer the same question about code coverage with three different answers. Why?”

Manager: Replied like below

Programmer 1 is new and getting started with Unit testing, so I want him to write unites cases first instead of concentrating on Code coverage.
Programmer 2 has quite experience at programming and testing. When I replied by asking him how many grains of rice I should put in a pot, I helped him to realize that the amount of testing necessary depends on a number of factors, and he knows those factors better than I do.
Programmer 3 always wants only simple answers – even when there are no simple answers … and then he does not follow them anyway


Conclusion: You should focus on writing good Unit test cases instead focus on the code coverage percentage.

No comments:

Post a Comment