Term Paper: ON Analysis and Comparison of Statement Coverage of C and C++ Code

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

TERM PAPER

ON
Analysis and Comparison of Statement
coverage of C and C++ Code

Submitted to: Dalwinder Singh


Submitted by:Uday Mohan
Section:KE045
Reg No:11410955
Roll no:A74
Introduction

Statement coverage is a white box testing technique, which involves the


execution of all the statements at least once in the source code. It is a
metric, which is used to calculate and measure the number of statements in
the source code which have been executed. Using this technique we can
check what the source code is expected to do and what it should not. It can
also be used to check the quality of the code and the flow of different paths
in the program. The main drawback of this technique is that we cannot test
the false condition in it.

(Statement coverage = No of statements Exec1uted/Total no of statements


in the source code * 100)

Example:

Read A

Read B

If A>B

Print ”A is greater than B”

Else

Print “B is greater than A”

Endif

Set1: If A=2, B=5

No. of statements executed:5

Statement coverage=5/7*100=71%

Set1:If A=2 ,B=5


No. of statements executed:6

Total no. of statements in the source code:7

Statement coverage =6/7*100 = 85.20 %

This is purely a white box testing method. It tests the software’s internal
coding and infrastructure and so the programmer is the one who should take
the initiative to do this. This technique is very suitable for drupal
programmers and other programmers.

Statement coverage is used to derive scenario based upon the structure of


the code under test.

Statement coverage=(No. of executed students/Total no. of students)*100

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 with respect to control flow graphs or flow charts.

Also, statement testing is concerned with exercising the loop in the


software by referring to either source code or control flow charts.

Different test cases may be derived to exercise the loop once, twice and
many times. This may be done regardless of the functionality of the
software.

Structural based testing is also referred as 'Code Coverage Technique'


which can be used by all levels of testing. Developers use structural based
techniques in component testing or module testing or Unit Testing,
component Integration Testing.

Testing can reveal a fault Defect only when the execution of that
corresponding faulty element causes a failure. For example, if there is a
fault in the statement at line 10 of the source code, it could be revealed by
the test cases which has potential test steps or test data to reveal the
defect in the source code statement.

Generally in any software, if we look at the source code there will be a wide
variety of elements included in operators, functions, looping, exceptional
handlers and etc. But given a set of instruction to perform a task. All the
possible path's, line and statement with respect to the task should be
covered.
Advantages and Disadvantages

 The statement coverage is also known as line coverage or segment


coverage.
 The statement coverage covers only the true condition.
 Through statement coverage we can identify the statements executed
and where the code is not executed because of blockage.
 In this process each and every line of code needs to be checked and
executed.

ADVANTAGES:

 It verifies what the written code is expected to do and not to do


 It measures the quality of code written
 It checks the flow of different paths in the program and it also ensure
that whether those path are tested or not.

DISADVANTAGES

 It cannot test the false conditions.


 It does not report that whether the loop reaches its termination
condition.
 It does not understand the logical operators.

To understand the statement coverage in a better way let us take an example


which is basically a pseudo-code. It is not any specific programming
language, but should be readable and understandable to you, even if you
have not done any programming yourself.

Consider code sample 4.1 :


READ X
READ Y
I F X>Y THEN Z = 0
ENDIF

Code sample 4.1

To achieve 100% statement coverage of this code segment just one test
case is required, one which ensures that variable A contains a value that is
greater than the value of variable Y, for example, X = 12 and Y = 10. Note
that here we are doing structural test design first, since we are choosing our
input values in order ensure statement coverage.

Since we have covered five out of six statements, we have 83% statement
coverage (with three tests). What test would we need in order to cover
statement 5, the one statement that we haven’t exercised yet? How about
this one:

Test 1_4: X = 20, Y = 25

This time the value of Z is 70, so we will print ‘Large Z’ and we will have
exercised all six of the statements, so now statement coverage = 100%.
Notice that we measured coverage first, and then designed a test to cover
the statement that we had not yet covered.

Note that Test 1_4 on its own is more effective which helps in achieving
100% statement coverage, than the first three tests together. Just taking Test
1_4 on its own is also more efficient than the set of four tests, since it has
used only one test instead of four. Being more effective and more efficient is
the mark of a good test technique.
Pros Of Statement Coverage
The main advantages of Statement Coverage are as mentioned below:

 Confirms Code Quality – Statement Coverage confirms the quality


of the code by ensuring that every statement is executed at least
once. It also checks each line of the code to ensure that each
statement actually performs the task that it should ideally be
performing. If the statement does not execute as per requirement,
then it can be fixed to prevent future trouble.

 Checks The Flow Of Statement – Statement Coverage verifies the


flow of the statements. This ensures that each path is tested. It is also
helpful in identifying those areas that prevent the execution of the
code. Such blocks in the flow can thus be identified and timely action
can be taken to correct them.

Cons Of Statement Coverage


The prime disadvantages of Statement Coverage are:

 Covers True Conditions Only – Statement Coverage is restrictive in


nature as it cannot test those conditions that are false. In other words,
only true conditions can be tested. This happens because it fails to
understand the logic operators. When the role of logic operators
comes into play, this metric fails to be completely reliable on its own.

 Cannot Check If Loop Reaches Termination – Codes tend to


consist of loops but Statement Coverage fails to validate if the loop
reaches its end point or the point of termination. This happens
because Statement Coverage is a very simple metric that fails to go
into in-depth paths of the code. It just reads whether the statement
was executed or not but does not make any efforts to see if the
logical loop end was attained or not.
The Ratio
A good measure of how well you exercise the code is the ratio of the
number of executed statements. So, all the statements that your test cases
execute to the total number of statements in the program.

The higher this number, the better you exercise your code.

Also, another way to look at this coverage criterion is in terms of questions.


So, what is the question they were trying to answer when you look at a
specific set of test cases and you assess the statement coverage that they
achieved? It is whether each statement in the program has been executed.
So, the statement coverage is satisfied when all the statements in the
program have been executed.

And you can satisfy it to different degrees and the degrees to which it’s
satisfied is measured by this value.

**Is Statement coverage Sufficient?

Consider a test set T={t1:<x= –5>}.


It is adequate with respect to statement coverage criterion,
but does not reveal the bug.

1.begin
2.int x, z;
3.input(x);
4.if(x<0)
5.x=-x;
6.z=foo-1(x);
7.output(z);
8.end

There should have been an else clause for x³0 before this statement.
Conclusion
Statement Coverage identifies those statements in any given method that
have been executed. By doing so it is able to recognize those chunks or
blocks of code that failed to perform their designated task. Ultimately,
identification of these blockages is the main job of Statement Coverage.
The higher the Statement Coverage number the better is the quality of the
written code. The best part of this testing technique is that it can be
conducted by the code developers themselves and that to continuously as
each subsequent statement is written.

References:
1. https://www.zyxware.com/articles/4161/what-is-statement-
coverage-in-testing.
2. https://www.guru99.com/learn-statement-coverage.html
3.istqbexamcertification.com/what-is-statement-coverage-
advantages-and-disadvantages/
4. https://www.zyxware.com/articles/4161/what-is-statement-
coverage-in-testing
5. www.testingbrain.com/whitebox/statement-coverage.html
6. www.softwaretestingmentor.com/line-coverage/

You might also like