Unit Testing and Test Case Strategies
Unit Testing and Test Case Strategies
Dr.Vani Vasudevan 2
Definitions of Testing
Dr.Vani Vasudevan 3
Unit Testing (1 of 2)
■ Unit tests are created using some techniques which ensure that all
logical paths of the code unit are tested, and maximum number of errors
can be detected by executing these tests
■ The Unit Test Plan (UTP) prepared towards the end of the design phase is
used for Unit Testing the software or code
Dr.Vani Vasudevan 4
Unit Testing (2 of 2)
■ Any defects found during unit testing are logged in the Defect
Tracking System (DTS) and they are tracked till the defects are
removed from the code
Dr.Vani Vasudevan 5
Documenting Test Cases (1 of 1)
Test Plan
Dr.Vani Vasudevan 6
Documenting Test Cases (2 of 2)
■ A test case name should be of the following format.
<Module Name>_<Function Name>_<Test Procedure>,
where
– Module Name is the name of the module the test case tests
– Function Name is the name of the function or functionality the test case tests
– Test Procedure is a term or word which briefly represents what the test case is trying to do
■ Test Procedure (Condition to be tested): Explains briefly but clearly, what the test case is
doing.
Dr.Vani Vasudevan 7
Types of Test Cases
■ Test cases are of two types:
Dr.Vani Vasudevan 8
Identifying Test Cases
■ Equivalence Partitioning
■ Logic Coverage
■ Random Generation
Dr.Vani Vasudevan 9
Boundary Value Analysis (1 of 5)
Dr.Vani Vasudevan 10
Boundary Value Analysis (2 of 5)
■ For example if an input condition specifies that the range of
values of the input variable items must be from 0 to 100, the
boundary values would be -1, 0,1,99,100 and 101
Lower Upper
Lower Limit Lower Upper Limit Upper
Limit -1 Limit + 1 Limit -1 Limit + 1
Dr.Vani Vasudevan 11
Boundary Value Analysis (3 of 5)
/**********************************************************************************
* Function: fnFindGrade
* Description:Given the percentage score of student,
* A - 80 to 100
* B - 65 to 79
* C - 55 to 64
* D - 40 to 54
* E - 0 to 39
* Input Parameters:
************************************************************************************/
Dr.Vani Vasudevan 13
Sl Test case name Test Procedure Pre- Expected Reference
N conditio Result to Detailed
o n Design /
Boundary Value Analysis (5 of 5) Spec
Document
1 fnFindGrade_MinusO Call fnFindGrade with None Function fnFindGrade
ne iPercentScore = -1 returns ‘Z’.
(Negative
Test case)
2 fnFindGrade_0 Call fnFindGrade with None Function fnFindGrade
iPercentScore = 0 returns ‘E’
3 fnFindGrade_1 Call fnFindGrade with None Function fnFindGrade
iPercentScore = 1 returns ‘E’
4 fnFindGrade_99 Call fnFindGrade with None Function fnFindGrade
iPercentScore = 99 returns ‘A’
5 fnFindGrade_100 Call fnFindGrade with None Function fnFindGrade
iPercentScore = 100 returns ‘A’
6 fnFindGrade_101 Call fnFindGrade with None Function fnFindGrade
iPercentScore = 101 returns ‘Z’.
(Negative
test case)
Dr.Vani Vasudevan 14
Equivalence Partitioning (1 of 3)
■ This consists of dividing all possible inputs into a set of classes, where
either all inputs that fall into a given class are valid or all are invalid.
Then selecting a few test cases from each class is sufficient.
■ For example if an input condition specifies that the range of values of the
input variable items must be from 1 to 999, one valid equivalence class
would be 1<=items<=999 and 2 invalid equivalence classes would be
items<1 and items>999
■ Most of the equivalence partition test cases are positive test cases
Dr.Vani Vasudevan 15
Equivalence Partitioning (2 of 3)
Sl Test case name Test Procedure Pre- Expected Reference to
No condition Result Detailed
Design / Spec
Document
1 fnFindGrade_E20 Call fnFindGrade None Function fnFindGrade
with iPercentScore = returns ‘E’
20
Dr.Vani Vasudevan 16
Equivalence Partitioning (3 of 3)
Sl Test case name Test Procedure Pre- Expected Reference to
No condition Result Detailed
Design / Spec
Document
6 fnFindGrade_Invalid_Minus30 Call fnFindGrade None Function fnFindGrade
with iPercentScore = returns ‘Z’.
-30 (Negative
Test case)
7 fnFindGrade_Invalid_300 Call fnFindGrade None Function fnFindGrade
with iPercentScore = returns ‘Z’.
300 (Negative
Test case)
Dr.Vani Vasudevan 17
Logic Coverage (1 of 4)
■ This technique aims to generate enough test cases so that an appropriately
defined coverage criterion is met
Dr.Vani Vasudevan 18
Logic Coverage (2 of 4)
Dr.Vani Vasudevan 19
Logic Coverage (3 of 4)
Sl Test case name Test Procedure Pre- Expected Result Reference to
No cond Detailed
ition Design
1 addrbook_all_blank All the fields are None Address book must Address book
kept blank and display an Error Module
click on ‘Search…’ message and prompt
user to enter at least
one field.
(Negative Test case)
2 addrbook_empno_ok Type in an None Address book must Address book
employee number fetch one (only one) Module
(Ex: 7342) and then entry of the person
click on ‘Search…’ with that employee
number
3 addrbook_empno_fail Type in an invalid None Address book must
employee number fetch zero records
and then click and display that
‘Search…’ record is not found.
(Negative Test case)
4 addrbook_email_full Type in a full e- None Address book must Address book
mail id (Ex: fetch one (only one) Module
nagendra_setty) entry of the person
and then click on corresponding to
‘Search…’ the e-mail Id.
Dr.Vani Vasudevan 20
Logic Coverage (4 of 4)
5 addrbook_email_partial Type in a partial None Address book Address book
but valid e-mail id should fetch one or Module
(Ex:nagen) and more records
then click on where e-mail id
‘Search…’ begins with the
same letters.
6 addrbook_email_fail Type in an invalid None Address book must Address book
name (Say jhsgjss) fetch zero records Module
and click on and display that
‘Search…’ record is not found.
(Negative Test
case)
7 addrbook_name_full Type in a full None Address book must Address book
name (Ex: fetch one (only one) Module
Nagendra R Setty) entry of the person
and then click on with that name.
‘Search…’
8 addrbook_name_partial Type in a Partial None Address book Address book
name (Ex: should fetch one or Module
Nagend) and then more records
click on ‘Search…’ where name begins
with the same
letters.
Dr.Vani Vasudevan 21
Random Generation
Dr.Vani Vasudevan 22
Implementing Test Cases (1 of 3)
■ Unit Tests can be executed either manually or can be automated
■ A test script or test program incorporates all the input test data and pre-
conditions (if any) documented in the Unit Test Plan before executing the test
Dr.Vani Vasudevan 23
Implementing Test Cases (2 of 3)
Sl Test case name Test Procedure Pre- Expected Result Reference to
No condition Detailed
Design
6 fnFindGrade_101 Call fnFindGrade with None Function returns fnFindGrade
iPercentScore = 101 ‘Z’. (Negative
test case)
■ Within the test function, when the test case does not result in
the expected output, it is always a good practice to print all
relevant values and information which can help the Software
Engineer to understand the defect and to solve it
Dr.Vani Vasudevan 24
Implementing Test Cases (3 of 3)
int testfnFindGrade_101 () {
const TEST_PASSED 0
const TEST_FAILED -1
char cRet;
return TEST_PASSED;
return TEST_FAILED;
} Dr.Vani Vasudevan 25
Recording / Logging a Defect
■ Any defect found in code or document must be recorded
Dr.Vani Vasudevan 26
Defect Tracking System
■ Most software companies have a dedicated system, for logging
and tracking defects
Dr.Vani Vasudevan 27
A sample defect tracking Excel
sheet (1 of 2)
Defe Submitted Description Detected Assigned Type of Severity Impact Priority Injected Action Notes
ct # by Stage To Defect Stage Taken
1 Vani V When 100 is Unit Martin Logical Medium Medium Medium Coding To be Addition
passed for Testing Error Fixed al notes if
iPercentScore any
fnFindGrade
returns ‘Z’
Dr.Vani Vasudevan 28
A sample defect tracking Excel
sheet (2 of 2)
■ A defect log captures some of the following information:
– Defect Number or Id: The number or Id which identifies the defect
– Submitted by: Person who found the defect
– Description: A detailed description of the defect, along with screen shots or logs excerpts and
any other information
– Detected Stage: The stage at which defect was detected
■ Example: Unit Testing, Code Review etc
– Assigned to: The developer who has to remove this defect
– Type of defect: Type of defect tells about the nature of the defect
■ Example: Coding Standards related, Logical Error
– Severity: How severe the defect is to the software application
■ Example: Cosmetic, Major, Minor and Not Applicable
– Impact: The impact of this defect on the software application
■ High, Medium, Low
– Injected Stage: The stage of software life cycle where this defect might have been introduced
■ High Level Design, Detailed Design, Coding etc
Dr.Vani Vasudevan 29
CODE REVIEW
Code Review (1 of 3)
■ A process where several people offer constructive criticism of a Software
Engineer’s code with a view to simplify it, to make it more efficient and to
eliminate errors
■ All technical reviews are based on the idea that Software Engineers may
overlook some of the trouble spots in their work, and other people don’t
have the same blind spots. So it is beneficial to have someone else look
at their work
■ An essential part of the Build Phase which helps make code bug-free to
a great extent
Dr.Vani Vasudevan 31
Code Review (2 of 3)
■ Locates or identifies potential bugs and failure
points before the code is sent to the user
Dr.Vani Vasudevan 32
Code Review (3 of 3)
■ Types:
– Self Code Review: The person who wrote code reviews his/her own
code using the code review checklist. Defects are fixed as they are
found
– Peer Code Review: The team member reviews the code written by
another team member using the code review checklist
Dr.Vani Vasudevan 33
Pre-Requisites for Code Review
The code has to meet these pre-requisites before it can be reviewed.
If the code does not meet any of the above mentioned pre-requisites, it
should be sent back to the Software Engineer.
Dr.Vani Vasudevan 34
Code Review Checklist
Considers following points for review:
Dr.Vani Vasudevan 35
Summary
■ Unit Testing
■ Code Review
Dr.Vani Vasudevan 36