Professional Documents
Culture Documents
Sabourin 507 - 607 Unit Testing
Sabourin 507 - 607 Unit Testing
Sabourin 507 - 607 Unit Testing
Robert Sabourin
President
AmiBug.Com, Inc.
Montreal, Canada
rsabourin@amibug.com
June 21, 2021 © Robert Sabourin, 2005 Slide 1
AmiBug.Com, Inc.
Unit Testing Workshop
Section 1
• Robert Sabourin ,
Software Evangelist
• President
• AmiBug.Com Inc.
• Montreal, Quebec,
Canada
• rsabourin@amibug.com
Fundamental Question
• How do you know when you are finished?
Crosby on Quality
• “Quality is defined as conformance to
requirements”
• “Quality is not a measure of
GOODNESS”
– Phil B. Crosby, Quality is Free
Edsger W. Dijkstra
• “Program testing can be used to show
the presence of bugs, but never to show
their absence”
Boris Beizer
• “Why software has bugs – the
fundamental problem – Programming is
a bitch.”
• The Frozen Keyboard - 1986
Ken Blanchard
• “Feedback is the breakfast of
champions!”
Watts S. Humphrey
• “… the job of the software
engineer is to deliver high-
quality software products at
agreed cost and schedule …”
• “… even the most
experienced software engineer
injects about one defect for ten
lines of code …”
Tom DeMarco
• “You cannot control what you cannot measure.”
C. Northcote Parkinson
Parkinson’s Law:
“…work expands so as to fill the
time available for its
completion…”
Yoda
Steve Covey
Section 2
About Bugs
Bug Defined
• To make our job more fun, whenever
we have a concern with software, we
call it a “bug”.
A Bug Vocabulary
Fault, Error
mistake in implementation or design
Failure, Conditions
misbehavior of software due to a fault
Symptom
something observed due to a failure
Bug
anything wrong with the software
Quality
value to someone
Defect
legal term. Something is definitely wrong.
Anomaly, Problem, Issue, Concern, Snag
other terms to use for a bug
June 21, 2021 © Robert Sabourin, 2005 Slide 19
AmiBug.Com, Inc.
About Bugs
Software Testing
• It’s all about people! (and the occasional
bug too)
Section 3
Requirements
Design
Development
Testing
Maintenance
June 21, 2021 © Robert Sabourin, 2005 Slide 25
AmiBug.Com, Inc.
Phase
Rational Unified
Inception Elaboration Construction Transition
Process (RUP)
Analysis
Core Workflow
organization.
Design
Development
Testing
Maintenance
June 21, 2021 © Robert Sabourin, 2005 Slide 26
AmiBug.Com, Inc.
Unit Testing in Development
Lifecycle Models
Spiral Development
Requirements
An iteration in the
elaboration phase
Analysis
Design
Implementation
Test
Extreme Programming
Testing done to at the time when the work of one or more task assignments are combined,
thus the term integration.
Integration Units combine and stubs are included for missing elements of the system.
Grey box.
Testing Phase
Customer runs his own testing to confirm that the system meets requirements.
Generally acceptance testing plan has been known by System testing team.
Acceptance
Non-intrusive testing of the system while it is in use to measure quality factors such as
performance, system resource use and to spot check correct operation.
Live
June 21, 2021 © Robert Sabourin, 2005 Slide 35
AmiBug.Com, Inc.
Unit Testing in Development
About Bugs
About Bugs
About Bugs
About Bugs
Bug Quadrants
Urgent Urgent
Severe Not Severe
Things Done
- Do they get reprioritized?
- Are business drivers known?
- Are technical risks known?
REQ BUG
Development FLOW
FLOW
Acceptance testing
Staging Servers As close as possible to live technologies
Operational system
Live Servers Site monitoring
Section 4
A Traditional
Design & implement
Approach
Code of assigned task
Implement corrections
Debug
Rework
Document unit testing
Repeatable, Reviewable Follow Up
June 21, 2021 © Robert Sabourin, 2005 Slide 45
AmiBug.Com, Inc.
Unit Testing Process
Extreme Programming
Extreme Programming
An Agile
Organize dev work into
Testable chunks Approach
Build test, code, data,
stubs, drivers, harness
Plan Tests
Create simplest code
to pass tests
Build Test
Run tests
Code
Correct code or test
Unit Test
Rework
Document unit testing
Repeatable, Reviewable Follow Up
June 21, 2021 © Robert Sabourin, 2005 Slide 48
AmiBug.Com, Inc.
Unit Testing Process
Getting Started
Practical Tip
Testing Ideas
• How to find them?
– Does system do what it is suppose to do?
– Does the system do things it is not supposed to?
– How can the system break?
– How does the system react to it’s environment?
– What characteristics must the system conform to?
– Why have previous or similar projects failed?
– How have previous or similar systems failed?
T e s t H a rn e s s
M o d u le A M o d u le B M o d u le C M o d u le D M o d u le E M o d u le F
T r a n s a c t io n C a p tu re
G e n e ra te d S im u la te d C o m p le te d U n it B e in g C o m p le te d C o m p le te d Test and
S tu b S tu b
fro m T r a n s a c t io n U n it D e v e lo p e d U n it U n it o u tc o m e p ro c e s s
T e s t D a ta te s t r e s u lts
T e s t H a rn e s s
Test Test
C ases R e s u lt s
E la p s e d T im e
Interoperability
Maintainability
Dependability
Expandability
Serviceability
Accessibility
Functionality
Adaptability
Re-usability
Auditability
Operability
Availability
Scalability
Testability
Continuity
Portability
Reliability
Usability
Security
Integrity
Application service provider
Automatic content generator
Customized access
Database access
Delivery
Document access
File sharing
Informational
Interactive
Transaction oriented
User-provided content
Workflow oriented
High Focus
Medium Focus Quality Factors Importance For
Low Focus Different Web Application Types
June 21, 2021 © Robert Sabourin, 2005 Slide 61
AmiBug.Com, Inc.
Unit Testing Process
Brainstorming
Unit Testing Ideas
• Peer review to identify
and clarify testing ideas
which are
– New
– Missing
– Redundant
– Incorrect
– Incomplete
– Inconsistent
– Vague
Rank Credibility
0.0 Wild guess, no credibility
0.1 We know it has been done somewhere
0.2 We have one measurement somewhere
0.3 There are several measurements in the estimated range
0.4 The measurements are relevant to our case
0.5 The method of measurement is considered reliable
0.6 We have used the method in-house
0.7 We have reliable measurements in-house
0.8 Reliable in-house measurements correlate to independent external
measurements
0.9 We have used the idea on this project and measured it
1.0 Perfect credibility, we have rock solid, contract- guaranteed, long-term, credible
experience with this idea on this project and, the results are unlikely to
disappear
Sources of
Information
• Version control system
– Monitor changes
– Track where work is
– Track where stability is
• Encourage finding defects earlier than system
testing
– Inspections of code, design, requirements
– Unit Testing
– Informal code check in peer reviews
June 21, 2021 © Robert Sabourin, 2005 Slide 70
AmiBug.Com, Inc.
Unit Testing Process
Testability
• Can application be controlled
by non-gui api for all business
functions?
• Can application logging be
controlled and logs viewed by
testing?
• Can everything which can be
set or changed be queried
programmatically?
• Is there a static equivalent for
dynamic guis?
Section 5
Example Effort
Breakdown
Task Breakdown Description Days Percent
Example Effort
Breakdown - SQUEEZED
Task Breakdown Description Days Percent
Economics
• Cost of fixing a defect sooner (closer to
when defect was injected) is
significantly less than fixing the defect
later
• Generally
– Find and fix problems as early as
practically possible
Example
Rework Effort
System Testing Phase - Rework Effort
1000.00
900.00
800.00
700.00
300.00
200.00
100.00
0.00
24-Sep 1-Oct 7-Oct 14-Oct 21-Oct 28-Oct 4-Nov 11-Nov 18-Nov 25-Nov 2-Dec 10-Dec 17-Dec 21-Dec
Static Testing
• Testing source code of
software
– Reviews
– Walkthroughs
– Inspections
– Static analysis (automatic or
manual)
– Study code and development
artifacts in order to gauge
correctness and identify defects
Input/Outcome
State Modeling
• Unit Test Case Design
– All state transitions
– All paths through the system
– Rational input
– Unexpected input
– Can we get to all states?
• Equivalence Classes
– Sets of input events which are similar and exercise the
application in the same way!
• Boundary Values
– Sets of input events on or very close to the boundary of
accepted valid input
• Test cases should include a mix of valid and
invalid input for all equivalence classes and
boundary conditions
June 21, 2021 © Robert Sabourin, 2005 Slide 92
AmiBug.Com, Inc.
Unit Testing Approaches
• Input
– Invalid inputs
• at least one test with each possible type of invalid input
for each type of field on a dialogue
• use gray box methods to determine common types of
fields
– Pairs
• at least one test for each possible pair of invalid inputs
• Error Conditions
– Error conditions
• at least one test for each possible error
condition
• pairs of error conditions
Wisdom
• Repeated testing
– Make sure test cases can be repeated
• allows for regression
• allows for data driven test automation
– Test descriptions should be explicit
Section 6
Coverage
Test Coverage
Coverage
• Line Coverage
• Branch Coverage
• Condition Coverage
• Coverage tools
Line Coverage
Branch Coverage
Coverage Example
1 If (condition A)
• Sample code 2 { code A }
3 If (condition B)
4 { code B }
5 If (condition C)
6 { code C }
Full line or branch coverage if A, B, C true
Full condition coverage if A, B, C true and false
Full path coverage if all combinations of A, B, C true or false
Coverage Exercise
• Code sample
• Define test cases which will give full line
coverage
Coverage Exercise
begin
//
// check range - value must be between 1 and 99
//
if ( ( SideA < 1 ) or ( SideA > 99 ) ) then
begin
Msg := 'A out of Range' ;
end
else
begin
if ( ( SideB < 1 ) or ( SideB > 99 ) ) then
begin
Msg := 'B out of Range' ;
end
else
begin
if ( ( SideC < 1 ) or ( SideC > 99 ) ) then
begin
Msg := 'C out of Range' ;
end
end
end
end ;
/
June 21, 2021 © Robert Sabourin, 2005 Slide 106
AmiBug.Com, Inc.
Coverage
Coverage Exercise
begin
//
// check range - value must be between 1 and 99
//
if ( ( SideA < 1 ) or ( SideA > 99 ) ) then A = -100, B=10, C=10
begin
Msg := 'A out of Range' ; A=10,B=-100,C=10
end A=10,B=10,C=-100
else
begin
if ( ( SideB < 1 ) or ( SideB > 99 ) ) then
begin
Msg := 'B out of Range' ;
end
else
begin
if ( ( SideC < 1 ) or ( SideC > 99 ) ) then
begin
Msg := 'C out of Range' ;
end
end
end
end ;
/
June 21, 2021 © Robert Sabourin, 2005 Slide 107
AmiBug.Com, Inc.
Coverage
Coverage Tools
Coverage Tools
Result = 0
Read
A, B, C
Do A Stuff
A True? Yes Result =
Result +4
Do B Stuff
No B True? Yes Result =
Result +2
Do C Stuff
No C True? Yes Result =
Result +1
No Print
Result
Truth Table
A B C Result
Result = 0 False False False 0
False False True 1
False True False 2
False True True 3
True False False 4
True False True 5
True True False 6
Read True True True 7
A, B, C
Do A Stuff
A True? Yes Result =
Result +4
Do B Stuff
No B True? Yes Result =
Result +2
Do C Stuff
No C True? Yes Result =
Result +1
No Print
Result
Read
2
A, B, C
Do A Stuff
3 A True? Yes Result = 4
Result +4
Do B Stuff
No B True? Yes Result = 6
Result +2
5
Do C Stuff
No C True? Yes Result = 8
Result +1
No Print
9
Result
Read
A, B, C
Do A Stuff
A True? Yes Result =
Result +4
Do B Stuff
No B True? Yes Result =
Result +2
Do C Stuff
No C True? Yes Result =
Result +1
No Print
Result
A B C Result Path
Result = 0 1
False False False 0 1,2,3,5,7,9,10
False False True 1 1,2,3,5,7,8,9,10
False True False 2 1,2,3,5,6,7,9,10
False True True 3 1,2,3,5,6,7,8,9,10
True False False 4 1,2,3,4,5,7,9,10
Read
True False True 5 1,2,3,4,5,7,8,9,10
2 True True False 6 1,2,3,4,5,6,7,9,10
A, B, C
True True True 7 1,2,3,4,5,6,7,8,9,10
Do A Stuff
3 A True? Yes Result = 4
Result +4
Do B Stuff
No B True? Yes Result = 6
Result +2
5
Do C Stuff
No C True? Yes Result = 8
Result +1
No Print
9
Result
Useful coverage
information
Section 7
Organization
Bottom Up Testing
• White box method
starting with lower
level units.
• Driver units must be
created for units not
yet completed, each
time a new higher
level unit is added to
those already tested.
June 21, 2021 © Robert Sabourin, 2005 Slide 119
AmiBug.Com, Inc.
Unit Testing Organization
Sandwich Testing
• Combining Bottom
Up and Top Down
approaches.
• Blend of stubs and
drivers depending
on part of software
being exercised.
Platform /
Configuration
• Ensure functionality
operates as required on
Testing
different hardware and
software configurations
• Different versions of
operating systems
• Different locales
• Different versions of www
browsers, plug-ins
• Different versions of any
co-dependent software
June 21, 2021 © Robert Sabourin, 2005 Slide 121
AmiBug.Com, Inc.
Unit Testing Organization
Assertion Testing
• “Asserts”
– Added as in-line code
– Ensure that software is
operating in the correct
state with correct values
of certain variables,
tables, parameters or
other data
– Assertions often
implemented as
MACROS
– Generally disabled in
commercial builds
Assertion Testing
Section 8
Do we still need
independent testing?
• Number of defects introduced at
development and early phases and
discovered at system testing will drop!
• We will focus system testing staff on
issues related to how the application
operates as a system and how it
interacts with different software,
systems and in different environments.
June 21, 2021 © Robert Sabourin, 2005 Slide 125
AmiBug.Com, Inc.
Paradoxes, Problems and Politics
Do we need a Unit
Testing Bug Database?
• Questions?
Appendix A
Appendix B
Equivalence
Equivalence Classes
– Defined
– Purpose
– Governance
– Assumptions
– Fallacies
– RobSab Rules of Thumb
– Budget Oriented
– Multiple Variables
– Examples
Equivalence Classes
• Defined
– An equivalence class is a subset of all possible input values to a field of the software being
tested
– Each member of an equivalence class is assumed to be processed the same way by the
software being tested.
» Assumption made by tester designing or selecting test date
» Assumption may be made based on knowledge of the code
» Assumption need not be true
– Each input field of the software being tested may have many different equivalence classes
– Each equivalence class of an input field of the software being tested is not mutually exclusive
Equivalence Classes
• Purpose
– We define equivalence classes for each input field to an application to help
us focus our testing and to help reduce the number of test cases required.
– If we have decided to invest more testing a particular field then we will model
more equivalence classes for the field
– A minimum of two equivalence classes are recommended for each input field
» Class of valid input
» Class of invalid input
Equivalence Classes
• Governance
– Requirements
» Business logic
» Capabilities
» Ranges
» Constraints
– Code
» Decisions
» Intermediate computations
– Data
» Input fields
» Database
» Internal structures
Equivalence Classes
• Assumptions
– We assume the class exists
– We assume all elements of the class are processed the same
– We assume the class is the best or better class to test
– We assume that defining equivalence classes is a worthwhile
investment, perhaps we could be doing something better with our time
– We assume past experience in equivalence class based testing may
be relevant for the current or future testing projects
Equivalence Classes
– Fallacies
– Fact
Equivalence Classes
• RobSab Rules of Thumb
Equivalence Classes
• Budget Oriented
Equivalence Classes
• Multiple Variables
Equivalence Classes
Example
– Square Root function in Windows Calculator
» Valid Range Real
» Invalid Range Real
» Valid Range Integer
» Invalid Range Integer
» Single Byte Integer
» Double Byte Integer
» Four Byte Integer
» Exponent
» Valid Precision
» Invalid Precision
» Too many digits
» Way too many digits
Equivalence Classes
Example
– Login Name
» Valid Authorized Name
» Valid Unauthorized Name
» Invalid Name
» Mixed Latin Accented Name
» Mixed CJK Name
» Mixed Cyrillic Latin Name
» Mixed Greek Latin Name
» Mixed Latin-2 Latin Name
» Mixed Arabic Latin Name
» Mixed Hebrew Latin Name
» Valid characters too short name
» Valid characters too long name
Equivalence Classes
Example
– Login Name
» Valid characters WAY TOO LONG name
» Leading spaces valid name
» Leading tabs valid name
» Mixed edit symbols valid characters
» Mixed numbers valid characters
» Mixed accented characters valid characters
» ALL CAPS valid name
» ALL Lower Case valid name
» Mixed control characters valid characters
» Mixed alt graphics characters valid characters
» Leading control characters
» Trailing control characters
Equivalence Classes
Example
– Login Name
» HTML Fragments
» Web Pages
» Document Files
» Binary Large Objects (BLOBS)
» JavaScript Fragments
» VB Script Fragments
» SQA Fragments
» Quoted strings
» XML Strings
» PDF Files
» Captured HTTP submits
Equivalence Classes
Example
– Login Name
» Longest possible valid authorized name with appended strings
» Longest possible valid authorized name with prefixed strings
» Special control characters intermixed
» Multiple lines with CR delimiters
» Multiple lines with CR LF delimiters
» Multiple lines with LF CR delimiters
» Multiple lines with LF delimiters
» Names with HL7 message wrappers
» Names wrapped with Braces
Equivalence Classes
Example
– Login Name
» What about Invalid but Authorized names?
» Can we load names directly in authentication database
without going through an API or a GUI or both?
Equivalence Classes
Example
– Phone Number
» Valid and active phone number
» Valid and inactive phone number
» Invalid phone number
» Local
» Long distance
» Toll Free
» Extension
» Dial out prefix
» Overseas prefix
» Six digit phone number
» Seven digit phone number
» Ten digit phone number
Equivalence Classes
Example
– Phone Number
» Time delay pauses
» Format with brackets
» Leading spaces valid number
» Trailing spaces valid number
» Dashes in format
» Brackets in format
» Out of sequence brackets
» Not closed brackets
» Nested brackets
» Out of sequence dashes
» Leading brackets
» Leading dashes
Equivalence Classes
Example
– Phone Number
» Missing dashes
» Missing brackets
» Group of four digits split with spaces
» Spaces instead of dashes
» Spaces instead of brackets
Equivalence Classes
Example
– File Path
» Missing directory
» Existing directory
» Missing file
» Existing file
» Path with spaces
» Path without spaces
» Filename with spaces
» Filename with out spaces
» Valid file name
» File name with accented characters
» Path name with accented characters
» File name without extension
Equivalence Classes
Example
– File Path
» Valid dos filename
» Filename longer than dos limits
» Filename with mixed upper and lower case
» Pathname with mixed upper and lower case
» Visible directory
» Invisible directory
» Access restricted directory
» Access restricted file
Equivalence Classes
Example
– File Path
» What about file system
» What about mounted file systems from foreign sources
Appendix C
Combinations
• Pairs
– Orthogonal array analysis (OATS)
– At least one test cases for each pairing of variables
– Open Source Tool
» allpairs
» www.satisfice.com
» James Bach
Testing
OpSys Browser Win2k Netscape 1 4
OpSys Browser Win2k Opera 1 8
OpSys Browser WinME IE 1 5
OpSys Browser WinME Netscape 2 6, 11
OpSys Browser WinME Opera 1 9
OpSys Server WinXP Apache 1 1
OpSys Server WinXP IIS 1 2
OpSys Server WinXP Coldfusion 1 7
OpSys Server Win2k Apache 2 4, 8
OpSys Server Win2k IIS 1 3
OpSys Server Win2k Coldfusion 1 10
OpSys Browser Server Acrobat OpSys Server WinME Apache 1 11
WinXP IE Apache Installed OpSys Server WinME IIS 1 9
Win2k Netscape IIS Not Installed OpSys Server WinME Coldfusion 2 5, 6
OpSys Acrobat WinXP Installed 2 1, 7
WinME Opera Coldfusion OpSys Acrobat WinXP Not Installed 1 2
OpSys Acrobat Win2k Installed 1 3
OpSys Acrobat Win2k Not Installed 3 4, 8, 10
OpSys Acrobat WinME Installed 3 6, 9, 11
OpSys Acrobat WinME Not Installed 1 5
Browser Server IE Apache 1 1
TEST CASES Browser Server IE IIS 1 3
case OpSys Browser Server Acrobat pairings Browser Server IE Coldfusion 2 5, 10
Browser Server Netscape Apache 2 4, 11
1 WinXP IE Apache Installed 6
Browser Server Netscape IIS 1 2
2 WinXP Netscape IIS Not Installed 6 Browser Server Netscape Coldfusion 1 6
3 Win2k IE IIS Installed 5 Browser Server Opera Apache 1 8
4 Win2k Netscape Apache Not Installed 5 Browser Server Opera IIS 1 9
Browser Server Opera Coldfusion 1 7
5 WinME IE Coldfusion Not Installed 6
Browser Acrobat IE Installed 2 1, 3
6 WinME Netscape Coldfusion Installed 5 Browser Acrobat IE Not Installed 2 5, 10
7 WinXP Opera Coldfusion Installed 4 Browser Acrobat Netscape Installed 2 6, 11
8 Win2k Opera Apache Not Installed 3 Browser Acrobat Netscape Not Installed 2 2, 4
Browser Acrobat Opera Installed 2 7, 9
9 WinME Opera IIS ~Installed 3
Browser Acrobat Opera Not Installed 1 8
10 Win2k ~IE Coldfusion ~Not Installed 1 Server Acrobat Apache Installed 2 1, 11
11 WinME ~Netscape Apache ~Installed 1 Server Acrobat Apache Not Installed 2 4, 8
Server Acrobat IIS Installed 2 3, 9
Server Acrobat IIS Not Installed 1 2
Server Acrobat Coldfusion Installed 2 6, 7
Server Acrobat Coldfusion Not Installed 2 5, 10