Unit Testing
Unit Testing
courses.cs.washington.edu/courses/cse403/15sp/
Unit Testing
Emina Torlak
emina@cs.washington.edu
Outline
2
intro
basics of software quality control
Errors and faults
4
Errors and faults
4
Errors and faults
4
Software quality control techniques
5
Showing the presence and absence of bugs …
testing verification
6
Showing the presence and absence of bugs …
testing verification
6
Showing the presence and absence of bugs …
testing verification
6
Common kinds of testing
7
unit
effective unit testing
Two rules of unit testing
• Be systematic
• If you thrash about arbitrarily, the bugs will hide in
the corner until you're gone
9
Four basic steps of a test
10
Four basic steps of a test
10
Four basic steps of a test
10
Four basic steps of a test
10
Four basic steps of a test
10
Four basic steps of a test
10
Choosing inputs: two key ideas
11
Choosing inputs: two key ideas
11
Choosing inputs: two key ideas
• Boundary values
• Pick inputs at the edges of the subdomains.
• Effective at finding corner case bugs:
• off-by-one, overflow, aliasing, empty container
11
Partitioning the input space
• Partition into
• a < b, a = b, a > b
12
Partitioning the input space
• Partition into
• a < b, a = b, a > b
How would you partition the
input space for
• Pick an input from each class • BigInteger multiplication?
• (1, 2), (0, 0), (2, 1) • Set intersection?
12
Choosing boundary values
// returns|x|
public static int abs(int a) { … }
• Partition into
• a < 0, a > 0, a = 0 (boundary)
13
Choosing boundary values
// returns|x|
public static int abs(int a) { … }
• Partition into
• a < 0, a > 0, a = 0 (boundary)
What are good boundary
• Other boundary values values for objects?
• Integer.MAX_VALUE
• Integer.MIN_VALUE
13
Black box testing
• 3 paths, so 3 subdomains
• (1, 2) => 2
• (2, 1) => 2
• (0, 0) => 0
14
Advantages of black box testing
15
Disadvantage of black box testing
16
White box testing
boolean isPrime(int x) {
if (x>CACHE_SIZE) {
for (int i=2; i<x/2; i++) {
if (x%i==0) return false;
}
return true;
} else {
return primeTable[x];
}
}
17
(Dis)advantages of white box testing
18
(Dis)advantages of white box testing
• Advantages
• Finds an important class of boundaries.
• Yields useful test cases.
• In isPrime example, need to check numbers on each
side of CACHE_SIZE
• CACHE_SIZE-1, CACHE_SIZE, CACHE_SIZE+1
18
(Dis)advantages of white box testing
• Advantages
• Finds an important class of boundaries.
• Yields useful test cases.
• In isPrime example, need to check numbers on each
side of CACHE_SIZE
• CACHE_SIZE-1, CACHE_SIZE, CACHE_SIZE+1
• Disadvantages
• Tests may have the same bugs as implementation!
18
Properties of good and bad unit tests
19
cover
coverage and regression testing
Measuring test suite quality with coverage
21
Measuring test suite quality with coverage
21
Measuring test suite quality with coverage
• Limitations of coverage
• Coverage is just a heuristic.
• 100% coverage may not be achievable.
• High-cost to approach the limit.
21
Coverage measuring tools: EclEmma
22
Regression testing
23
Regression testing
23
Regression testing
23
Summary
24