Professional Documents
Culture Documents
1
Control-flow-based Testing Example of a Control Flow Graph
else
}
.....
bonus := 0.7;
break;
case MANAGER:
Note: other loops (e.g. FOR,
bonus := 1.5;
if (retiring_soon) DO-WHILE, …) are mapped similarly
bonus := 1.2 * bonus
break; Mini-assignment: figure out how this is done
case ...
endswitch CSE 757 - Software Testing 11 CSE 757 - Software Testing 12
2
Statement Coverage Example
Given the control flow graph, define a Suppose that we write and
execute two test cases 1
“coverage target” and write test cases to
achieve it Test case #1: follows path 2
1-2-exit
Traditional target: statement coverage 3
Test case #2: 1-2-3-4-5-
Test cases that cover all nodes 7-8-2-3-4-5-7-8-2-exit
(loop twice, and both times T 4 F
Code that has not been executed during take the true branch)
testing is more likely to contain errors 5 6
Problem: node 6 is never 7
Often this is the “low-probability” code executed, so we don’t have
100% statement coverage
8
CSE 757 - Software Testing 13 CSE 757 - Software Testing 14
Target: write test cases that cover all Statement coverage does not imply
branches of predicate nodes branch coverage
True and false branches of each IF
Example: if (c) then s;
The two branches corresponding to the
By executing only with c=true, we will achieve
condition of a loop
statement coverage, but not branch
All alternatives in a SWITCH coverage
In modern languages, branch coverage Motivation: experience shows that many
implies statement coverage errors occur in “decision making”
Plus, it subsumes statement coverage
3
Data-flow-based Testing Example
4
Data-flow-based Testing Black-box Testing
All-definitions: for each definition, cover Unlike white-box testing: don’t use any
at least one DU pair for that definition knowledge about the internals of the code
i.e., if x is defined at n1, execute at least
Test cases are designed based on
one path n1…n2 such that x is in USE(n2) and
the path is clear of definitions of x specifications
Motivation: see the effects of using the Example: search for a value in an array
values produced by computations Postcondition: return value is the index of
some occurrence of the value, or -1 if the
Focuses on the data, while control-flow-
value does not occur in the array
based testing focuses on the control
Example for input range 2..5: “less than 2”, Choosing test values
“between 2 and 5”, and “greater than 5” Choose a typical value in the middle of the
class(es) that represent valid input
Testing with values from different
classes is more likely to find errors than Choose values at the boundaries of classes
e.g. for [a..b], use a-1, a, a+1, b-1, b, b+1
testing with values from the same class
CSE 757 - Software Testing 27 CSE 757 - Software Testing 28
Spec says that the code accepts between Similarly for the output: exercise
4 and 24 inputs; each is a 3-digit integer boundary values
One partition: number of inputs Spec: the output is between 3 and 6
Classes “x<4”, “4<=x<=24”, “24<x” integers, each in the range 1000-2500
Chosen values: 3,4,5,14,23,24,25
Try to design inputs that produce
Another partition: integer values 3 outputs with value 1000
Classes: “x<100”, “100<=x<=999”, “999<x” 3 outputs with value 2500
Chosen values: 99,100,101,500,998,999,1000 6 outputs with value 1000
6 outputs with value 2500
CSE 757 - Software Testing 29 CSE 757 - Software Testing 30
5
Example: Searching Example: Searching
Testing Strategies
Unit testing: scope = individual component Scope: one component from the design
Focus: component correctness Often corresponds to the notion of
White-box and black-box techniques “compilation unit” from the prog. language
Integration testing: scope = set of Responsibility of the developer
interacting components
Focus: correctness of component interactions Both white-box and black-box techniques
Mostly black-box, some white-box May be necessary to create stubs: “fake”
System testing: scope = entire system code that replaces called modules
Focus: overall system correctness If not yet implemented, or not yet tested
Only black-box techniques
CSE 757 - Software Testing 35 CSE 757 - Software Testing 36
6
Basic Strategy for Unit Testing Test-First Programming
Developers don’t “skip” unit testing Goal: find whether the program does
Satisfying for the programmer: feeling of what the customer expects to see
accomplishment when the tests pass Black-box
Helps clarify interface and behavior From requirements analysis, there should
before programming be validation criteria
To write tests for something, first you need How are the developers and the customers
to understand it well going to agree that the software is OK?
Software evolution Many issues: functionality, performance,
After changing existing code, rerun the tests usability, portability, etc.
to gain confidence (regression testing)
CSE 757 - Software Testing 39 CSE 757 - Software Testing 40
Initial system testing is done by the For multiple customers: two phases
software producer
Alpha testing: at the vendor’s site by a
Eventually need testing by the customers few customers
Customers are great testers
Beta testing: distributed to many end-
If the software is built for a single users
customer: series of acceptance tests Users run it in their own environment
Deploy in the customer environment and have Sometimes done by thousands of users
end-users run it
7
Stress Testing Stress Testing (cont)
Form of system testing: behavior under Find how the system deals with overload
very heavy load
Reason 1: determine failure behavior
e.g. normally there are 1-2 interrupts per
If the load goes above the intended, how
second; what if there are 10 interrupts?
“gracefully” does the system fail?
e.g. what if data sets that are an order of
magnitude larger than normal? Reason 2: expose bugs that only occur
e.g. what if our server gets 10 times more under heavy loads
client requests than normally expected? Especially for OS, middleware, servers, etc.
e.g. memory leaks, incorrect resource
allocation and scheduling, race conditions
Regression Testing
8
Class Testing Example: Inter-method DU Pairs
class A {
Traditional black-box and white-box
private int index;
techniques still apply public void m1() {
E.g. testing with boundary values index = …;
Inside each method: at least 100% branch …
coverage; also, DU-pairs inside a method m2();
}
Extension: DU pairs that cross method private void m2() { … x = index; … }
boundaries public void m3() { … z = index; … }
Example: inside method m1, field f is }
assigned a value; inside method m2, this test 1: call m1, which calls m2 and
value is read reads the value of index
test 2: call m1, and then call m3
CSE 757 - Software Testing 49 CSE 757 - Software Testing 50
Example: class A with subclasses B and C During class testing of X: “drive” call site
class A { … void m() {…} …} a.m() through all possible bindings
class B extends A { … }
All-receiver-classes: execute with at
class C extends A { … void m() {…} … } least one receiver of class A, at least one
Suppose inside class X there is call a.m(), receiver of class B, and at least one
where variable a is of type A receiver of class C
Could potentially send message m() to an All-invoked-methods: need to execute
instance of A, instance of B, or instance of C
with receivers that cover A.m and C.m
The invoked method could be A.m or C.m
i.e. (A or B receiver) and (C receiver)
9
State-based Testing Example: Stack
Each valid transition should be tested People thought that inheritance will
Verify the resulting state using a state reduce the need for testing
inspector that has access to the internals of Claim 1: “If we have a well-tested superclass,
the class we can reuse its code (in subclasses, through
inheritance) without retesting inherited
Each invalid transition should be tested code”
to ensure that it is rejected and the
Claim 2: “A good-quality test suite used for a
state does not change superclass will also be good for a subclass”
e.g. Full -> Full is not allowed: we should call
add on a full stack Both claims are wrong
10
Problems with Inheritance Testing of Inheritance
Test cases for a method m defined in Until now we only talked about testing of
class X are not necessarily good for individual classes
retesting m in subclasses of X
Class testing is not sufficient
e.g. if m calls m2 in A, and then some
OO design: several classes collaborate to
subclass overrides m2, we have a completely
implement the desired functionality
new interaction
11
UML Interaction Diagrams for Testing Collaboration Diagram
Coverage Requirements
12