Next: Large-scale testing. Up: Testing and debugging. Previous: None.


Testing

Solid testing requires the following three steps.

  1. Generate a test case.
  2. Determine what the results should be.
  3. Run the program to observe its results.
  4. Observe how the program's results differ from expectations.
This is really very similar to the classic scientific method (hypothesis, procedure, results, conclusion).

Testing falls into two categories: white-box testing and black-box testing.

White-box testing

White-box testing involves generating test cases while looking at the code. Generally, you're looking for a large enough set of test cases to hit all the cases in the code.

For example: Consider the following piece of code to find the maximum of 5 numbers typed by the user.

int max = IO.readInt();

for(int i = 0; i < 4; i++) {
    int q = IO.readInt();
    if(q > max) {
        max = q;
    }
}
If we were trying to hit all execution paths, we'd have to cover all 16 combinations of hitting/missing the if condition. Such examples could include the following.
1 1 1 1 1     1 1 1 1 5
1 1 1 4 1     1 1 1 4 5
1 1 3 1 1     1 1 3 1 5
1 1 3 4 1     1 1 3 4 5
1 2 1 1 1     1 2 1 1 5
1 2 1 4 1     1 2 1 4 5
1 2 3 1 1     1 2 3 1 5
1 2 3 4 1     1 2 3 4 5
If we modified the program to find the the maximum of 20 numbers, we'd have 220 > 1 million different cases. This is just not reasonable.

If you settle for the second case, then there is just one case to test: And that case must have the if condition be true at some time.

1 2 1 1 1
Of course, this isn't as thorough, but it at least checks the fundamentals: That the statement wasn't a complete catastrophe.

Black-box testing

In black-box testing, the tester generates test cases without reference to the source code - that is, the tester is treating the program as a black box, into which the tester cannot look.

Beta testing obviously always involves black-box testing. But even the original software developer does this. In fact, it's probably the primary kind of testing you've been doing on your laboratories: Once you have the program coded, you run it by acting like a regular user.

Good black-box testing will include tests falling into three categories.

After finding a bug in black-box testing, it's often a good idea to try to prune the test case down to try to determine exactly what's going on. You'd do this before you even begin to try to debug, because the simpler test case will generally illustrate the actual problems better.


Next: Large-scale testing. Up: Testing and debugging. Previous: None.