You are on page 1of 48

Software Engineering

Principles
CSI1007

Module – 5
Software Testing
Testing Strategies – Regression Testing
§ Regression testing ensures changes haven't introduced
unintended bugs or broken existing functionalities.
§ It's like a safety net, catching regressions (reintroductions of
bugs) before they reach users.

13/03/24 2
Testing Strategies – Regression Testing
§ Purpose:
§ Verify that code modifications haven't negatively impacted

previously working features.


§ Catch regressions early in the development cycle to
minimize rework and delays.
§ Maintain the overall quality and stability of the software.

13/03/24 3
Testing Strategies – Regression Testing
§ When to Perform:
§ After any code changes, bug fixes, or new feature additions.

§ Following code integrations from different development teams.

§ As part of the testing process before a new software release.

§ Can be automated for efficiency, especially for frequently

modified parts of the codebase.

13/03/24 4
Testing Strategies – Regression Testing
§ Types of Regression Testing:
§ Selective Testing: Focuses on retesting functionalities
directly impacted by the code changes.
§ Full Regression Testing: Re-runs a comprehensive suite of

tests covering all major functionalities. This is often used


for critical releases or when significant changes have been
made

13/03/24 5
Testing Strategies – Regression Testing
§ Benefits:
§ Improves software quality and reliability.
§ Reduces the risk of regressions impacting users.
§ Increases developer confidence in the stability of the codebase.
§ Saves time and resources by catching regressions early.
§ Challenges:
§ Can be time-consuming, especially for full regression testing.
§ Maintaining a large and up-to-date test suite can be a burden.
§ Deciding on the appropriate level of regression testing for different
scenarios
13/03/24 6
Testing Strategies – Others
§ Unit Testing:
§ This strategy tests individual units or components of the

software to ensure they are functioning as intended.


§ For example, in a banking application, you might test the

function that calculates interest to ensure it returns the


correct values.

13/03/24 7
Testing Strategies – Others
§ Integration Testing:
§ This strategy tests the integration of different components

of the software to ensure they work together as a system.


§ For instance, in a web application, you might test how the

user interface interacts with the database.

13/03/24 8
Testing Strategies – Others
§ Functional Testing:
§ This strategy tests the functional requirements of the

software to ensure they are met.


§ For example, in an email application, you might test

whether the “send email” function works correctly.

13/03/24 9
Testing Strategies – Others
§ System Testing:
§ This strategy tests the complete software system to ensure

it meets the specified requirements.


§ For instance, for a mobile app, you might test all its

features, performance, and compatibility with different


devices.

13/03/24 10
Testing Strategies – Others
§ Acceptance Testing:
§ This strategy tests the software to ensure it meets the

customer’s or end-user’s expectations.


§ For example, before releasing a new feature in a product,

you might have a group of users try it out to ensure it


meets their needs and is easy to use.

13/03/24 11
Testing Strategies – Others
§ Performance Testing:
§ This strategy tests the software to determine its
performance characteristics such as speed, scalability, and
stability.
§ For example, you might test how a website handles a large

number of simultaneous users.

13/03/24 12
Testing Strategies – Others
§ Security Testing:
§ This strategy tests the software to identify vulnerabilities

and ensure it meets security requirements.


§ For instance, you might test a web application for common

security vulnerabilities like SQL injection or cross-site


scripting attacks.

13/03/24 13
Testing Methods – White Box Testing
§ It is clear box testing, glass box testing, transparent box testing, and
structural testing.
§ It is a method of software testing that tests the internal structures or
workings of an application.
§ The tester has access to the source code and uses this knowledge to
design test cases that can verify the correctness of the software at the
code level.
§ It involves testing the software’s internal logic, flow, and structure.
§ The tester creates test cases to examine the code paths and logic flows
to ensure they meet the specified requirements.
13/03/24 14
Testing Methods – White Box Testing
§ Key Characteristics:
§ Testers' Skills: Requires programming knowledge and
understanding of the specific programming language used
in the application being tested.
§ Focus: Examines the internal logic, code paths, and data

structures of the software.

13/03/24 15
Testing Methods – White Box Testing
§ Benefits:
§ Identifies logic errors, code inefficiencies, and potential

security vulnerabilities.
§ Ensures all code paths are exercised and tested.

§ More targeted testing based on the code structure.

13/03/24 16
Testing Methods – White Box Testing
§ When to Use White-Box Testing:
§ When the source code and design documents are readily
available.
§ For critical applications where high reliability is essential.
§ When the focus is on testing the internal logic and
structure of the software.
§ Often used in conjunction with other testing techniques
like black-box testing for a more comprehensive approach.
13/03/24 17
Testing Methods – White Box Testing
§ Techniques:
§ Code Coverage: Analyzes how much of the code is actually
executed during testing.
§ Path Testing: Tests all possible execution paths through the
code, including conditional branches.
§ Data Flow Testing: Focuses on how data flows through the
code and validates outputs for various inputs.
§ Mutation Testing: Deliberately introduces errors in the code to
see if the tests can detect them

13/03/24 18
Testing Methods – White Box Testing
§ Techniques:
§ Statement Coverage: In this technique, the aim is to traverse all statements at
least once. Hence, each line of code is tested.
§ Branch Coverage: In this technique, test cases are designed so that each branch
from all decision points is traversed at least once.
§ Condition Coverage: In this technique, all individual conditions must be covered.
§ Multiple Condition Coverage: In this technique, all the possible combinations of
the possible outcomes of conditions are tested at least once.
§ Basis Path Testing: In this technique, control flow graphs are made from code or
flowchart and then Cyclomatic complexity is calculated which defines the number
of independent paths so that the minimal number of test cases can be designed for
each independent path

13/03/24 19
Testing Methods – White Box Testing
§ Limitations:
§ Requires significant time and effort due to the complexity
of analyzing code.
§ Relies heavily on the tester's programming skills and
knowledge of the specific codebase.
§ May not effectively identify usability issues or user
experience problems, which are better suited for black-box
testing.
13/03/24 20
Testing Methods – White Box Testing
§ Statement Coverage
§ Aims to ensure that every single line of code (executable

statement) is executed at least once during testing.

13/03/24 21
Testing Methods – White Box Testing
§ Statement Coverage
§ Example –
def calculate_area(length, width):
if length < 0 or width < 0:
return "Invalid dimensions. Length and width must be non-
negative."
else:
area = length * width
return area

13/03/24 22
Testing Methods – White Box Testing
§ Statement Coverage
§ Test Case 1: calculate_area(2, 3) (Executes all lines,

returns 6)

§ Test Case 2: calculate_area(-1, 4) (Executes all lines,

returns "Invalid dimensions...")

13/03/24 23
Testing Methods – White Box Testing
§ Branch Coverage
§ Focuses on ensuring that each possible outcome of a
conditional statement (branch) is executed at least once.
§ Example: Using the same calculate_area function (Slide 50) :
§ Test Case 1 (from Statement Coverage) covers the "valid
dimension" branch.
§ Test Case 3: calculate_area(2, -5) (Executes the "invalid
dimension" branch, returns "Invalid dimensions...")
13/03/24 24
Testing Methods – White Box Testing
§ Condition Coverage
§ Like branch coverage but focuses on individual conditions

within a decision statement.


§ Ensures each condition evaluates to both True and False

at least once.

13/03/24 25
Testing Methods – White Box Testing
§ Condition Coverage
§ Example - Imagine a function that checks if a number is

even or odd:
def is_even(number):
return number % 2 == 0

13/03/24 26
Testing Methods – White Box Testing
§ Condition Coverage
§ Test Case 1: is_even(4) (Condition number % 2 == 0

evaluates to True, returns True)

§ Test Case 2: is_even(3) (Condition evaluates to False,

returns False)

13/03/24 27
Testing Methods – White Box Testing
§ Multiple Condition Coverage
§ Extends condition coverage by testing all possible
combinations of conditions (when there are multiple
conditions) to ensure they produce the expected results.

13/03/24 28
Testing Methods – White Box Testing
§ Multiple Condition Coverage
§ Example - Example: Let's modify the is_even function to consider
negative numbers:
def is_even(number):
if number == 0:
return "Zero is considered even."
elif number % 2 == 0:
return True
else:
return False
13/03/24 29
Testing Methods – White Box Testing
§ Multiple Condition Coverage
§ Test Case 1 (from Condition Coverage) covers number % 2

== 0.

§ Test Case 3: is_even(0) (Covers the number == 0 condition,

returns "Zero is considered even.")

13/03/24 30
Testing Methods – White Box Testing
§ Basis Path Testing (Cyclomatic Complexity)
§ Analyzes the code's control flow structure using a control

flow graph (CFG).


§ Cyclomatic complexity (CC) is calculated based on the

number of decision points (nodes) and edges in the CFG.


§ It indicates the minimum number of test cases needed to

cover all independent paths through the code.

13/03/24 31
Testing Methods – White Box Testing
§ Basis Path Testing (Cyclomatic Complexity)
§ It is a quantitative measure of the number of linearly
independent paths in the source code of a software
program
§ The nodes in the graph represent the smallest group of
commands of a program, and a directed edge connects two
nodes if the second command might immediately follow the
first command
13/03/24 32
Testing Methods – White Box Testing
§ Basis Path Testing (Cyclomatic Complexity)
Mathematically, cyclomatic complexity (M) is defined as:
M=E−N+2P
where:
E = the number of edges in the control flow graph,
N = the number of nodes in the control flow graph,
P = the number of connected components3.

13/03/24 33
Testing Methods – White Box Testing
§ Basis Path Testing (Cyclomatic Complexity)
§ In the case of a single method, P is equal to 1. So, for a single subroutine,
the formula can be simplified as:
M=E−N+2
§ It is used to indicate the complexity of a program.
§ It helps in determining the number of test cases to achieve branch
coverage.
§ It ensures that every path has been tested at least once, thus helping to
focus more on uncovered paths.
§ It can also improve code coverage and evaluate risks associated with the
program.
13/03/24 34
Testing Methods – White Box Testing
§ Basis Path Testing (Cyclomatic Complexity)
§ Example
IF A = 354 THEN
IF B > C THEN
A=B
ELSE
A=C
END IF
END IF
PRINT A
13/03/24 35
Testing Methods – White Box Testing
§ Basis Path Testing (Cyclomatic Complexity)
§ Step 1 Create a control flow graph

In this graph, nodes represent parts of the code having no


branches, and edges represent possible control flow
transfers during program execution

13/03/24 36
Testing Methods – White Box Testing
§ Basis Path Testing (Cyclomatic Complexity)
Start

IF IF
A = 354 B>C

Print IF IF
A A=B A=C
Print
A

End IF
End IF
Print
END
A
13/03/24 37
Testing Methods – White Box Testing
§ Basis Path Testing (Cyclomatic Complexity)
§ Step 2 – Calculate cyclomatic complexity:

§ Method-01:
§ Cyclomatic Complexity = Total number of closed regions
in the control flow graph + 1

13/03/24 38
Testing Methods – White Box Testing
§ Basis Path Testing (Cyclomatic Complexity)
§ Step 2 – Calculate cyclomatic complexity:

§ Method-02:
§ Cyclomatic Complexity = E – N + 2,
§ where E is the total number of edges in the control flow
graph and
§ N is the total number of nodes in the control flow graph.

13/03/24 39
Testing Methods – White Box Testing
§ Basis Path Testing (Cyclomatic Complexity)
§ Step 2 – Calculate cyclomatic complexity:

§ Method-03: Cyclomatic Complexity = P + 1,


§ where P is the total number of predicate nodes
contained in the control flow graph.
§ Predicate nodes are the conditional nodes.
§ They give rise to two branches in the control flow graph.

13/03/24 40
Testing Methods – White Box Testing
§ Basis Path Testing (Cyclomatic Complexity)
§ Method-01: Cyclomatic Complexity = 2 (closed regions) + 1

=3
§ Method-02: Cyclomatic Complexity = 8 (edges) – 7 (nodes)

+2=3
§ Method-03: Cyclomatic Complexity = 2 (predicate nodes) +

1=3

13/03/24 41
Testing Methods – White Box Testing
§ Basis Path Testing (Cyclomatic Complexity)
§ So, the cyclomatic complexity of the given code is 3 according
to all three methods.
§ This means there are three linearly independent paths through
the code, which corresponds to the three possible paths of
execution:
§ one where A is not equal to 354,
§ one where A is equal to 354 and B is greater than C, and
§ one where A is equal to 354 and B is not greater than C.
13/03/24 42
Testing Methods – White Box Testing
§ Data Flow Testing
§ Focuses on how data flows through the program,
particularly where variables are assigned values and how
those values are used.
§ It ensures proper data manipulation and avoids
unintended side effects.

13/03/24 43
Testing Methods – White Box Testing
§ Data Flow Testing
§ Example
def factorial(n):
if n < 0:
return "Factorial is not defined for negative numbers."
elif n == 0:
return 1
else:
fact = 1
for i in range(1, n + 1):
fact *= i
return fact

13/03/24 44
Testing Methods – White Box Testing
§ Data Flow Testing
§ Data flow testing would involve checking:

§ Initialization of fact to 1
§ The loop's iteration and multiplication of fact
§ The return value for different input scenarios

13/03/24 45
Testing Methods – White Box Testing
§ Loop Testing
§ Specifically designed to test various aspects of loops (for,

while, do-while) in the code, ensuring they execute


correctly under different conditions.

13/03/24 46
Testing Methods – White Box Testing
§ Loop Testing
§ Example: Using the factorial function:
§ Test Case 1: factorial(0) (Tests the loop's behavior when it
doesn't iterate)
§ Test Case 2: factorial(5) (Tests the loop's iteration and

multiplication)

13/03/24 47
Testing Methods – White Box Testing
§ Loop Testing
§ Example: Using the factorial function:
§ Test Case 1: factorial(0) (Tests the loop's behavior when it
doesn't iterate)
§ Test Case 2: factorial(5) (Tests the loop's iteration and

multiplication)

13/03/24 48

You might also like