SkillRary

Please login to post comment

Importance of Code Coverage

  • Swetha Y
  • Sep 12, 2019
  • 0 comment(s)
  • 5409 Views

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

     Print (a)      

    }

 

 

    Scenario 1:

 

     Value of a is 2

 

 

         

     

The code highlighted in yellow will be executed. Here the "No" outcome of the decision If (a>5) is checked.

Decision Coverage = 50%

 

 

    Scenario 2:

 

    Value of a is 6

   

 

 

The code highlighted in yellow will be executed. Here the "Yes" outcome of the decision If (a>5) is checked.

 

Decision Coverage = 50%

 

 

                 Test Case

Value of A

Output

Decision  Coverage

                      1

2

2       

50%

2

6

18

50%

 

Branch Coverage

 

In the branch coverage, every outcome from a code module is tested. For example, if the outcomes are binary, you need to test both True and False outcomes.

 

It helps you to ensure that every possible branch from each decision condition is executed at least a single time.

 

By using Branch coverage method, you can also measure the fraction of independent code segments. It also helps you to find out which is sections of code don't have any branches.

The formula to calculate Branch Coverage:

 

Example of Branch Coverage

To learn branch coverage, let's consider the same example used earlier

Consider the following code

Demo(int a) {                       
     If (a> 5)
         a=a*3
     Print (a)
    }                                       

 

 

Branch Coverage will consider unconditional branch as well

Test Case

Value of A

Output

Decision Coverage

Branch Coverage

1

2

2

50%

33%

2

6

18

50%

67%

Advantages of Branch coverage:

 

Branch coverage Testing offers the following advantages:

 

  • Allows you to validate-all the branches in the code
  • Helps you to ensure that no branched lead to any abnormality of the program's operation
  • Branch coverage method removes issues which happen because of statement coverage testing
  • Allows you to find those areas which are not tested by other testing methods
  • It allows you to find a quantitative measure of code coverage

Branch coverage ignores branches inside the Boolean expressions

 

Condition Coverage

Conditional coverage or expression coverage will reveal how the variables or subexpressions in the conditional statement are evaluated. In this coverage expressions with logical operands are only considered.

 

For example, if an expression has Boolean operations like AND, OR, XOR, which indicated total possibilities.

 

Conditional coverage offers better sensitivity to the control flow than decision coverage. Condition coverage does not give a guarantee about full decision coverage

 

The formula to calculate Condition Coverage:

 

Example:

For the above expression, we have 4 possible combinations

  • TT
  • FF
  • TF
  • FT

Consider the following input

X=3

Y=4

(x<y)

TRUE

 

Condition Coverage is ¼ = 25%

A=3

B=4

(a>b)

FALSE

 

Finite State Machine Coverage

 

Finite state machine coverage is certainly the most complex type of code coverage method. This is because it works on the behaviour of the design. In this coverage method, you need to look for how many time-specific states are visited, transited. It also checks how many sequences are included in a finite state machine.

 

How does Code Coverage Work?

 

Although there are various approaches to code coverage measurement, they are broadly classified into three approaches.

 

i.             Source Code Instrumentation:

 

In this approach instrumentation statements are added to the source code and it compiles the code with the normal compile tool chain to produce an instrumented assembly.

 

ii.            Intermediate Code Instrumentation:

 

In this, new compiled files are instrumented by adding new bytecodes and a new instrumented class is generated.

 

iii.           Runtime Information Collection:

 

In this approach, information is collected from the runtime environment as the code executes to determine coverage information.

 

 

Conclusion:

Code coverage is helpful in evaluating a quantitative measures. It also allows you to create extra test cases to increase coverage. It enables you to find the areas of a program which is not exercised by a set of test cases. However, there are also a few disadvantages of using code coverage. It is not possible to determine whether we tested all possible values of a feature with the help of code coverage. Code coverage is also not telling how much and how well you have covered your logic. Even when any specific feature is not implemented ibn design, code coverage will report 100% coverage.


Author: Mithun A

 

 

 


 

 


 

 

 

 


 

 

 


 

 

 

 








Please login to post comment

( 0 ) comment(s)