Unit 4 Sample Problems and Solutions
Unit 4 Sample Problems and Solutions
Hence for maximum coverage, we need to consider “Branch Coverage”, which will evaluate
the ―FALSE‖ conditions.
In the real world, you may add appropriate statements when the condition fails.
To simplify these let's consider below flowchart of the pseudo code we have:
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
So the path covered will be:
Hence, instead of using each and every input value we can now use any one value from the
group/class to test the outcome. In this way, we can maintain the test coverage while we can
reduce a lot of rework and most importantly the time spent.
For Example:
As present in the above image, an ―AGE‖ text field accepts only the numbers from 18 to 60.
There will be three sets of classes or groups.
Boundary means the values near the limit where the behavior of the system changes. In
boundary value analysis both the valid inputs and invalid inputs are being tested to verify the
issues.
For Example:
If we want to test a field where values from 1 to 100 should be accepted then we choose the
boundary values: 1-1, 1, 1+1, 100-1, 100, and 100+1. Instead of using all the values from 1 to
100, we just use 0, 1, 2, 99, 100, and 101.
Example on Boundary Value Analysis Test Case Design Technique:
Example 1:
Assume, we have to test a field which accepts Age 18 – 56
In this example condition, C1 has two values as true and false, condition C2 also has two
values as true and false. The number of total possible combinations would then be four. This
way we can derive test cases using a decision table.
A systematic state transition diagram gives a clear view of the state changes but it is effective
for simpler applications. More complex projects may lead to more complex transition
diagrams thus making it less effective.
For Example:
It is a testing method without having knowledge about It is a testing method having knowledge about the
the actual code or internal structure of the application actual code and internal structure of the application
This is a higher level testing such as functional This type of testing is performed at a lower level of
testing. testing such as Unit Testing, Integration Testing
It concentrates on the functionality of the system It concentrates on the actual code – program and its
under test syntax's
Black box testing requires Requirement specification White Box testing requires Design documents with
to test data flow diagrams, flowcharts etc.
Black box testing is done by the testers White box testing is done by Developers or testers
with programming knowledge.
Boundary Value Analysis and Equivalence Partitioning explained with a
simple example:
Boundary Value Analysis and Equivalence Partitioning both are test case design strategies in
Black-Box Testing.
Equivalence Partitioning
In this method, the input domain data is divided into different equivalence data classes. This
method is typically used to reduce the total number of test cases to a finite set of testable
test cases, still covering maximum requirements.
In short, it is the process of taking all possible test cases and placing them into classes. One
test value is picked from each class while testing.
For Example, If you are testing for an input box accepting numbers from 1 to 1000 then
there is no use in writing thousand test cases for all 1000 valid input numbers plus other test
cases for invalid data.
Using the Equivalence Partitioning method above test cases can be divided into three sets of
input data called classes. Each test case is representative of a respective class.
So in the above example, we can divide our test cases into three equivalence classes of some
valid and invalid inputs.
Test cases for input box accepting numbers between 1 and 1000 using Equivalence
Partitioning:
#1) One input data class with all valid inputs. Pick a single value from range 1 to 1000 as a
valid test case. If you select other values between 1 and 1000 the result is going to be the
same. So one test case for valid input data should be sufficient.
#2) Input data class with all values below the lower limit. I.e. any value below 1, as an invalid
input data test case.
#3) Input data with any value greater than 1000 to represent the third invalid input class.
So using Equivalence Partitioning you have categorized all possible test cases into three
classes. Test cases with other values from any class should give you the same result.
We have selected one representative from every input class to design our test cases. Test case
values are selected in such a way that largest number of attributes of equivalence class can be
exercised.
Equivalence Partitioning uses fewest test cases to cover maximum requirements.
It's widely recognized that input values at the extreme ends of the input domain cause more
errors in the system. More application errors occur at the boundaries of the input domain.
‗Boundary Value Analysis' testing technique is used to identify errors at boundaries rather
than finding those that exist in the centre of the input domain.
Boundary Value Analysis is the next part of Equivalence Partitioning for designing test cases
where test cases are selected at the edges of the equivalence classes.
Test cases for input box accepting numbers between 1 and 1000 using Boundary value
analysis:
#1) Test cases with test data exactly as the input boundaries of input domain i.e. values 1 and
1000 in our case.
#2) Test data with values just below the extreme edges of input domains i.e. values 0 and
999.
#3) Test data with values just above the extreme edges of the input domain i.e. values 2 and
1001.
Boundary Value Analysis is often called as a part of the Stress and Negative Testing.
Note: There is no hard-and-fast rule to test only one value from each equivalence class you
created for input domains. You can select multiple valid and invalid values from each
equivalence class according to your needs and previous judgments.
For Example, if you divided 1 to 1000 input values invalid data equivalence class, then you
can select test case values like 1, 11, 100, 950, etc. Same case for other test cases having
invalid data classes.
This should be a very basic and simple example to understand the Boundary Value Analysis
and Equivalence Partitioning concept.
Ex of ECP:
A text field permits only numeric characters
Length must be 6-10 characters long
Ex of BVA:
Example 1
Suppose you have very important tool at office, accepts valid User Name and
Password field to work on that tool, and accepts minimum 8 characters and m aximum
12 characters. Valid range 8-12, Invalid range 7 or less than 7 and Invalid range 13 or
more than 13.
Write Test Cases for Valid partition value, Invalid partition value and exact boundary
value.
Write Test Cases for Valid partition value, Invalid partition value and exact boundary
value.
Test Cases 1: Consider test data exactly as the input boundaries of input domain i.e.
values 1 and 1000.
Test Cases 2: Consider test data with values just below the extreme edges of input
domains i.e. values 0 and 999.
Test Cases 3: Consider test data with values just above the ext reme edges of input
domain i.e. values 2 and 1001.
Here are few sample questions for practice on Equivalence Partitioning and BVA.
Question #1)
One of the fields on a form contains a text box that accepts numeric values in the range of 18
to 25. Identify the invalid Equivalence class.
a) 17
b) 19
c) 24
d) 21
Solution:
The text box accepts numeric values in the range 18 to 25 (18 and 25 are also part of the
class). So this class becomes our valid class. But the question is to identify invalid
equivalence class. The classes will be as follows:
Class I: values < 18 => invalid class
Class II: 18 to 25 => valid class
Class III: values > 25 => invalid class
17 fall under an invalid class. 19, 24 and 21 falls under valid class. So the answer is „A‟
Question #2)
In an Examination, a candidate has to score a minimum of 24 marks in order to clear the
exam. The maximum that he can score is 40 marks. Identify the Valid Equivalence values
if the student clears the exam.
a) 22,23,26
b) 21,39,40
c) 29,30,31
d) 0,15,22
Solution:
The classes will be as follows:
Class I: values < 24 => invalid class
Class II: 24 to 40 => valid class
Class III: values > 40 => invalid class
We have to identify Valid Equivalence values. Valid Equivalence values will be there in a
Valid Equivalence class. All the values should be in Class II. So the answer is „C‟
Question #3)
One of the fields on a form contains a text box that accepts alphanumeric values. Identify the
Valid Equivalence class
a) BOOK
b) Book
c) Boo01k
d) Book
Solution:
Alphanumeric is a combination of alphabets and numbers. Hence we have to choose an
option which has both of these. A valid equivalence class will consist of both alphabets and
numbers. Option ‗c‘ contains both alphabets and numbers. So the answer is „C‟
Question #4)
The Switch is switched off once the temperature falls below 18 and then it is turned on when
the temperature is more than 21. When the temperature is more than 21. Identify the
Equivalence values which belong to the same class.
a) 12,16,22
b) 24,27,17
c) 22,23,24
d) 14,15,19
Solution:
We have to choose values from the same class (it can be a valid or invalid class). The classes
will be as follows:
Class I: less than 18 (switch turned off)
Class II: 18 to 21
Class III: above 21 (switch turned on)
Only in Option ‗c‘, all values are from one class. Hence the answer is „C‟. (Please note that
the question does not talk about valid or invalid classes. It is only about values in the same
class)
Question #5)
A program validates a numeric field as follows: values less than 10 are rejected, values
between 10 and 21 are accepted, values greater than or equal to 22 are rejected. Which of the
following input values cover all of the equivalence partitions?
a. 10,11,21
b. 3,20,21
c. 3,10,22
d. 10,21,22
Solution:
We have to select values that fall in all the equivalence class (valid and invalid both). The
classes will be as follows:
Class I: values <= 9 => invalid class
Class II: 10 to 21 => valid class
Class III: values >= 22 => invalid class
All the values from option ‗c‘ fall under all different equivalence classes. So the answer is
„C‟.
Question #6)
A program validates a numeric field as follows: values less than 10 are rejected, values
between 10 and 21 are accepted, values greater than or equal to 22 are rejected. Which of the
following covers the MOST boundary values?
a. 9,10,11,22
b. 9,10,21,22
c. 10,11,21,22
d. 10,11,20,21
Solution:
We have already come up with the classes as shown in question 5. The boundaries can be
identified as 9, 10, 21, and 22. These four values are in option ‗b‘. So the answer is „B‟
Question #7)
In a system designed to work out the tax to be paid:
An employee has £4000 of salary tax-free.
The next £1500 is taxed at 10%.
The next £28000 after that is taxed at 22%.
Any further amount is taxed at 40%.
To the nearest whole pound, which of these groups of numbers fall into three
DIFFERENT equivalence classes?
a) £4000; £5000; £5500
b) £32001; £34000; £36500
c) £28000; £28001; £32001
d) £4000; £4200; £5600
Solution:
The classes will be as follows:
Class I : 0 to £4000 => no tax
Class II : £4001 to £5500 => 10 % tax
Class III : £5501 to £33500 => 22 % tax
Class IV : £33501 and above => 40 % tax
Select the values which fall in three different equivalence classes. Option ‗d‘ has values from
three different equivalence classes. So the answer is „D‟.
Question #8)
In a system designed to work out the tax to be paid:
An employee has £4000 of salary tax-free.
The next £1500 is taxed at 10%.
The next £28000 after that is taxed at 22%.
Any further amount is taxed at 40%.
To the nearest whole pound, which of these is a valid Boundary Value Analysis test case?
a) £28000
b) £33501
c) £32001
d) £1500
Solution:
The classes are already divided in question # 7. We have to select a value which is a
boundary value (start/end value). 33501 is a boundary value. So the answer is „B‟.
Question #9)
Given the following specification, which of the following values for age are in the SAME
equivalence partition?
If you are less than 18, you are too young to be insured.
Between 18 and 30 inclusive, you will receive a 20% discount.
Anyone over 30 is not eligible for a discount.
a) 17, 18, 19
b) 29, 30, 31
c) 18, 29, 30
d) 17, 29, 31
Solution:
The classes will be as follows:
Class I: age < 18 => not insured
Class II: age 18 to 30 => 20 % discount
Class III: age > 30 => no discount
Here we cannot determine if the above classes are valid or invalid, as nothing is mentioned in
the question. (But according to our guess we can say I and II are valid and III is invalid. But
this is not required here.) We have to select values that are in the SAME equivalence
partition. Values from option ‗c‘ fall in the same partition. So the answer is „C‟.
Orthogonal arrays can be applied in user interface testing, system testing, regression testing,
configuration testing and performance testing.
Precise tests
Generate TestCases more quickly and cheaply
Increase coordination among the team
Easy for managers measure the team‘s performance
Make the analysis simple
Isolate defects
Why don‟t use it ?
As any other technique we can find some negative points:
Testing will fail if you fail to identify the good pairs
Probability of not identifying the most important combination which can result in losing
a defect
This technique will fail if you do not know the interactions between the pairs
Applying only this technique will not ensure the complete coverage
It can find only those defects which arise due to pairs, as input parameters
So, you need to choose wisely because not all the applications will suit in this technique, this
depends of the behaviour of your application. You need to measure the priority points of the
project as well, like if you want to cover 100% of the tests of cover a good part of the tests
and save a lot of time…
How to use it ?
Examples:
If we have 3 parameters, each can have 3 values then the possible Number of tests using
conventional method is 3^3 = 27
While the same using OAT, it boils down to 9 test cases.
The array is orthogonal, because all possible pair-wise combinations between parameters
occurs only once.The given L9 Orthogonal Array assess result of test cases as follows:
Single Mode Faults - Single mode faults occur only due to one parameter. For example, in
above Orthogonal array if test cases 7, 8 and 9 show error, we can expect that value 3 of
parameter 1 is causing the error. Likewise we can detect as well as isolate the error.
Double Mode Fault - Double mode fault is caused by the two specific parameters values
interacting together. Such an interaction is a harmful interaction between interacting
parameters.
Multimode Faults - If more than two interacting components produce the consistent
erroneous output, then it is a multimode fault. Orthogonal array detects the multimode faults.
Cyclomatic complexity
This metric was developed by Thomas J. McCabe in 1976 and it is based on a control flow
representation of the program. Control flow depicts a program as a graph which consists of
Nodes and Edges.
In the graph, Nodes represent processing tasks while edges represent control flow between
the nodes.
Flow graph notation for a program:
Flow Graph notation for a program defines several nodes connected through the edges.
Below are Flow diagrams for statements like if-else, While, until and normal sequence of
flow.
Mathematical representation:
Cyclomatic complexity is calculated using the control flow representation of the program
code.
There are 3 commonly used methods for calculating the cyclomatic complexity-
Method-01:
Cyclomatic Complexity = Total number of closed regions in the control flow graph + 1
Method-02:
Cyclomatic Complexity = E – N + 2
Here-
E = Total number of edges in the control flow graph
N = Total number of nodes in the control flow graph
Method-03:
Cyclomatic Complexity = P + 1
Here,
P = Total number of predicate nodes contained in the control flow graph
Note-
Example-01:
Solution-
We draw the following control flow graph for the given code-
Using the above control flow graph, the cyclomatic complexity may be calculated as-
Method-01:
Cyclomatic Complexity
= Total number of closed regions in the control flow graph + 1
=2+1
=3
Method-02:
Cyclomatic Complexity
=E–N+2
=8–7+2
=3
Method-03:
Cyclomatic Complexity
=P+1
=2+1
=3
Example-02:
Solution-
We draw the following control flow graph for the given code-
Using the above control flow graph, the cyclomatic complexity may be calculated as-
Method-01:
Cyclomatic Complexity
= Total number of closed regions in the control flow graph + 1
=3+1
=4
Method-02:
Cyclomatic Complexity
=E–N+2
= 16 – 14 + 2
=4
Method-03:
Cyclomatic Complexity
=P+1
=3+1
=4
Example-03:
Solution-
We draw the following control flow graph for the given code-
Using the above control flow graph, the cyclomatic complexity may be calculated as-
Method-01:
Cyclomatic Complexity
= Total number of closed regions in the control flow graph + 1
=3+1
=4
Method-02:
Cyclomatic Complexity
=E–N+2
= 16 – 14 + 2= 4
Method-03:
Cyclomatic Complexity
=P+1
=3+1
=4
Example 04 -
i = 0;
n=4; //N-Number of nodes present in the graph
while (i<n-1) do
j = i + 1;
while (j<n) do
if A[i]<A[j] then
swap(A[i], A[j]);
end do;
i=i+1;
end do;
Computing mathematically,
V(G) = 9 - 7 + 2 = 4
V(G) = 3 + 1 = 4 (Condition nodes are 1,2 and 3 nodes)
Basis Set - A set of possible execution path of a program
1, 7
1, 2, 6, 1, 7
1, 2, 3, 4, 5, 2, 6, 1, 7
1, 2, 3, 5, 2, 6, 1, 7
Properties of Cyclomatic complexity:
Basis Path testing is one of White box technique and it guarantees to execute atleast one
statement during testing. It checks each linearly independent path through the program,
which means number test cases, will be equivalent to the cyclomatic complexity of the
program.
Example 05 -
If (Condition 1)
Statement 1
Else
Statement 2
If (Condition 2)
Statement 3
Else
Statement 4
As complexity has calculated as 3, three test cases are necessary to the complete path
coverage for the above example.
Steps to be followed:
The following steps should be followed for computing Cyclomatic complexity and test cases
design.
Step 1 - Construction of graph with nodes and edges from the code
Step 2 - Identification of independent paths
Step 3 - Cyclomatic Complexity Calculation
Step 4 - Design of Test Cases
Once the basic set is formed, TEST CASES should be written to execute all the paths.
More on V (G):
Cyclomatic complexity can be calculated manually if the program is small. Automated tools
need to be used if the program is very complex as this involves more flow graphs. Based on
complexity number, team can conclude on the actions that need to be taken for measure.
Following table gives overview on the complexity number and corresponding meaning of v
(G):
Example 06:
Let a section of code as such:
A = 10
IF B > C THEN
A=B
ELSE
A=C
ENDIF
Print A
Print B
Print C
Control Flow Graph of above code
The cyclomatic complexity calculated for above code will be from control flow graph.
The graph shows seven shapes(nodes), seven lines(edges), hence cyclomatic complexity is
7-7+2 = 2.
Example 07:
IF A = 10 THEN
IF B > C THEN
A=B
ELSE
A=C
ENDIF
ENDIF
Print A
Print B
Print C
FlowGraph:
The Cyclomatic complexity is calculated using the above control flow diagram that shows
seven nodes(shapes) and eight edges (lines), hence the cyclomatic complexity is 8 - 7 + 2 =
3
Example 08:
Solution:
1. V(G) = RNumber of regions = 4 , so Cyclomatic Complexity =4
2. V(G) = Predicate node(P) +1 = 3+1 = 4
3. V(G) = E-N+2 = 11 edges – 9 nodes + 2 = 4
Test Cases
Case # Input Values Expected Outcomes Actual Outcomes
Count Length
1 5 101 594
2 5 99 493
3 7 99 99
Data Flow Testing is a type of structural testing. It is a method that is used to find the test
paths of a program according to the locations of definitions and uses of variables in the
program. It has nothing to do with data flow diagrams.
It is concerned with:
Statements where variables receive values,
Statements where these values are used or referenced.
To illustrate the approach of data flow testing, assume that each statement in the program
assigned a unique statement number. For a statement number S-
DEF(S) = {X | statement S contains the definition of X}
USE(S) = {X | statement S contains the use of X}
If a statement is a loop or if condition then its DEF set is empty and USE set is based on the
condition of statement s.There are two types of USE(S), 1. Computation use (C_USE/C) and
predicate use (P_USE/P). A def-use association is a triple (x, d, u,), where:
x is a variable,
d is a node containing a definition of x,
u is either a statement or predicate node
containing a use of x, and there is a sub-path in the flow graph from d to u with no other
definition of x between d and u.
Data Flow Testing uses the control flow graph to find the situations that can interrupt the
flow of the program.
Reference or define anomalies in the flow of the data are detected at the time of associations
between values and variables. These anomalies are:
A variable is defined but not used or referenced,
A variable is used but never defined,
A variable is defined twice before it is used
Advantages of Data Flow Testing:
Data Flow Testing is used to find the following issues-
To find a variable that is used but never defined,
To find a variable that is defined but never used,
To find a variable that is defined multiple times before it is use,
Deallocating a variable before it is used.
Disadvantages of Data Flow Testing
Time consuming and costly process
Requires knowledge of programming languages
Example 1:
1. read x, y;
2. if(x>y)
3. a = x+1
else
4. a = y-1
5. print a;
Control flow graph of above example:
Define/use of variables of above example:
VARIABLE DEFINED AT NODE USED AT NODE
x 1 2, 3
y 1 2, 4
a 3, 4 5
Example 2:
Example 3:
Graph Matrices
To develop a software tool that assists in basis path testing, a data structure.
A graph matrix is a square matrix whose size is equal to the number of nodes on
the flow graph. Each row and collumn corresponds to an identified node, and
matrix entries correspond to connections between nodes.
Example: