Lesson 2.1 - Unit Testing Basics
Lesson 2.1 - Unit Testing Basics
Code: N/A
Authors:
Course: <Course-Name>
Conductor: <Acc> – <Unit>
1
Introduction
Agenda(90 minutes)
Unit Test - What and Who?
Unit Test - Why?
Unit Test - How? (method, technique)
Unit Test - Practice (3-5 labs) + Answer
2
Objectives
3
Unit Test – What and Who ?
4
Unit Test – Why ?
5
Cost of bugs
$14,000
85% % Defects
Percentage of Bugs
Introduced in
this phase
% Defects
found in
in this phase
$1000
$ Cost to
$130 $250 repair defect
$25 in this phase
6
Unit Test – When ?
After Coding
Before Integration Test
7
Unit Test – How ?(1) Methodologies
UT processes
Follow the UT process defined in FSOFT UT Guidelines
Black-box testing
Functional testing: ensure each unit acts right as its design
Business testing: ensure the software program acts right as user
requirement
White-box testing
Developer does himself
• Check syntax of code by compiler to avoid syntax errors
• Run code in debug mode, line by line, through all independent paths of program to
ensure that all statement of codes has been executed at least one time
• Examine local data structure to ensure that data stored temporarily maintains its
integrity during all all steps of code execution
• Check boundary conditions to ensure that code will run properly at the boundaries
established as requirements
• Review all error handling paths
Apply WB Test techniques
• See next slides
12
Example Specification
13
Example Test Cases
Test Case 2: Input -10, Return 0, Output "Square root error - illegal negative input“
• Use the second and third statements in the specification
• ("When given an input of less than 0, the error message "Square root error -
illegal negative input" shall be displayed and a value of 0 returned.”).
14
Black box test: Equivalence partitioning
Divide the input of a program into classes of data from which test cases can
be derived. This might help you to reduce number of test cases that must be
developed.
Behavior of software is equivalent for any value within particular partition
A limited number of representative test cases should be chosen from each
partition
Invalid inputs
Outputs
System
Valid inputs
1 >= 0 a >= 0
2 <0 b Error
Test Case 2: Input -10, Return 0, Output "Square root error - illegal negative input“
• Use the < 0 input partition (2)
• Use the “Error” output partition (b)
17
Example Test Cases
1 >= 0 a >= 0
2 <0 b Error
Input
Partitions (1) (2)
- 0 +
1 2 3 4 5
Boundaries and test cases
E
buy dress
F
display “buy successful”
F end if
G end if
G
}
Total Nodes = 7;
A
20
White box test: Decision (branch) coverage
D E 4 in total.
F
4 covered
G
So 4/4 = 100% branch coverage
21
White box test: Path coverage
D E 3 in total.
F
3 covered
G
So 3/3 = 100% path coverage
22
Example: White box test case
23
White box test: Comparison
24
Practice 1
How many unit test cases you must do to check this function?
25
result
26
Discussion
27
Q&A
28