Testing

In this lesson, we look at how debugging the logic of a program is interrelated with testing the program.

We'll cover the following

Kinds of testing data

Before we can correct an error in logic, we must recognize that an error has occurred. To do so, we need to test the program. Thus, testing and debugging are interrelated activities. Ideally, a test should try all possible paths through a program’s logic. When those paths depend on some sort of input data, designing the test data set is the immediate task.

Testing should proceed using the following kinds of data:

• Typical data
• Extreme but acceptable data
• Unacceptable or incorrect data

We want our programs to behave well under all of these conditions. That is, a program should not crash or end in an infinite loop. A well-behaved program is robust, as we stated in an earlier Debugging Interlude.

Example

Let’s consider a simple example. Imagine that a class we intend to write needs a method to accept two integers, m, and n, as arguments and return either:

m2 + (m + 1)2 + … + n2, if 0 < mn

or

0 for other values of m and n

For example, if m is 3 and n is 5, our method should return 32 + 42 + 52—that is, 9 + 16 + 25, or 50. If either m or n, or both, is less than or equal to zero, or if m is larger than n, the method should return zero. Mathematically speaking, we have a function that our method will evaluate. We can denote the value of this function as f (m, n). The following method, sumOfSquares, implements this function.

Get hands-on with 1200+ tech skills courses.