CH 4 part I
CH 4 part I
Department of
Software
engineering 4th
Year
Software testing and quality
assurance
Presented by: Demeke M.
MEKDELA AMBA U.
Chapter 4 part I
Chapter four
Structural (White Box)
testing
Presented by: Demeke M.
MEKDELA AMBA U.
Control flow testing 3
Introduction
o Software testing technique includes the various strategies or approaches used to
test an application to ensure how it behaves and looks as expected.
o These are software testing techniques which the organization
mustchoose carefully, which to implement on the software application.
o In order to get the most out of each type of testing, and choose the right tools for a
given situation, it’s crucial to understand the benefits and limitations of each type
of testing.
Control flow testing 4
White box testing
o White - box testing also called clear box testing, glass box testing, transparent
box testing, or structural testing is a method of testing software that tests
internal structures or workings of an application [the tester has access to the
internal data structures and algorithms including the code that implement these.]
o Focuses on the internal code structure rather than just input-output
behavior(checking of predefined inputs against expected and desired outputs).
o It ensures software is built correctly according to design specifications.
o White box testing methods are especially useful for revealing design and code-
based control, logic and sequence defects, initialization defects, and data flow defects.
Control flow testing 5
Cont.
o …
Developers do white box testing. In this, the developer will test every line of
the code of the program.
o The developers perform the White-box testing and then send the application
or the software to the testing team, where they will perform the black box
testing and verify the application along with the requirements and identify the
bugs and sends it to the developer.
Control flow testing 6
Why use white box testing?
o Tests software modules independently and ensures proper interaction between
them.
o Evaluates all aspects of code - efficiency, branching logic, module
interfaces, memory organization, and clarity.
o To optimizes code by removing redundant lines and hidden defects.
o Helps in finding security vulnerabilities before release.
o Increases code efficiency and maintainability.
Control flow testing 7
Limitations white box testing?
o Requires skilled testers with coding knowledge.
o Testing 100% of the code is time-consuming and often impractical.
o Not effective for evaluating user experience and high-level
functionality.
Control flow testing 8
White box testing techniques
o The following techniques are used to perform white box
testing.
1. Control flow testing
2. Data flow testing
Control flow testing 9
C ontr ol fl w t e st ing
PART I
Control flow testing 1
0
Control flow testing
o Control flow testing is a one of white box testing technique.
o Control flow is the flow of control from one instruction to another.
o Is a structural testing strategy that use the programs control flow as model.
o The aim of this technique is to determine the execution order of
statements or instructions of the program through a control structure.
o The control structure of a program is used to develop a test case for the program.
o It is often done by the development team. And it is mostly used in unit testing.
Control flow testing 1
Cont. 1
o …
The following are the process that we follow in control flow testing.
• Understand code - read repeatedly.
• Creation of control flow graph - From the provided source code, we can
manually draw graph or by using a tool
• Coverage target - A coverage target is described in the control flow graph
taking help of the nodes, edges, paths, branches etc.
• Creation of test cases - generate test cases.
• Test cases execution - The test cases are executed.
• Analysis of test results - prepare test reports . In this steps, we determine
whether the program is error-free or whether it has certain flaws by analyzing the
Control flow testing 1
2
Control flow graph
o The control flow graph is known as the graphical representation of the complete
flow of the program code(visual representation of sources code).
o It is driven by the complete process followed in the program code.
o It discovers every path that can be covered while running the code.
Control flow testing 1
3
Notation of CFG
o The notation that used in control flow are
• Node – it represent the sequence of procedure which procedure in next to come
so, the tester can determine the sequence of occurrence of procedures.
• Edge – is used to link the direction of node. It represent relation.
• Decision node – is used to decide next node of procedure as per the value.
Simply a node associated with multiple link.
• Junction node – is the point where at least three links meet.
Control flow testing 1
Cont. 4
o Basics of CFG
…
pattern
o If – o While o For o Switch
else Block Block
Control flow testing 1
Cont. 5
o Example 1 of CFG …
1. <?php
2. $a = 5;
3. $b = 10;
4. If($b > $a)
5. {
6. echo
$b;
7. }
8. Else
9. {
10. echo
$a;
11. }
12. ?>
Control flow testing 1
Cont. 6
o Example 2 of CFG …
string check_addition(int a, int b)
{ int sum = a + b;
if (sum > 0) {
return "Positive result";
} else if (sum < 0) {
return "Negative result";
} else {
return "Zero result";
}
}
Control flow testing 1
Control flow testing technique 7
o …
In white box testing, concentration of the tester is on the working of internal source
code and flow chart or flow graph of the code.
o Generally, in the internal source code, there is a wide variety of elements like
operators, methods, arrays, looping, control statements, exception handlers, etc. Based
on the input given to the program, some code statements are executed and some may
not be executed.
o The goal of statement coverage technique is to cover all the possible executing
statements and path lines in the code.
Control flow testing 20
Cont.
o Example 1:
…
1. Int print (int a, int b) {
2. int sum = a+b;
3. if (sum>0)
4. print ("This is a positive result")
5. else
6. print ("This is negative result")
7. }
Control flow testing 2
Cont. 1
Test case a b sum Expected output Covered line Statement…
coverage
o To achieve 100% decision coverage, we need to create test cases that cover
both outcomes of this decision.
Control flow testing 25
Cont.
o Example: …
Test (int a)
{
If(a>4)
a=a*3
Print (a)
}
o Test 1: Value of
a is 7 (a=7)
o Decision point = a>4
o CFG when the value of a is
o Decision Coverage = ½*100
7 is as shown below (Only "True" is
exercised)
=100/2
= 50
o Decision Coverage is 50%
Control flow testing 26
Cont.
o Test 2: Value of a is 3 (a=3) …
o CFG for the above code when
the value of a is 3 is shown
below
oFinaly, we get
oDC = ½*100 (Only "False" is
exercised)
=100/2
= 50
oSo, decision coverage = 50%
Control flow testing 2
Cont. 7
o …
Summary of decision coverage for the above source code are given below
1 3 3 50%
2 7 21 50%
Control flow testing 28
Cont.
o Exercise: Access Control System …
• Consider the following function that determines whether a user is allowed access
based on their age and membership status. Apply decision coverage to test the
following source code?
def access_control(age, is_member):
if age >= 18:
if is_member:
print("Access granted.")
else:
print("Access denied:
Membership
required.")
else:
print("Access denied: Age
Control flow testing 29
Branch coverage
o Branch coverage technique and decision coverage technique are very similar, but
there is a key difference between the two. Decision coverage technique covers all
branches of each decision point whereas branch testing covers all branches of every
decision point of the code.
o Branch coverage technique is a white box testing technique that ensures that every
branch of each decision point must be executed.
o Branch coverage technique is used to cover all branches of the control flow graph. It
covers all the possible outcomes (true and false) of each condition of decision point
at least once.
o Generally, branch coverage follows decision point and branch coverage edges.
Control flow testing 30
Cont.
o Example of branch coverage for login validation …
• Consider the following function that validates a login attempt based
on the username and password:
def validate_login(username, password):
if username == "admin":
if password == "password123":
print("Login successful!")
else:
print("Incorrect password.")
else:
if password == "password123":
print("Incorrect username.")
else:
print("Both username and
Control flow testing 3
Cont. 1
…
Control flow testing 32
o There are 4 branch in the above code, those
Cont.
•areFirst if (username == "admin"): True and False …
• Second if (password == "password123"): True and False
o Then develop test case
1. Test case 1 : validate_login("admin", "password123")
• Execution:
• I f username == "admin": evaluates to true (because the username is "admin").
• if password == "password123": evaluates to true (because the password is
"password123"), so it prints "Login successful!".
• Covered branches:
• if username == "admin": (True)
• if password == "password123": (True)
• Branches tested: 2 branches (True for both conditions).
Control flow testing 33
Cont.
2. Test case 2: validate_login("admin", "wrongpassword") …
• Execution:
• if username == "admin": evaluates to true (because the username is "admin").
• if password == "password123": evaluates to false (because the
password is "wrongpassword"), so it prints "Incorrect password.".
• Covered branches:
• if username == "admin": (True)
• if password == "password123": (False)
• Branches tested: 2 branches (True for the first condition, False for the
second condition).
Control flow testing 34
Cont.
3. Test case 3: validate_login("user", "password123")
…
o Execution:
• if username == "admin": evaluates to false (because the username is "user" and not
"admin").
• if password == "password123": evaluates to true (because the
password is "password123"), so it prints "Incorrect username.".
o Covered branches:
• if username == "admin": (False)
• if password == "password123": (True)
o Branchestested: 2 branches (False for the first condition,True for the
second condition).
Control flow testing 35
Cont.
4. Test case 4: validate_login("user", "wrongpassword") …
o Execution:
• if username == "admin": evaluates to false (because the username is "user").
• if password == "password123": evaluates to false (because the
password is "wrongpassword"), so it prints "Both username and password are
incorrect.".
o Covered branches:
• if username == "admin": (False)
• if password == "password123": (False)
o Branches tested: 2 branches (False for both conditions).
Control flow testing 36
Cont.
o Summary …
• In this login validation example, 100% branch coverage was
achieved by testing the following combinations:
• Test Case 1: Both username and password are correct.
• Test Case 2: username is correct, but password is incorrect.
• Test Case 3: username is incorrect, but password is correct.
•Test Case 4: Both username and password are
incorrect. Branch Coverage = (4/4)×100 = 100%
Control flow testing 37
Cont.
o Exercise: Temperature Control System …
• Test the following function controls the state of a fan based on temperature and
humidity using branch coverage?
def control_fan(temperature, humidity):
if temperature > 30:
if humidity > 70:
print("Turn on fan for cooling
and dehumidifying.")
else:
print("Turn on fan for
cooling.")
else:
if humidity > 70:
print("Turn on fan for
Control flow testing 38
Path coverage
o Path coverage is a white-box testing technique that aims to test all possible paths
through a given program or function. It’s a more comprehensive measure of testing
compared to decision coverage or branch coverage because it takes into account
the full sequence of decisions (paths) a program might take.
o To achieve path coverage, you need to test every possible route (path) through the
code, which is more exhaustive and ensures that all combinations of decisions are
exercised.
o Path coverage done by
Design CFG=> calculate CC => generate set of path => test each path (test case)
Control flow testing 39
Cont.
o Example 1: …
o Total path
1. 1,2,3,4,6,12
2.
1,2,3,4,10,12
Control flow testing 40
Cont.
o Example 2: Banking System …
o Consider a simple function for withdrawing money from an account. The function
checks if the user has enough balance and if the withdrawal amount is valid.
def withdraw(account_balance, amount):
if amount <= 0:
print("Invalid withdrawal amount.")
else:
if account_balance >= amount:
account_balance -= amount
print(f"Withdrawal successful.
New balance:
{account_balance}")
else:
print("Insufficient funds.")
Control flow testing 4
Cont. 1
o First identify path by drawing CFG.
…
o So, from CFG there are 3 distinct path. Those are
1. Path 1: amount <= 0 is true
• This leads to the "Invalid withdrawal amount." message.
• Test case: amount = -1 (or amount = 0)
2. Path 2: amount <= 0 is False, and account_balance >= amount is True.
• This leads to the successful withdrawal.
• Test case: amount = 50, account_balance = 100
3. Path 3: amount <= 0 is False, and account_balance >= amount is False.
• This leads to the "Insufficient funds." message.
• Test case: amount = 150, account_balance = 100
Control flow testing 42
o Total path = 3
o Test case covered = 3
o Path coverage = (number of executed path / total number of path) *
100%
= (3/3) * 100% = 100%
Control flow testing 43
Cont.
o Exercise: … credit
Imagine a loan approval function that checks the applicant's
score, income, and employment status to decide whether to approve or deny a loan
application. Test by applying path coverage for the ff source code?
def approve_loan(credit_score, income, employed):
if credit_score < 600:
print("Loan Denied due to low credit score.")
else:
if income >= 50000:
if employed:
print("Loan
Approved!
")
else:
print("Loan
Denied
due to
Control flow testing 44
Cyclomatic complexity
o Cyclomatic complexity(CC) – is a software metric that provides a quantitative measure of
the logical complexity of the program control flow. It plays a vital role in determining the
necessary scope of testing efforts.[It quantifies the complexity of program].
o Is measure the number of linearly independent paths through a program's source code.
o In simple terms, it provides a way to measure how complex the control flow of a program
is, which can help in identifying areas of the code that may be error-prone or hard to
maintain.
Control flow testing 45
Cont.
o …
Cyclomatic Complexity (V) can be calculated using the following formula
V = E – N + 2P
o Where, E = The number of edges in the control flow graph (CFG)
N = The number of nodes in the control flow graph
(CFG)
P = The number of connected components (usually 1 for a
single program)
o Alternatively, Cyclomatic Complexity can be calculated by counting the
decision points (like if, for, while, case) in the program:
V=D+1
o Where:
• D = The number of decision points (branches, conditional statements).
• +1 is added to account for the entry point of the program (start).
Control flow testing 46
Cont.
Calculate cyclomatic complexity
…
Control flow testing 47
Cont.
Calculate cyclomatic complexity
…
Control flow testing 48
Cont.
o …
Example: for the ff simple program to illustrate how Cyclomatic Complexity
works. def loan_approval(age, credit_score):
if age >= 18:
if credit_score >= 700:
print("Loan Approved!")
else:
print("Loan Denied - Credit
score too low.")
else:
print("Loan Denied - Age too
low.")
1. Step 1: Control Flow Graph (CFG)
2. Step 2: Counting Nodes and Edges
(N = 6) and (E= 8)
3. Step 3: Calculating Cyclomatic
Complexity (V)
Control flow testing 49
Cont.
Cyclomatic complexity interpretation …
Complexity Means
1 The function has a single path, no decision points,
meaning the code is very simple and easy to understand.
1 - 10 Well – written code, testability is high (testing should include
multiple test cases to cover all paths.)
10 – 20 Moderately complex, increase test case. Achieving
complete test coverage becomes more challenging as the
number of paths increases.
>40 Very complex, difficulties in test coverage, maintenance
challenges and higher risk of bug.
Control flow testing 50
Cont.
o Exercise: Find the Cyclomatic Complexity (CC) complexity of the …
following User
Login Function?
def user_login(username, password, is_account_locked):
if is_account_locked:
print("Account is locked. Please contact support.")
elif len(username) < 5 or len(password) < 8:
print("Invalid username or password. Please try again.")
elif username == "admin" and password == "admin123":
print("Login successful. Welcome!")
else:
print("Login failed. Incorrect username or
password.")
Control flow testing 5
Reading 1
assignment
Read more about Path selection ???
Control flow testing 52
Data flow
te sti NEXT
CLASS
Control flow testing 53
ANY
QUESTION!