Understanding Of Code Coverage Analysis 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 the test methods, using Test Explorer.

Analyze code coverage on unit tests in Test Explorer

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

    Analyze code coverage
    Image 1: Analyze code coverage

    Code Coverage Results

    Code coverage results
    Image 2: Code coverage results

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

    Test Method

    Test method
    Image 3: Test method

    Actual Method

    Actual method
    Image 4: Actual method

Discussion on Code Coverage %

When any programmer starts writing the Unit testing, he will have one question in mind --  how much we need to aim for code coverage percentage. Thus, he will go and keep on asking his Manager about code coverage percentage.

Below is the discussion that happened between programmers and the manager about the code coverage percentage.

Code coverage discussion
Image 5: Code coverage discussion

Programmer 1: I am ready to write some unit tests. What code coverage should I aim for?

Manager: Don’t worry about the 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 the Manager.

Manager: He pointed at a pot of the 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 to your question.

Programmer 2: Smiled and left.

Programmer 3: He also came and asked the same question to the Manager about the code coverage.

Manager: Got frustrated and said 80 percent and no less with a stern voice.

Programmer 3: Smiled and left.

One programmer (programmer 4) observed these conversations and approached the Manager

Programmer 4: "Sir, today I heard your answer regarding the same question about the code coverage with three different answers. Why?”

Manager: Replied as below:

Programmer 1 is new and getting started with Unit testing. Thus, I want him to write unit cases first instead of concentrating on the code coverage.

Programmer 2 is quite experienced at programming and testing. When I replied by asking him, how many grains of rice should I put in a pot, I helped him to realize that the amount of testing necessary depends on a number of factors and he knows these 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 of focusing on the code coverage percentage.

Up Next
    Ebook Download
    View all
    Learn
    View all