SkillRary

Please login to post comment

Importance of Code Coverage

  • swetha goud
  • Sep 12, 2019
  • 0 comment(s)

Importance of Code Coverage

 

Code coverage is the percentage measure of the code which describes of which the source code of the program has been tested. In general, code coverage system collects information about the running program and combines it with the source information to generate a report on the test suite’s code coverage. In the development process, code coverage is a part of a feedback loop. In this process, as the tests are developed, code coverage will highlight the aspects of the code which may not be tested in sufficient amounts and may require further testing. This loop will continue until coverage meets the specified target.

 

Why is Code Coverage measurement necessary?

 

It is known that unit testing enhances the quality and predictability of any software release. However, it is difficult to know how well a unit actually tests your code and to determine how many tests are good enough. It could also be uncertain if your software needs more tests. All of these doubts can be cleared by code coverage measurements.

 

Coverage measure also helps to avoid test entropy. As code goes through multiple release cycles, there can be a tendency for unit test atrophy. As new code is added, it may not meet the same testing standards you put in place when the project was first released. Measuring code coverage can keep your testing up to the standards you require. You can be confident that when you go into production there will be minimal problems as the code not only passes its test but that it is well tested.

 

 

Types of Code Coverage:

 

Code Coverage is further divided into various subtypes based on its functionalities:

 

  • 1. Statement Coverage

    2.   Decision Coverage

    3.   Branch Coverage

    4.   Toggle Coverage

    5.   FSM Coverage

 

Statement Coverage

 

Statement coverage is a white box test design technique which involves the execution of all the executable statements in the source code at least once. It is used to calculate and measure the number of statements in the source code which can be executed given the requirements.

 

Statement coverage is used to derive a scenario-based upon the structure of the code under test.

 

 

In  White Box Testing, the tester is concentrating on how the software works. In other words, the tester will be concentrating on the internal working of source code concerning control flow graphs or flow charts.

GeGenerally in any software, if we look at the source code, there will be a wide variety of elements like operators, functions, looping, exceptional handlers, etc. Based on the input to the program, some of the code statements may not be executed.

    The goal of Statement coverage is to cover all the possible path's, line, and statement in the code. Let's understand this with   an example, how to calculate statement coverage. Scenario to calculate Statement Coverage for given source code. Here we are taking two different scenarios to check the percentage of statement coverage for each scenario.  

                                                                                                                                                                                                                                                                                                                                                             

Source Code:

Prints (int a, int b) {                       ------------  Printsum is a function

    int result = a+ b;

    If (result> 0)

         Print ("Positive", result)

    Else

         Print ("Negative", result)

    }                                        -----------   End of the source code

                                                                                                                                                             

 

      Scenario 1:                                                                 

     

         If A = 3, B = 9

  

       

 

 

        The statements marked in yellow colour are those which are executed as per the scenario

 

        Number of executed statements = 5, Total number of statements = 7

 

        Statement Coverage: 5/7 = 71%

 

        Likewise, we will see scenario 2,   

 

 

  Scenario 2:

 

    If A = -3, B = -9

 

 

    

 

         The statements marked in yellow colour are those which are executed as per the scenario.

 

         Number of executed statements = 6

 

         Total number of statements = 7

 

 

      Statement Coverage: 6/7 = 85%

       

     But overall if you see, all the statements are being covered by 2nd scenario's considered. So we can conclude that overall         statement coverage is 100%.

       

 

 

What is covered by Statement Coverage?

 

1.  Unused Statements

 

2.   Dead Code

 

3.   Unused Branches

 

 

Decision Coverage

 

Decision coverage reports the true or false outcomes of each Boolean expression. In this coverage, expressions can sometimes get complicated. Therefore, it is very hard to achieve 100% coverage.

That's why there are many different methods of reporting this metric. All these methods focus on covering the most important combinations. It is very much similar to decision coverage, but it offers better sensitivity to control flow.

 

 

Example of decision coverage

 

       Consider the following code-

 

Demo(int a) {                      

     If (a> 5)

         a=a*3