Lec 15

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

Software Engineering

Lecture 15
Main WBT Techniques
• Statement Coverage
• Branch Coverage
• Path Coverage
Path Coverage
• Path coverage tests all the paths of the program.

• This is a comprehensive technique which ensures that all the paths of


the program are traversed at least once.

• Path Coverage is even more powerful than Branch coverage.

• This technique is useful for testing the complex programs and it a


structural testing technique.
• Path coverage is used to test the complex code snippets, which
basically involve loop statements or combination of loops and
decision statements and the tester finds every possible executable
path.
• Consider this pseudocode
INPUT A & B
C=A+B
IF C>100
PRINT “ITS DONE”
END IF
IF A>45
PRINT “ITS PENDING”
END IF

• To simplify these let’s consider below flowchart of the pseudo code we have:
• Now to ensure maximum coverage, we would require 4 test cases.
• How? Simply – there are 2 decision statements, so for each decision
statement, we would need two branches to test. One for true and the
other for the false condition. So for 2 decision statements, we would
require 2 test cases to test the true side and 2 test cases to test the
false side, which makes a total of 4 test cases.
• In order to have the full coverage, we would need following test
cases:
• TestCase_01: A=50, B=60
• TestCase_02: A=55, B=40
• TestCase_03: A=40, B=65
• TestCase_04: A=30, B=30
Loop in the path
Can we cover all the possible paths?
Basis Path Testing
• Any software program includes multiple entry and exit points Testing
each of these points is challenging and time consuming.

• In order to reduce the redundant tests and to achieve maximum text


coverage, basis path testing is used.
• Basis path testing is a method in which test cases are defined based on
flows or logical paths that can be taken through the program.

• Its objective is to define the number of independent paths, so the


number of test cases needed can be defined explicitly to maximize test
coverage.

• Basis path testing involves execution of all possible blocks in a program


and achieves maximum path coverage with the least number of test
cases.
• An execution path is a set of nodes and directed edges in a flow graph
that connects (in a directed fashion) the start node to a terminal node.

• Two execution paths are said to be independent if they do not include


the same set of nodes and edges.

• A basic set of execution paths for a flow graph is an independent set of


paths in which all nodes and edges of the graph are included at least
once.
Steps to be followed for Basis Path Testing:
1. Construction of graph with nodes and edges from the code

2. Cyclomatic Complexity Calculation

3. Identification of independent paths

4. Design of Test Cases


McCabe’s Cyclomatic Complexity
• Cyclomatic complexity of a code section is the quantitative measure
of the number of linearly independent paths in it.

• It is a software metric used to indicate the complexity of a program.

• It is computed using the Control Flow Graph of the program.

• The nodes in the graph indicate the smallest group of commands of a


program, and a directed edge in it connects the two nodes.
Methods to calculate Cyclomatic
Complexity
• Three formulas to calculate:

• V(G) = e – n + 2 (Edges and nodes method)


• V(G) = P + 1 (Branch Method)
• Number of Regions
Example 1
Edges and Nodes Method
• V(G) = e – n + 2

• V(G) = 9 – 8 + 2 = 3

• e=9
• n=8
Predicate Method
• V(G) = P + 1

• V(G) = 2 + 1 = 3

• P=2
Number of Regions
• V(G) = 3
• Basis Path Coverage
There are few conditional statements
that are executed depending on what
condition it suffice. Here there are 3
paths or condition that need to be
tested to get the output.

• Path 1: 1,2,3,5,6, 7
• Path 2: 1,2,4,5,6, 7
• Path 3: 1, 6, 7
Why Cyclomatic Complexity?
• To check if the program is testable?
• To check if the program easy to understand?
• To check if the program easy to modify?
Example 1
GET(A); GET(B);
if A > 15 then
if B < 10 then
B := A + 5;
else
B := A - 5;
end if
else
A := B + 5;
end if;
end XYZ;
Example 2
IF A = 10 THEN
IF B > C THEN
A=B
ELSE
A=C
ENDIF
ENDIF
Solution for Example 2

• V(G) = E – N + 2
• 8-7+2=3
• Rule of thumb:
• Begin restructuring your code with the component with highest V(G)

You might also like