Programming and Problem Solving with Java

(やまだぃちぅ) #1

(^192) | Selection and Encapsulation
We need at least three data sets to test the different branches. For example, the following
sets of argument values cause all of the branches to be executed:
Score 1Score 2Score 3
Set 1 75 75 75
Set 2 65 65 65
Set 3 50 50 50
Every branch in the code is executed at least once through this series of test runs.
Eliminating any of the test data sets would leave at least one branch untested. This series
of data sets provides what is called minimum complete coverageof the application’s branch-
ing structure. Whenever you test an application containing branches, you should design
a series of tests that cover all of the branches. Because an action in one branch often af-
fects processing in a later branch, it is critical to test as many combinations of branches,or
paths, through the code as possible. By doing so, we can be sure that no interdependen-
cies will cause problems. Shouldn’t we try all possible paths? Yes, in theory. However, the
number of paths in even a small application can be very large.
The approach to testing that we’ve used here is calledcode coveragebecause the test
data are designed by looking at the code. Code coverage is also calledwhite-box(orclear-box)
testingbecause we are allowed to see the application code while designing the tests. Another
approach to testing, calleddata coverage, attempts to test as many allowable data values as
possible without referencing the code. Because we need not see the code in this form of test-
ing, it is also calledblack-box testing—we would design the same set of tests even if the code
were hidden in a black box. Complete data coverage is as impractical as complete code cov-
erage for many applications. For example, theStudentconstructor bases the student’s status
on the average of threeintvalues in the range of 0 to 100 and thus has more than 1,000,000
possible input values.
Often, testing combines these two strategies. Instead of trying every possible data
value (data coverage), we examine the code (code coverage) and look for ranges of values
for which processing is identical. Then we test the values at the boundaries and, some-
times, a value in the middle of each range. For example, a simple condition such as
alpha < 0
divides the integers into two ranges:
1.Integer.MIN_VALUE through– 1



  1. 0 throughInteger.MAX_VALUE
    Thus, we should test the four valuesInteger.MIN_VALUE,1, 0, and Integer.MAX_VALUE. A com-
    pound condition such as


alpha >= 0 && alpha <= 100
Free download pdf