Testing: Luca Viganò
Testing: Luca Viganò
Luca Vigan
Ingegneria del Software, AA 10/11, 04.05.11
The slides in this section are partly based on the courses Software Engineering by Prof. Peter Mller, ETH Zurich, Software Engineering I by Prof. Bernd Brgge, TU Mnchen and Software Engineering by Prof. Jan Vitek, Purdue University
Slide 8!
We make mistakes
Unclear requirements, miscommunication Wrong assumptions (e.g., behavior of operating system) Design errors (e.g., capacity of data structure too small) Coding errors (e.g., wrong loop condition)
7. Testing Basics
Goal of Testing
An error is a deviation of the observed behavior from the required (desired) behavior
- Functional requirements (e.g., user-acceptance testing) - Nonfunctional requirements (e.g., performance testing)
Testing is a process of executing a program with the intent of finding an error A successful test is a test that finds errors
Definition: testing
To test a software system is to try to make it fail
7. Testing Basics
Limitations of Testing
Testing can only show the presence of bugs, not their absence. [E. W. Dijkstra] It is impossible to completely test any nontrivial module or any system
- Theoretical limitations: termination - Practical limitations: prohibitive in time and cost
Test Stages
Analysis System Test
System Design
Integration Test
Detailed Design
Unit Test
Implementation
Peter Mller Software Engineering, SS 11
10
Unit Testing
Testing individual subsystems (collection of classes)
Detailed Design Model Subsystem Code
Unit Test
Goal: Confirm that subsystem is correctly coded and carries out the intended functionality
JUnit: Overview
Provides a framework for running test cases Test cases !! Written manually !! Normal classes, with annotated methods Input values and expected results defined by the tester Execution is the only automated step
17
Test Execution
Execute the test cases Re-execute test cases after every change
- Automate as much as possible - For instance, after each refactoring
Regression testing
- Testing that everything that used to work still works after changes are made to the system - Also important for system testing
18
19
Integration Testing
Testing groups of subsystems and eventually the entire system
Subsystem Code Subsystem Code Subsystem Code
20
Steps in Integration-Testing
1. Select a component to be tested
- Unit test all the classes in the component
3. Do the testing
- Functional testing, structural testing, performance testing
4. Keep records of the test cases and testing activities 5. Repeat steps 1 to 4 until the full system is tested
21
Call hierarchy
A
22
C
D E F
Whole System
Integration Test
G
Peter Mller Software Engineering, SS 11
23
Bottom-Up Strategy
Strategy
1. Start with subsystems in lowest layer of call hierarchy 2. Test subsystems that call the previously tested subsystems 3. Repeat until all subsystems are included Pros - Useful for integrating object-oriented systems and systems with strict performance requirements
Peter Mller Software Engineering, SS 11
24
B
E B,E,F A C D E F G
Whole System
D,G
G
Peter Mller Software Engineering, SS 11
25
Top-Down Strategy
Strategy
1. Start with subsystems in top layer of call hierarchy 2. Include subsystems that are called by the previously tested subsystems 3. Repeat until all subsystems are included
Cons - Writing stubs is difficult: Stubs must allow all possible conditions to be tested - Possibly very large number of stubs required
26
C
D
E F G
Whole System
27
Sandwich Strategy
Combines top-down with bottom-up strategy
How do you select the target layer if there are more than three layers?
- Try to minimize the number of stubs and drivers
Peter Mller Software Engineering, SS 11
28
Target layer
B B,E,F E Bottom / target D,G Whole System F G C D
E
B G D A C
A,B,C, D
Top / target
29
31
System Testing
Testing the entire system
Requirements Specification Entire System System Test
Goal: Determine if the system meets the requirements (functional and non-functional)
32
33
Functional Testing
Goal: . Test functionality of system
- System is treated as black box
Black-box testing !! Properties of IUT available only through specification Alternative names: responsibility-based, functional
A comparison
White-box
IUT internals Focus Origin of test cases Typical use Who?
Black-box
Knows internal structure No knowledge & implementation Ensure coverage of many Test conformance to execution possibilities specification Source code analysis Unit testing Developer Specification Integration & system testing Developers, testers, customers
34
Performance Testing
Stress Testing
- Stress limits of system (maximum number of users, peak demands)
Volume testing
- Large amounts of data
Configuration testing
- Various software and hardware configurations
Compatibility testing
- Backward compatibility with existing systems
Security testing
- Try to violate security requirements (red team)
Peter Mller Software Engineering, SS 11
35
Environmental testing
- Tolerances for heat, humidity, motion
Quality testing
- Reliability, maintainability, and availability
Recovery testing
- Systems response to presence of errors or loss of data
Usability testing
- Tests user interface with user
Peter Mller Software Engineering, SS 11
36
Acceptance Testing
Goal: Demonstrate that the system meets customer requirements and is ready to use Choice of tests is made by client
- Many tests can be taken from integration testing
37
Beta test
- Conducted at clients site (developer is not present) - Software gets a realistic workout in target environment - Potential client might get discouraged
38
Independent Testing
Programmers have a hard time believing they made a mistake
- Plus a vested interest in not finding mistakes - Often stick to the data that makes the program work
39
40
41
42
Testing Steps
Select what will be tested
What parts of the system? What aspects of the system?
What are the test data? How is the test carried out?
43
44
Assuming that double represents 64-bit values, we get (264)3 1058 possible values for a, b, c Programs with heap data structures have a much larger state space!
45
46
Disadvantages
- Treats all inputs as equally valuable
47
One solution
a = 0 and b 0 or a 0 and b2-4ac = 0
No solution
a = 0, b = 0, and c 0 or a 0 and b2-4ac < 0
48
Attempts to find
- Incorrect or missing functions - Interface errors - Performance errors
Limitations
- Does not effectively detect design and coding errors (e.g., buffer overflow, memory management) - Does not reveal errors in the specification (e.g., missing cases)
Peter Mller Software Engineering, SS 11
49
Error might still be missed, for instance, when case is tested with a==1, b==2, c==1
50
51
Random testing Goal: Cover corner cases Black-box test Suitable for all test stages
Peter Mller Software Engineering, SS 11
7. Testing Basics
52
Summary
Main objective
- Design tests that systematically uncover different classes of errors with a minimum amount of time and effort - A good test has a high probability of finding an error - A successful test uncovers an error
Secondary benefits
- Demonstrate that software appears to be working according to specification (functional and non-functional) - Data collected during testing provides indication of software reliability and software quality - Good testers clarify the specification (creative work)
Peter Mller Software Engineering, SS 11
GUI Testing
Model-View-Controller
Views
VIEW
Model
Model-View Controller
View
! Renders the model ! Sends user gestures to controller ! Allows controller to select view Feature calls Events
View selection
Controller
! Defines application behavior ! Maps user actions to model User gestures updates ! Selects view for response ! One for each functionality
Test Data
8. Test Data
8.1 Functional Testing 8.2 Structural Testing 8.3 Automatic Test Case Generation 8.4 Dynamic Test Generation 8.5 Mutation Testing
System Test
Integration Test
During test-driven development, when code is not yet written
Unit Test
Peter Mller Software Engineering, SS 11
8. Test Data
8.1 Functional Testing
8.1.1 Partition Testing 8.1.2 Selecting Representative Values 8.1.3 Combinatorial Testing
8.2 Structural Testing 8.3 Automatic Test Case Generation 8.4 Dynamic Test Generation 8.5 Mutation Testing
Peter Mller Software Engineering, SS 11
- Each possible input belongs to at least one of the equivalence classes - Goal: some classes have higher density of failures
Failure No failure
year
(year mod 4 = 0 and year mod 100 0) or year mod 400 = 0
Non-leap years
year
(year mod 4 = 0 and year mod 100 0) or year mod 400 = 0
Non-leap years
year
year mod 4 = 0 and year mod 100 0
year mod 4 0
year mod 400 = 0 year mod 100 = 0 and year mod 400 0
Sources of Information
Use analysis knowledge to determine test cases that check requirements
Analysis Model Functional Model Analysis Object Model Dynamic Model
Input / output behavior, valid and invalid input Data manipulated by the system
10
11
System Steps
2. Bankomat displays options
12
Relevant information
- Classes and attributes - Subtypes - Aggregations and multiplicities
13
1..*
Copy Recall: library members may borrow at most 6 items (12 for staff members)
Staff Member
borrow( Journal )
Journal
14
Equivalence Classes
Consider the operation member.borrow( copy )
member copy
Library member
Staff member Invalid library member Invalid staff member
05 borrowed copies
0 11 borrowed items more than 5 borrowed copies more than 11 borrowed items
Copy on shelf
Copy borrowed by library member Copy borrowed by staff member Invalid
15
16
<<Entity>> :Engine
<<Entity>> :RequestPool
[ floor = nextStop ]
[ nextStop = ] [ else ]
Test both cases
17
Equivalence Classes
Consider the operation controller.signal( floor )
controller Serve only request Serve first request floor = nextStop, and there is no further request floor = nextStop, and there are further requests
Serve no request
floor nextStop
18
Idle
Requested Stop
request( floor )
Moving
Test all cases signal( floor ) [ floor nextStop ]
request( floor )
Peter Mller Software Engineering, SS 11
19
Equivalence Classes
Consider the operation controller.signal( floor )
controller Reach floor controller is in state Moving and floor = nextStop controller is in state Moving and floor nextStop
Keep moving
Invalid state
20
8. Test Data
8.1 Functional Testing
8.1.1 Partition Testing 8.1.2 Selecting Representative Values 8.1.3 Combinatorial Testing
8.2 Structural Testing 8.3 Automatic Test Case Generation 8.4 Dynamic Test Generation 8.5 Mutation Testing
Peter Mller Software Engineering, SS 11
21
22
Boundary Testing
Given an integer x, determine the absolute value of x
x Valid all values
int abs( int x ) { if( 0 <= x ) return x; return x; } Negative result for x==Integer.MIN_VALUE
23
There is only one value Choose all values Choose 1 and 12 plus one more Choose MIN_VALUE, 0, 13, MAX_VALUE
24
year Standard leap years Standard nonleap years Special leap years Special nonleap years year mod 4 = 0 and year mod 100 0 year mod 4 0
25
Analogous test cases for February in non-leap year, months with 30 days, and months with 31 days
Peter Mller Software Engineering, SS 11
26
All selected [ Test ] invalid values [ ExpectedException( typeof(ArgumentException) ) ] for month public void TestDemoInvalid( [ Values( int.MinValue, 0, 13, int.MaxValue ) ] int month, [ Values( -200100, -200004, -200003, -200000, -4, -1, 0, 1, 4, 1900, 2000, 2011, 2012, 400000, 400008, 400009, 400100 ) ] int year ) { int d = Days( month, year ); All selected } values for year
27
28
Coverage Example
request( floor )
Idle
[ pool is empty ]
Requested Stop
request( floor )
Moving
signal( floor ) [ floor nextStop ]
request( floor )
Peter Mller Software Engineering, SS 11
29
Reach floor
Keep moving Invalid state
Two test cases for transition coverage: empty and non-empty pool Not required for state coverage
30
8. Test Data
8.1 Functional Testing
8.1.1 Partition Testing 8.1.2 Selecting Representative Values 8.1.3 Combinatorial Testing
8.2 Structural Testing 8.3 Automatic Test Case Generation 8.4 Dynamic Test Generation 8.5 Mutation Testing
Peter Mller Software Engineering, SS 11
31
Combinatorial Testing
Combining equivalence classes and boundary testing leads to many values for each input
- Twelve values for month and 17 values for year in the Leap Year example
Testing all possible combinations leads to a combinatorial explosion (12 x 17 = 204 tests) Reduce test cases to make effort feasible
- Semantic constraints - Combinatorial selection - Random selection
Peter Mller Software Engineering, SS 11
32
Eliminating Combinations
Inspect test cases for unnecessary combinations
- Especially for invalid values - Use domain knowledge
month Month with 28 or 29 days month = 2 month {4, 6, 9, 11} month {1, 3, 5, 7, 8, 10, 12} month < 1 or month > 12
34
When selecting test data for objects, one has to consider object identities and aliasing Referenced objects lead to combination problem
Peter Mller Software Engineering, SS 11
35
Empty list, list of 1 and 5 (11) copies, list without duplicates, list with duplicates
List List of of 6 (12) 6 (12) copies copies and very large collection
This is a case of combinatorial testing since it combines the Library Member and the collection
Peter Mller Software Engineering, SS 11
36
- A Library Member m contains a Copy c in its collection of borrowed items if and only if cs Library Member is m - For each Library Member, the collection of borrowed items contains no duplicates - For each Library Member, the collection of borrowed items contains at most 6 copies (12 items for staff)
Peter Mller Software Engineering, SS 11
37
copy
Copy on shelf copy has zero library members
Staff member
Invalid library member Invalid staff member
0 11 borrowed items
more than 5 borrowed copies more than 11 borrowed items
38
Roots Example
Given three values, a, b, c, compute all solutions of the equation ax2 + bx + c = 0
a b any value c any value
Valid
Invalid
any value
infinity, NaN
infinity, NaN
infinity, NaN
39
One solution
40
One solution
a = 0 and b 0
No solution
a = 0, b = 0, and c 0
41
One solution
a = 0 and b 0
No solution
a = 0, b = 0, and c 0
a = 0, b = 0, c = 0
42
One option is to manually choose a manageable number of test cases such that there is at least one test case for each semantic constraint
- Note that omitting test cases might leave errors such as arithmetic overflow undetected
Peter Mller Software Engineering, SS 11
43
44
Vars 1 2 3 4 5 6
Medical NASA Network Browser Server Devices GSFC Security 66% 97% 99% 100% 29% 76% 95% 97% 99% 100% 42% 70% 89% 96% 96% 100% 68% 93% 98% 100% 20% 65% 90% 98% 100%
Network Security
45
Pairwise-Combinations Testing
Instead of testing all possible combinations of all inputs, focus on all possible combinations of each pair of inputs
- Pairwise-combinations testing is identical to combinatorial testing for two or less inputs
46
Result holds for large n and d, and for all k in k-way testing
47
Bug is still detected (depends only on a and b) Some cases depend on three parameters, e.g., invalid input
Peter Mller Software Engineering, SS 11
48
Should be combined with other approaches to detect errors that are triggered by more complex interactions among parameters
Peter Mller Software Engineering, SS 11
49
Representative Values
Test Cases